Skip to content

Commit

Permalink
bug #36457 [Cache] CacheItem with tag is never a hit after expired (a…
Browse files Browse the repository at this point in the history
…lexander-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
  • Loading branch information
nicolas-grekas committed Apr 19, 2020
2 parents e0e3cf6 + d082eca commit 95becc4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Expand Up @@ -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;
}
Expand Down
38 changes: 38 additions & 0 deletions src/Symfony/Component/Cache/Tests/Traits/TagAwareTestTrait.php
Expand Up @@ -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());
}
}

0 comments on commit 95becc4

Please sign in to comment.