8000 [Cache] fixed TagAwareAdapter returning invalid cache · symfony/symfony@a23b2c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit a23b2c3

Browse files
committed
[Cache] fixed TagAwareAdapter returning invalid cache
1 parent fefb2ff commit a23b2c3

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
8000
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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,84 @@ 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+
242+
public function testHasItemReturnsFalseWhenPoolDoesNotHaveItemAndOnlyHasTags()
243+
{
244+
$pool = $this->createCachePool();
245+
246+
$itemKey = 'foo';
247+
$item = $pool->getItem($itemKey);
248+
$pool->save($item);
249+
250+
$anotherPool = $this->createCachePool();
251+
252+
$adapter = new FilesystemAdapter();
253+
$adapter->deleteItem($itemKey); //simulate losing item but keeping tags
254+
255+
$this->assertFalse($anotherPool->hasItem($itemKey));
256+
}
257+
258+
public function testGetItemReturnsCacheMissWhenPoolDoesNotHaveItemAndOnlyHasTags()
259+
{
260+
$pool = $this->createCachePool();
261+
262+
$itemKey = 'foo';
263+
$item = $pool->getItem($itemKey);
264+
$pool->save($item);
265+
266+
$anotherPool = $this->createCachePool();
267+
268+
$adapter = new FilesystemAdapter();
269+
$adapter->deleteItem($itemKey); //simulate losing item but keeping tags
270+
271+
$item = $anotherPool->getItem($itemKey);
272+
$this->assertFalse($item->isHit());
273+
}
274+
197275
/**
198276
* @return MockObject|PruneableCacheInterface
199277
*/

0 commit comments

Comments
 (0)
0