File tree Expand file tree Collapse file tree 2 files changed +52
-2
lines changed
src/Symfony/Component/Cache Expand file tree Collapse file tree 2 files changed +52
-2
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ class ProxyAdapter implements CacheItemPoolInterface
22
22
{
23
23
private $ pool ;
24
24
private $ createCacheItem ;
25
+ private $ hits = 0 ;
26
+ private $ misses = 0 ;
25
27
26
28
public function __construct (CacheItemPoolInterface $ pool )
27
29
{
@@ -47,8 +49,13 @@ public function getItem($key)
47
49
{
48
50
$ f = $ this ->createCacheItem ;
49
51
$ item = $ this ->pool ->getItem ($ key );
52
+ if ($ isHit = $ item ->isHit ()) {
53
+ ++$ this ->hits ;
54
+ } else {
55
+ ++$ this ->misses ;
56
+ }
50
57
51
- return $ f ($ key , $ item ->get (), $ item -> isHit () );
58
+ return $ f ($ key , $ item ->get (), $ isHit );
52
59
}
53
60
54
61
/**
@@ -134,7 +141,33 @@ private function generateItems($items)
134
141
$ f = $ this ->createCacheItem ;
135
142
136
143
foreach ($ items as $ key => $ item ) {
137
- yield $ key => $ f ($ key , $ item ->get (), $ item ->isHit ());
144
+ if ($ isHit = $ item ->isHit ()) {
145
+ ++$ this ->hits ;
146
+ } else {
147
+ ++$ this ->misses ;
148
+ }
149
+
150
+ yield $ key => $ f ($ key , $ item ->get (), $ isHit );
138
151
}
139
152
}
153
+
154
+ /**
155
+ * Returns the number of cache read hits.
156
+ *
157
+ * @return int
158
+ */
159
+ public function getHits ()
160
+ {
161
+ return $ this ->hits ;
162
+ }
163
+
164
+ /**
165
+ * Returns the number of cache read misses.
166
+ *
167
+ * @return int
168
+ */
169
+ public function getMisses ()
170
+ {
171
+ return $ this ->misses ;
172
+ }
140
173
}
Original file line number Diff line number Diff line change @@ -29,4 +29,21 @@ public function createCachePool()
29
29
{
30
30
return new ProxyAdapter (new ArrayAdapter ());
31
31
}
32
+
33
+ public function testGetHitsMisses ()
34
+ {
35
+ $ pool = $ this ->createCachePool ();
36
+
37
+ $ this ->assertSame (0 , $ pool ->getHits ());
38
+ $ this ->assertSame (0 , $ pool ->getMisses ());
39
+
40
+ $ bar = $ pool ->getItem ('bar ' );
41
+ $ this ->assertSame (0 , $ pool ->getHits ());
42
+ $ this ->assertSame (1 , $ pool ->getMisses ());
43
+
44
+ $ pool ->save ($ bar ->set ('baz ' ));
45
+ $ bar = $ pool ->getItem ('bar ' );
46
+ $ this ->assertSame (1 , $ pool ->getHits ());
47
+ $ this ->assertSame (1 , $ pool ->getMisses ());
48
+ }
32
49
}
You can’t perform that action at this time.
0 commit comments