Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b868feb

Browse filesBrowse files
committed
feature #17734 [Cache] Count cache hits/misses in ProxyAdapter (nicolas-grekas)
This PR was merged into the 3.1-dev branch. Discussion ---------- [Cache] Count cache hits/misses in ProxyAdapter | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17537 partially | License | MIT | Doc PR | - I propose to add this subset of the `Doctrine\Common\Cache\Cache` interface so that we can build data collectors on top and show these stats in the web profiler. ping @javiereguiluz Commits ------- e6f21f9 [Cache] Count cache hits/misses in ProxyAdapter
2 parents 1330662 + e6f21f9 commit b868feb
Copy full SHA for b868feb

File tree

Expand file treeCollapse file tree

2 files changed

+52
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+52
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
+35-2Lines 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;
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Tests/Adapter/ProxyAdapterTest.php
+17Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.