From f815b011c3d02c486e29b682886b6eb8583bd8c8 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sat, 18 Apr 2020 16:27:57 +0200 Subject: [PATCH 1/2] [Cache] fix FilesystemTagAwareAdapter failing when a tag link preexists --- .../Component/Cache/Adapter/FilesystemTagAwareAdapter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Cache/Adapter/FilesystemTagAwareAdapter.php b/src/Symfony/Component/Cache/Adapter/FilesystemTagAwareAdapter.php index d9a1ad39ac6e..2ce9eeb977fc 100644 --- a/src/Symfony/Component/Cache/Adapter/FilesystemTagAwareAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/FilesystemTagAwareAdapter.php @@ -107,7 +107,7 @@ protected function doSave(array $values, ?int $lifetime, array $addTagData = [], $file = $this->getFile($id); - if (!@symlink($file, $this->getFile($id, true, $tagFolder))) { + if (!@symlink($file, $tagLink = $this->getFile($id, true, $tagFolder)) && !is_link($tagLink)) { @unlink($file); $failed[] = $id; } From d082eca7ddde683fd8e0be56f13f4b865dc6294d Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Wed, 15 Apr 2020 14:44:16 +0200 Subject: [PATCH 2/2] Add reproducer to for hit after update expire cacheItem --- .../Cache/Tests/Traits/TagAwareTestTrait.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/Symfony/Component/Cache/Tests/Traits/TagAwareTestTrait.php b/src/Symfony/Component/Cache/Tests/Traits/TagAwareTestTrait.php index 7f1544af5b41..8b687b5a7564 100644 --- a/src/Symfony/Component/Cache/Tests/Traits/TagAwareTestTrait.php +++ b/src/Symfony/Component/Cache/Tests/Traits/TagAwareTestTrait.php @@ -155,4 +155,42 @@ public function testGetMetadata() $i = $pool->getItem('k'); $this->assertSame(['foo' => 'foo'], $i->getMetadata()[CacheItem::METADATA_TAGS]); } + + public function testRefreshAfterExpires() + { + $pool = $this->createCachePool(); + $pool->clear(); + + $cacheItem = $pool->getItem('test'); + + $this->assertFalse($cacheItem->isHit()); + + // write cache with expires + $cacheItem->set('test'); + $cacheItem->tag('1234'); + $cacheItem->expiresAfter(1); + + $pool->save($cacheItem); + + $cacheItem = $pool->getItem('test'); + $this->assertTrue($cacheItem->isHit()); + + // wait until expired + sleep(2); + + // item should not longer be a hit + $cacheItem = $pool->getItem('test'); + $this->assertFalse($cacheItem->isHit()); + + // update expired item + $cacheItem->set('test'); + $cacheItem->tag('1234'); + $cacheItem->expiresAfter(1); + + $pool->save($cacheItem); + + // item should be again a hit + $cacheItem = $pool->getItem('test'); + $this->assertTrue($cacheItem->isHit()); + } }