55use phpFastCache \Cache \ExtendedCacheItemPoolInterface ;
66use phpFastCache \CacheManager ;
77use phpFastCache \Exceptions \phpFastCacheDriverException ;
8+ use Symfony \Component \Stopwatch \Stopwatch ;
89
910/**
1011 * Class Cache
1112 * @package phpFastCache\Bundle\Service
1213 */
1314class Cache
1415{
16+ /**
17+ * @var array
18+ */
1519 private $ drivers = [];
1620
21+ /**
22+ * @var Stopwatch
23+ */
24+ protected $ stopwatch ;
25+
1726 /**
1827 * Contains all cache instances
1928 *
@@ -25,12 +34,14 @@ class Cache
2534 * Cache constructor.
2635 *
2736 * @param $drivers
37+ * @param Stopwatch $stopwatch
2838 *
2939 * @throws \phpFastCache\Exceptions\phpFastCacheDriverException
3040 */
31- public function __construct ($ drivers )
41+ public function __construct ($ drivers, Stopwatch $ stopwatch = null )
3242 {
3343 $ this ->drivers = (array ) $ drivers [ 'drivers ' ];
44+ $ this ->stopwatch = $ stopwatch ;
3445 }
3546
3647 /**
@@ -60,6 +71,10 @@ public function createInstance($name, ExtendedCacheItemPoolInterface $instance)
6071 */
6172 public function get ($ name )
6273 {
74+ if ($ this ->stopwatch ) {
75+ $ this ->stopwatch ->start (__METHOD__ . "(' {$ name }') " );
76+ }
77+
6378 if (!array_key_exists ($ name , $ this ->cacheInstances )) {
6479 if (array_key_exists ($ name , $ this ->drivers )) {
6580 $ this ->createInstance ($ name , CacheManager::getInstance ($ this ->drivers [ $ name ][ 'type ' ], $ this ->drivers [ $ name ][ 'parameters ' ]));
@@ -71,6 +86,9 @@ public function get($name)
7186 }
7287 }
7388
89+ if ($ this ->stopwatch ) {
90+ $ this ->stopwatch ->stop (__METHOD__ . "(' {$ name }') " );
91+ }
7492 return $ this ->cacheInstances [ $ name ];
7593 }
7694
0 commit comments