[Cache] Count cache hits/misses in ProxyAdapter
This commit is contained in:
parent
38c36c2f67
commit
e6f21f999e
@ -22,6 +22,8 @@ class ProxyAdapter implements CacheItemPoolInterface
|
||||
{
|
||||
private $pool;
|
||||
private $createCacheItem;
|
||||
private $hits = 0;
|
||||
private $misses = 0;
|
||||
|
||||
public function __construct(CacheItemPoolInterface $pool)
|
||||
{
|
||||
@ -47,8 +49,13 @@ class ProxyAdapter implements CacheItemPoolInterface
|
||||
{
|
||||
$f = $this->createCacheItem;
|
||||
$item = $this->pool->getItem($key);
|
||||
if ($isHit = $item->isHit()) {
|
||||
++$this->hits;
|
||||
} else {
|
||||
++$this->misses;
|
||||
}
|
||||
|
||||
return $f($key, $item->get(), $item->isHit());
|
||||
return $f($key, $item->get(), $isHit);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -134,7 +141,33 @@ class ProxyAdapter implements CacheItemPoolInterface
|
||||
$f = $this->createCacheItem;
|
||||
|
||||
foreach ($items as $key => $item) {
|
||||
yield $key => $f($key, $item->get(), $item->isHit());
|
||||
if ($isHit = $item->isHit()) {
|
||||
++$this->hits;
|
||||
} else {
|
||||
++$this->misses;
|
||||
}
|
||||
|
||||
yield $key => $f($key, $item->get(), $isHit);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of cache read hits.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getHits()
|
||||
{
|
||||
return $this->hits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of cache read misses.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getMisses()
|
||||
{
|
||||
return $this->misses;
|
||||
}
|
||||
}
|
||||
|
@ -29,4 +29,21 @@ class ProxyAdapterTest extends CachePoolTest
|
||||
{
|
||||
return new ProxyAdapter(new ArrayAdapter());
|
||||
}
|
||||
|
||||
public function testGetHitsMisses()
|
||||
{
|
||||
$pool = $this->createCachePool();
|
||||
|
||||
$this->assertSame(0, $pool->getHits());
|
||||
$this->assertSame(0, $pool->getMisses());
|
||||
|
||||
$bar = $pool->getItem('bar');
|
||||
$this->assertSame(0, $pool->getHits());
|
||||
$this->assertSame(1, $pool->getMisses());
|
||||
|
||||
$pool->save($bar->set('baz'));
|
||||
$bar = $pool->getItem('bar');
|
||||
$this->assertSame(1, $pool->getHits());
|
||||
$this->assertSame(1, $pool->getMisses());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user