Skip to content

Commit

Permalink
[Cache] fixed TagAwareAdapter returning invalid cache
Browse files Browse the repository at this point in the history
  • Loading branch information
v-m-i committed Oct 11, 2019
1 parent fefb2ff commit 93b9d9f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
Expand Up @@ -156,7 +156,15 @@ public function hasItem($key)
if (!$this->pool->hasItem($key)) {
return false;
}
if (!$itemTags = $this->pool->getItem(static::TAGS_PREFIX.$key)->get()) {

//always get the item instead of calling "hasItem" and then "getItem" as having cache miss on getting item tags is rare occurrence
$itemTags = $this->pool->getItem(static::TAGS_PREFIX.$key);

if (!$itemTags->isHit()) {
return false;
}

if (!$itemTags = $itemTags->get()) {
return true;
}

Expand Down Expand Up @@ -296,7 +304,11 @@ private function generateItems($items, array $tagKeys)
}

unset($tagKeys[$key]);
$itemTags[$key] = $item->get() ?: [];

//item is tag
if ($item->isHit()) {
$itemTags[$key] = $item->get() ?: [];
}

if (!$tagKeys) {
$tagVersions = $this->getTagVersions($itemTags);
Expand Down
45 changes: 45 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php
Expand Up @@ -194,6 +194,51 @@ public function testKnownTagVersionsTtl()
$this->assertTrue($pool->getItem('foo')->isHit());
}

public function testTagEntryIsCreatedForItemWithoutTags()
{
$pool = $this->createCachePool();

$itemKey = 'foo';
$item = $pool->getItem($itemKey);
$pool->save($item);

$adapter = new FilesystemAdapter();
$this->assertTrue($adapter->hasItem(TagAwareAdapter::TAGS_PREFIX.$itemKey));
}

public function testHasItemReturnsFalseWhenPoolDoesNotHaveItemTags()
{
$pool = $this->createCachePool();

$itemKey = 'foo';
$item = $pool->getItem($itemKey);
$pool->save($item);

$anotherPool = $this->createCachePool();

$adapter = new FilesystemAdapter();
$adapter->deleteItem(TagAwareAdapter::TAGS_PREFIX.$itemKey); //simulate item losing tags pair

$this->assertFalse($anotherPool->hasItem($itemKey));
}

public function testGetItemReturnsCacheMissWhenPoolDoesNotHaveItemTags()
{
$pool = $this->createCachePool();

$itemKey = 'foo';
$item = $pool->getItem($itemKey);
$pool->save($item);

$anotherPool = $this->createCachePool();

$adapter = new FilesystemAdapter();
$adapter->deleteItem(TagAwareAdapter::TAGS_PREFIX.$itemKey); //simulate item losing tags pair

$item = $anotherPool->getItem($itemKey);
$this->assertFalse($item->isHit());
}

/**
* @return MockObject|PruneableCacheInterface
*/
Expand Down

0 comments on commit 93b9d9f

Please sign in to comment.