8000 bug #36457 [Cache] CacheItem with tag is never a hit after expired (a… · symfony/symfony@95becc4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 95becc4

Browse files
bug #36457 [Cache] CacheItem with tag is never a hit after expired (alexander-schranz, nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [Cache] CacheItem with tag is never a hit after expired | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes/no | New feature? | no | Deprecations? | no | Tickets | Fix #36458 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> It seems like a tag cacheItem is never a hit again. Not sure how fix this but the cache component is really hard to debug 🙈 . It need to be somewhere generally as all TagAware caches are effected: ``` 1) Symfony\Component\Cache\Tests\Adapter\FilesystemTagAwareAdapterTest::testRefreshAfterExpires Failed asserting that false is true. /home/travis/build/symfony/symfony/src/Symfony/Component/Cache/Tests/Traits/TagAwareTestTrait.php:194 2) Symfony\Component\Cache\Tests\Adapter\PredisTagAwareClusterAdapterTest::testRefreshAfterExpires Failed asserting that true is false. /home/travis/build/symfony/symfony/src/Symfony/Component/Cache/Tests/Traits/TagAwareTestTrait.php:183 3) Symfony\Component\Cache\Tests\Adapter\RedisTagAwareAdapterTest::testRefreshAfterExpires Failed asserting that true is false. /home/travis/build/symfony/symfony/src/Symfony/Component/Cache/Tests/Traits/TagAwareTestTrait.php:183 4) Symfony\Component\Cache\Tests\Adapter\RedisTagAwareClusterAdapterTest::testRefreshAfterExpires Failed asserting that true is false. /home/travis/build/symfony/symfony/src/Symfony/Component/Cache/Tests/Traits/TagAwareTestTrait.php:183 ``` Commits ------- d082eca Add reproducer to for hit after update expire cacheItem f815b01 [Cache] fix FilesystemTagAwareAdapter failing when a tag link preexists
2 parents e0e3cf6 + d082eca commit 95becc4

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected function doSave(array $values, ?int $lifetime, array $addTagData = [],
107107

108108
$file = $this->getFile($id);
109109

110-
if (!@symlink($file, $this->getFile($id, true, $tagFolder))) {
110+
if (!@symlink($file, $tagLink = $this->getFile($id, true, $tagFolder)) && !is_link($tagLink)) {
111111
@unlink($file);
112112
$failed[] = $id;
113113
}

src/Symfony/Component/Cache/Tests/Traits/TagAwareTestTrait.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,42 @@ public function testGetMetadata()
155155
$i = $pool->getItem('k');
156156
$this->assertSame(['foo' => 'foo'], $i->getMetadata()[CacheItem::METADATA_TAGS]);
157157
}
158+
159+
public function testRefreshAfterExpires()
160+
{
161+
$pool = $this->createCachePool();
162+
$pool->clear();
163+
164+
$cacheItem = $pool->getItem('test');
165+
166+
$this->assertFalse($cacheItem->isHit());
167+
168+
// write cache with expires
169+
$cacheItem->set('test');
170+
$cacheItem->tag('1234');
171+
$cacheItem->expiresAfter(1);
172+
173+
$pool->save($cacheItem);
174+
175+
$cacheItem = $pool->getItem('test');
176+
$this->assertTrue($cacheItem->isHit());
177+
178+
// wait until expired
179+
sleep(2);
180+
181+
// item should not longer be a hit
182+
$cacheItem = $pool->getItem('test');
183+
$this->assertFalse($cacheItem->isHit());
184+
185+
// update expired item
186+
$cacheItem->set('test');
187+
$cacheItem->tag('1234');
188+
$cacheItem->expiresAfter(1);
189+
190+
$pool->save($cacheItem);
191+
192+
// item should be again a hit
193+
$cacheItem = $pool->getItem('test');
194+
$this->assertTrue($cacheItem->isHit());
195+
}
158196
}

0 commit comments

Comments
 (0)
0