bug #37671 [Cache] fix saving no-expiry items with ArrayAdapter (philipp-kolesnikov)

This PR was merged into the 3.4 branch.

Discussion
----------

[Cache] fix saving no-expiry items with ArrayAdapter

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #37667
| License       | MIT
| Doc PR        |

0 is a special value that means "infinity".

Commits
-------

bdec105a72 [Cache] Fix #37667
This commit is contained in:
Fabien Potencier 2020-07-31 09:57:22 +02:00
commit b61fa4480b
2 changed files with 24 additions and 0 deletions

View File

@ -113,6 +113,10 @@ class ArrayAdapter implements AdapterInterface, LoggerAwareInterface, Resettable
$value = $item["\0*\0value"];
$expiry = $item["\0*\0expiry"];
if (0 === $expiry) {
$expiry = PHP_INT_MAX;
}
if (null !== $expiry && $expiry <= time()) {
$this->deleteItem($key);

View File

@ -14,6 +14,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\ArrayAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
@ -255,6 +256,25 @@ class TagAwareAdapterTest extends AdapterTestCase
$this->assertFalse($anotherPool->hasItem($itemKey));
}
public function testInvalidateTagsWithArrayAdapter()
{
$adapter = new TagAwareAdapter(new ArrayAdapter());
$item = $adapter->getItem('foo');
$this->assertFalse($item->isHit());
$item->tag('bar');
$item->expiresAfter(100);
$adapter->save($item);
$this->assertTrue($adapter->getItem('foo')->isHit());
$adapter->invalidateTags(['bar']);
$this->assertFalse($adapter->getItem('foo')->isHit());
}
public function testGetItemReturnsCacheMissWhenPoolDoesNotHaveItemAndOnlyHasTags()
{
$pool = $this->createCachePool();