diff --git a/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php b/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php index 7e5c9aa20393d..eaeb5ba4fbd44 100644 --- a/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php @@ -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 @@ public function getItem($key) { $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 @@ private function generateItems($items) $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; + } } diff --git a/src/Symfony/Component/Cache/Tests/Adapter/ProxyAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/ProxyAdapterTest.php index 76606e256eea6..58594b0e1ef0d 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/ProxyAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/ProxyAdapterTest.php @@ -29,4 +29,21 @@ public function createCachePool() { 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()); + } }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: