5
5
use phpFastCache \Cache \ExtendedCacheItemPoolInterface ;
6
6
use phpFastCache \CacheManager ;
7
7
use phpFastCache \Exceptions \phpFastCacheDriverException ;
8
+ use Symfony \Component \Stopwatch \Stopwatch ;
8
9
9
10
/**
10
11
* Class Cache
11
12
* @package phpFastCache\Bundle\Service
12
13
*/
13
14
class Cache
14
15
{
16
+ /**
17
+ * @var array
18
+ */
15
19
private $ drivers = [];
16
20
21
+ /**
22
+ * @var Stopwatch
23
+ */
24
+ protected $ stopwatch ;
25
+
17
26
/**
18
27
* Contains all cache instances
19
28
*
@@ -25,12 +34,14 @@ class Cache
25
34
* Cache constructor.
26
35
*
27
36
* @param $drivers
37
+ * @param Stopwatch $stopwatch
28
38
*
29
39
* @throws \phpFastCache\Exceptions\phpFastCacheDriverException
30
40
*/
31
- public function __construct ($ drivers )
41
+ public function __construct ($ drivers, Stopwatch $ stopwatch = null )
32
42
{
33
43
$ this ->drivers = (array ) $ drivers [ 'drivers ' ];
44
+ $ this ->stopwatch = $ stopwatch ;
34
45
}
35
46
36
47
/**
@@ -60,6 +71,10 @@ public function createInstance($name, ExtendedCacheItemPoolInterface $instance)
60
71
*/
61
72
public function get ($ name )
62
73
{
74
+ if ($ this ->stopwatch ) {
75
+ $ this ->stopwatch ->start (__METHOD__ . "(' {$ name }') " );
76
+ }
77
+
63
78
if (!array_key_exists ($ name , $ this ->cacheInstances )) {
64
79
if (array_key_exists ($ name , $ this ->drivers )) {
65
80
$ this ->createInstance ($ name , CacheManager::getInstance ($ this ->drivers [ $ name ][ 'type ' ], $ this ->drivers [ $ name ][ 'parameters ' ]));
@@ -71,6 +86,9 @@ public function get($name)
71
86
}
72
87
}
73
88
89
+ if ($ this ->stopwatch ) {
90
+ $ this ->stopwatch ->stop (__METHOD__ . "(' {$ name }') " );
91
+ }
74
92
return $ this ->cacheInstances [ $ name ];
75
93
}
76
94
0 commit comments