Skip to content

Commit

Permalink
bug #33962 [Cache] fixed TagAwareAdapter returning invalid cache (v-m-i)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

[Cache] fixed TagAwareAdapter returning invalid cache

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #33953
| License       | MIT
| Doc PR        |

This PR fixes `TagAwareAdapter` returning `CacheItem` when item-tags pair is missing tag key in pool. Currently `TagAwareAdapter` returns `CacheItem` with empty tags and `isHit` set to `true`. With this PR it returns `CacheItem` with `isHit` set to `false` as we can't know if item is valid or invalid when it's missing tag entry so we treat it as cache miss.

Commits
-------

946f0a1 [Cache] fixed TagAwareAdapter returning invalid cache
  • Loading branch information
nicolas-grekas committed Oct 12, 2019
2 parents a8a4aad + 946f0a1 commit 1201085
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
Expand Up @@ -156,7 +156,12 @@ public function hasItem($key)
if (!$this->pool->hasItem($key)) {
return false;
}
if (!$itemTags = $this->pool->getItem(static::TAGS_PREFIX.$key)->get()) {

if (!($itemTags = $this->pool->getItem(static::TAGS_PREFIX.$key))->isHit()) {
return false;
}

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

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

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

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

if (!$tagKeys) {
$tagVersions = $this->getTagVersions($itemTags);
Expand Down
78 changes: 78 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php
Expand Up @@ -194,6 +194,84 @@ 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());
}

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

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

$anotherPool = $this->createCachePool();

$adapter = new FilesystemAdapter();
$adapter->deleteItem($itemKey); //simulate losing item but keeping tags

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

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

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

$anotherPool = $this->createCachePool();

$adapter = new FilesystemAdapter();
$adapter->deleteItem($itemKey); //simulate losing item but keeping tags

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

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

0 comments on commit 1201085

Please sign in to comment.