99 * with this source code in the file LICENSE.
1010 */
1111
12- namespace Cache \CacheBundle \Cache ;
12+ namespace Cache \CacheBundle \Cache \ Recording ;
1313
1414use Cache \Taggable \TaggablePoolInterface ;
15- use Cache \Taggable \TaggablePSR6PoolAdapter ;
1615use Psr \Cache \CacheItemInterface ;
1716use Psr \Cache \CacheItemPoolInterface ;
17+ use Psr \Log \LoggerInterface ;
1818
1919/**
20+ * A pool that logs and collects all your cache calls.
21+ *
2022 * @author Aaron Scherer <aequasi@gmail.com>
23+ * @author Tobias Nyholm <tobias.nyholm@gmail.com>
2124 */
22- class RecordingCachePool implements CacheItemPoolInterface, TaggablePoolInterface
25+ class CachePool implements CacheItemPoolInterface, TaggablePoolInterface
2326{
2427 /**
2528 * @type array
@@ -31,14 +34,29 @@ class RecordingCachePool implements CacheItemPoolInterface, TaggablePoolInterfac
3134 */
3235 private $ cachePool ;
3336
37+ /**
38+ * @type LoggerInterface
39+ */
40+ private $ logger ;
41+
42+ /**
43+ * @type string
44+ */
45+ private $ name ;
46+
47+ /**
48+ * @type string
49+ */
50+ private $ level = 'info ' ;
51+
3452 /**
3553 * LoggingCachePool constructor.
3654 *
3755 * @param CacheItemPoolInterface $cachePool
3856 */
3957 public function __construct (CacheItemPoolInterface $ cachePool )
4058 {
41- $ this ->cachePool = TaggablePSR6PoolAdapter:: makeTaggable ( $ cachePool) ;
59+ $ this ->cachePool = $ cachePool ;
4260 }
4361
4462 /**
@@ -49,6 +67,8 @@ public function __construct(CacheItemPoolInterface $cachePool)
4967 protected function addCall ($ call )
5068 {
5169 $ this ->calls [] = $ call ;
70+
71+ $ this ->writeLog ($ call );
5272 }
5373
5474 /**
@@ -57,7 +77,7 @@ protected function addCall($call)
5777 *
5878 * @return object
5979 */
60- private function timeCall ($ name , array $ arguments = [])
80+ protected function timeCall ($ name , array $ arguments = [])
6181 {
6282 $ start = microtime (true );
6383 $ result = call_user_func_array ([$ this ->cachePool , $ name ], $ arguments );
@@ -198,4 +218,61 @@ private function getValueRepresentation($value)
198218
199219 return $ rep ;
200220 }
221+
222+ protected function writeLog ($ call )
223+ {
224+ if (!$ this ->logger ) {
225+ return ;
226+ }
227+
228+ $ data = [
229+ 'name ' => $ this ->name ,
230+ 'method ' => $ call ->name ,
231+ 'arguments ' => json_encode ($ call ->arguments ),
232+ 'hit ' => isset ($ call ->isHit ) ? $ call ->isHit ? 'True ' : 'False ' : 'Invalid ' ,
233+ 'time ' => round ($ call ->time * 1000 , 2 ),
234+ 'result ' => $ call ->result ,
235+ ];
236+
237+ $ this ->logger ->log (
238+ $ this ->level ,
239+ sprintf ('[Cache] Provider: %s. Method: %s(%s). Hit: %s. Time: %sms. Result: %s ' ,
240+ $ data ['name ' ],
241+ $ data ['method ' ],
242+ $ data ['arguments ' ],
243+ $ data ['hit ' ],
244+ $ data ['time ' ],
245+ $ data ['result ' ]
246+ ),
247+ $ data
248+ );
249+ }
250+
251+ /**
252+ * @param LoggerInterface $logger
253+ */
254+ public function setLogger (LoggerInterface $ logger = null )
255+ {
256+ $ this ->logger = $ logger ;
257+ }
258+
259+ /**
260+ * @param string $name
261+ */
262+ public function setName ($ name )
263+ {
264+ $ this ->name = $ name ;
265+
266+ return $ this ;
267+ }
268+
269+ /**
270+ * @param string $level
271+ */
272+ public function setLevel ($ level )
273+ {
274+ $ this ->level = $ level ;
275+
276+ return $ this ;
277+ }
201278}
0 commit comments