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 93b9d9f

Browse filesBrowse files
committed
[Cache] fixed TagAwareAdapter returning invalid cache
1 parent fefb2ff commit 93b9d9f
Copy full SHA for 93b9d9f

File tree

Expand file treeCollapse file tree

2 files changed

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

2 files changed

+59
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
+14-2Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,15 @@ public function hasItem($key)
156156
if (!$this->pool->hasItem($key)) {
157157
return false;
158158
}
159-
if (!$itemTags = $this->pool->getItem(static::TAGS_PREFIX.$key)->get()) {
159+
160+
//always get the item instead of calling "hasItem" and then "getItem" as having cache miss on getting item tags is rare occurrence
161+
$itemTags = $this->pool->getItem(static::TAGS_PREFIX.$key);
162+
163+
if (!$itemTags->isHit()) {
164+
return false;
165+
}
166+
167+
if (!$itemTags = $itemTags->get()) {
160168
return true;
161169
}
162170

@@ -296,7 +304,11 @@ private function generateItems($items, array $tagKeys)
296304
}
297305

298306
unset($tagKeys[$key]);
299-
$itemTags[$key] = $item->get() ?: [];
307+
308+
//item is tag
309+
if ($item->isHit()) {
310+
$itemTags[$key] = $item->get() ?: [];
311+
}
300312

301313
if (!$tagKeys) {
302314
$tagVersions = $this->getTagVersions($itemTags);

‎src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,51 @@ public function testKnownTagVersionsTtl()
194194
$this->assertTrue($pool->getItem('foo')->isHit());
195195
}
196196

197+
public function testTagEntryIsCreatedForItemWithoutTags()
198+
{
199+
$pool = $this->createCachePool();
200+
201+
$itemKey = 'foo';
202+
$item = $pool->getItem($itemKey);
203+
$pool->save($item);
204+
205+
$adapter = new FilesystemAdapter();
206+
$this->assertTrue($adapter->hasItem(TagAwareAdapter::TAGS_PREFIX.$itemKey));
207+
}
208+
209+
public function testHasItemReturnsFalseWhenPoolDoesNotHaveItemTags()
210+
{
211+
$pool = $this->createCachePool();
212+
213+
$itemKey = 'foo';
214+
$item = $pool->getItem($itemKey);
215+
$pool->save($item);
216+
217+
$anotherPool = $this->createCachePool();
218+
219+
$adapter = new FilesystemAdapter();
220+
$adapter->deleteItem(TagAwareAdapter::TAGS_PREFIX.$itemKey); //simulate item losing tags pair
221+
222+
$this->assertFalse($anotherPool->hasItem($itemKey));
223+
}
224+
225+
public function testGetItemReturnsCacheMissWhenPoolDoesNotHaveItemTags()
226+
{
227+
$pool = $this->createCachePool();
228+
229+
$itemKey = 'foo';
230+
$item = $pool->getItem($itemKey);
231+
$pool->save($item);
232+
233+
$anotherPool = $this->createCachePool();
234+
235+
$adapter = new FilesystemAdapter();
236+
$adapter->deleteItem(TagAwareAdapter::TAGS_PREFIX.$itemKey); //simulate item losing tags pair
237+
238+
$item = $anotherPool->getItem($itemKey);
239+
$this->assertFalse($item->isHit());
240+
}
241+
197242
/**
198243
* @return MockObject|PruneableCacheInterface
199244
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.