Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 03f2adc

Browse files
bug #33754 [Cache] fix known tag versions ttl check (SwenVanZanten)
This PR was merged into the 3.4 branch. Discussion ---------- [Cache] fix known tag versions ttl check | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | none | License | MIT The introduced changes from PR #27007 came with a knownTagVersionTtl property defaulted to 0.15 microseconds. The if statement defined on line 353 checks if the ttl is higher then the already known tag versions. This results in the opposite of the wanted behavior. Instead of waiting for the ttl to expire the if statement is only true if the known versions are lower than the version ttl. So if this Adapter stays in memory the tag versions are never checked again if the ttl is already lower then the passed time. The unit test I've added tests once if the version is changed and another time it doesn't get checked cause the ttl hasn't been expired yet. Commits ------- 205abf3 [Cache] fix known tag versions ttl check
2 parents fda5b20 + 205abf3 commit 03f2adc

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ private function getTagVersions(array $tagsByKey, array &$invalidatedTags = [])
350350
continue;
351351
}
352352
$version -= $this->knownTagVersions[$tag][1];
353-
if ((0 !== $version && 1 !== $version) || $this->knownTagVersionsTtl > $now - $this->knownTagVersions[$tag][0]) {
353+
if ((0 !== $version && 1 !== $version) || $now - $this->knownTagVersions[$tag][0] >= $this->knownTagVersionsTtl) {
354354
// reuse previously fetched tag versions up to the ttl, unless we are storing items or a potential miss arises
355355
$fetchTagVersions = true;
356356
} else {

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Cache\Tests\Adapter;
1313

1414
use PHPUnit\Framework\MockObject\MockObject;
15+
use Psr\Cache\CacheItemInterface;
1516
use Symfony\Component\Cache\Adapter\AdapterInterface;
1617
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
1718
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
@@ -160,6 +161,39 @@ public function testPrune()
160161
$this->assertFalse($cache->prune());
161162
}
162163

164+
public function testKnownTagVersionsTtl()
165+
{
166+
$itemsPool = new FilesystemAdapter('', 10);
167+
$tagsPool = $this
168+
->getMockBuilder(AdapterInterface::class)
169+
->getMock();
170+
171+
$pool = new TagAwareAdapter($itemsPool, $tagsPool, 10);
172+
173+
$item = $pool->getItem('foo');
174+
$item->tag(['baz']);
175+
$item->expiresAfter(100);
176+
177+
$tag = $this->getMockBuilder(CacheItemInterface::class)->getMock();
178+
$tag->expects(self::exactly(2))->method('get')->willReturn(10);
179+
180+
$tagsPool->expects(self::exactly(2))->method('getItems')->willReturn([
181+
'baz'.TagAwareAdapter::TAGS_PREFIX => $tag,
182+
]);
183+
184+
$pool->save($item);
185+
$this->assertTrue($pool->getItem('foo')->isHit());
186+
$this->assertTrue($pool->getItem('foo')->isHit());
187+
188+
sleep(20);
189+
190+
$this->assertTrue($pool->getItem('foo')->isHit());
191+
192+
sleep(5);
193+
194+
$this->assertTrue($pool->getItem('foo')->isHit());
195+
}
196+
163197
/**
164198
* @return MockObject|PruneableCacheInterface
165199
*/

0 commit comments

Comments
 (0)