Skip to content

Commit

Permalink
[Cache] fix known tag versions ttl check
Browse files Browse the repository at this point in the history
  • Loading branch information
SwenVanZanten committed Sep 29, 2019
1 parent fda5b20 commit 205abf3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
Expand Up @@ -350,7 +350,7 @@ private function getTagVersions(array $tagsByKey, array &$invalidatedTags = [])
continue;
}
$version -= $this->knownTagVersions[$tag][1];
if ((0 !== $version && 1 !== $version) || $this->knownTagVersionsTtl > $now - $this->knownTagVersions[$tag][0]) {
if ((0 !== $version && 1 !== $version) || $now - $this->knownTagVersions[$tag][0] >= $this->knownTagVersionsTtl) {
// reuse previously fetched tag versions up to the ttl, unless we are storing items or a potential miss arises
$fetchTagVersions = true;
} else {
Expand Down
34 changes: 34 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Cache\Tests\Adapter;

use PHPUnit\Framework\MockObject\MockObject;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
Expand Down Expand Up @@ -160,6 +161,39 @@ public function testPrune()
$this->assertFalse($cache->prune());
}

public function testKnownTagVersionsTtl()
{
$itemsPool = new FilesystemAdapter('', 10);
$tagsPool = $this
->getMockBuilder(AdapterInterface::class)
->getMock();

$pool = new TagAwareAdapter($itemsPool, $tagsPool, 10);

$item = $pool->getItem('foo');
$item->tag(['baz']);
$item->expiresAfter(100);

$tag = $this->getMockBuilder(CacheItemInterface::class)->getMock();
$tag->expects(self::exactly(2))->method('get')->willReturn(10);

$tagsPool->expects(self::exactly(2))->method('getItems')->willReturn([
'baz'.TagAwareAdapter::TAGS_PREFIX => $tag,
]);

$pool->save($item);
$this->assertTrue($pool->getItem('foo')->isHit());
$this->assertTrue($pool->getItem('foo')->isHit());

sleep(20);

$this->assertTrue($pool->getItem('foo')->isHit());

sleep(5);

$this->assertTrue($pool->getItem('foo')->isHit());
}

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

0 comments on commit 205abf3

Please sign in to comment.