8000 [Cache] Count cache hits/misses in ProxyAdapter · symfony/symfony@e6f21f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit e6f21f9

Browse files
[Cache] Count cache hits/misses in ProxyAdapter
1 parent 38c36c2 commit e6f21f9

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/Symfony/Component/Cache/Adapter/ProxyAdapter.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class ProxyAdapter implements CacheItemPoolInterface
2222
{
2323
private $pool;
2424
private $createCacheItem;
25+
private $hits = 0;< 8000 /div>
26+
private $misses = 0;
2527

2628
public function __construct(CacheItemPoolInterface $pool)
2729
{
@@ -47,8 +49,13 @@ public function getItem($key)
4749
{
4850
$f = $this->createCacheItem;
4951
$item = $this->pool->getItem($key);
52+
if ($isHit = $item->isHit()) {
53+
++$this->hits;
54+
} else {
55+
++$this->misses;
56+
}
5057

51-
return $f($key, $item->get(), $item->isHit());
58+
return $f($key, $item->get(), $isHit);
5259
}
5360

5461
/**
@@ -134,7 +141,33 @@ private function generateItems($items)
134141
$f = $this->createCacheItem;
135142

136143
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);
138151
}
139152
}
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+
}
140173
}

src/Symfony/Component/Cache/Tests/Adapter/ProxyAdapterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,21 @@ public function createCachePool()
2929
{
3030
return new ProxyAdapter(new ArrayAdapter());
3131
}
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+
}
3249
}

0 commit comments

Comments
 (0)
0