Skip to content

Commit

Permalink
Merge pull request #134 from boesing/bugfix/deferred-item-expiry
Browse files Browse the repository at this point in the history
Bugfix: Expired cache items must not be queued for later persistence
  • Loading branch information
boesing committed Jul 27, 2021
2 parents 3cea23c + 05a1122 commit 10b2259
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Psr/CacheItemPool/CacheItemPoolDecorator.php
Expand Up @@ -266,6 +266,11 @@ public function saveDeferred(CacheItemInterface $item)
throw new InvalidArgumentException('$item must be an instance of ' . CacheItem::class);
}

$ttl = $item->getTtl();
if ($ttl !== null && $ttl <= 0) {
return false;
}

// deferred items should always be a 'hit' until they expire
$item->setIsHit(true);
$this->deferred[$item->getKey()] = $item;
Expand Down
34 changes: 34 additions & 0 deletions test/Psr/CacheItemPool/CacheItemPoolDecoratorTest.php
Expand Up @@ -2,13 +2,16 @@

namespace LaminasTest\Cache\Psr\CacheItemPool;

use DateTimeImmutable;
use Laminas\Cache\Exception;
use Laminas\Cache\Psr\CacheItemPool\CacheException;
use Laminas\Cache\Psr\CacheItemPool\CacheItem;
use Laminas\Cache\Psr\CacheItemPool\CacheItemPoolDecorator;
use Laminas\Cache\Psr\CacheItemPool\InvalidArgumentException;
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
use Laminas\Cache\Storage\Capabilities;
use Laminas\Cache\Storage\StorageInterface;
use LaminasTest\Cache\Psr\CacheItemPool\TestAsset\FlushableStorageAdapterInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
Expand Down Expand Up @@ -560,6 +563,37 @@ public function testWillVerifyKeyExistenceByUsingHasItemsWhenDeletionWasNotSucce
self::assertEquals($sucsessful, $cache->deleteItems(['foo']));
}

public function testWontSaveAlreadyExpiredCacheItemAsDeferredItem(): void
{
$adapter = $this->createMock(FlushableStorageAdapterInterface::class);
$adapter
->expects(self::exactly(2))
->method('getCapabilities')
->willReturn(new Capabilities($adapter, new stdClass(), $this->defaultCapabilities));

$adapter
->expects(self::never())
->method('removeItems');
$adapter
->expects(self::never())
->method('setItem');

$adapter
->expects(self::once())
->method('hasItem')
->with('foo')
->willReturn(false);

$item = new CacheItem('foo', 'bar', false);
$item->expiresAt(DateTimeImmutable::createFromFormat('U', time() - 1));

$cache = new CacheItemPoolDecorator($adapter);
$cache->saveDeferred($item);

self::assertFalse($item->isHit());
self::assertFalse($cache->hasItem($item->getKey()));
}

public function deletionVerificationProvider()
{
return [
Expand Down
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace LaminasTest\Cache\Psr\CacheItemPool\TestAsset;

use Laminas\Cache\Storage\FlushableInterface;
use Laminas\Cache\Storage\StorageInterface;

interface FlushableStorageAdapterInterface extends StorageInterface, FlushableInterface
{

}

0 comments on commit 10b2259

Please sign in to comment.