[Cache] Fix default lifetime being ignored

This commit is contained in:
Nicolas Grekas 2016-07-27 10:15:52 +02:00
parent 54043a084f
commit 35ba478680
12 changed files with 70 additions and 31 deletions

View File

@ -53,7 +53,7 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
foreach ($deferred as $key => $item) {
if (null === $item->expiry) {
$byLifetime[0][$namespace.$key] = $item->value;
$byLifetime[0 < $item->defaultLifetime ? $item->defaultLifetime : 0][$namespace.$key] = $item->value;
} elseif ($item->expiry > $now) {
$byLifetime[$item->expiry - $now][$namespace.$key] = $item->value;
} else {

View File

@ -151,6 +151,9 @@ class ArrayAdapter implements AdapterInterface, LoggerAwareInterface
return false;
}
}
if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
$expiry = time() + $item["\0*\0defaultLifetime"];
}
$this->values[$key] = $value;
$this->expiries[$key] = null !== $expiry ? $expiry : PHP_INT_MAX;

View File

@ -144,6 +144,9 @@ class ProxyAdapter implements AdapterInterface
}
$item = (array) $item;
$expiry = $item["\0*\0expiry"];
if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
$expiry = time() + $item["\0*\0defaultLifetime"];
}
$innerItem = $item["\0*\0poolHash"] === $this->poolHash ? $item["\0*\0innerItem"] : $this->pool->getItem($this->namespace.$item["\0*\0key"]);
$innerItem->set($item["\0*\0value"]);
$innerItem->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null);

View File

@ -11,20 +11,19 @@
namespace Symfony\Component\Cache\Tests\Adapter;
use Cache\IntegrationTests\CachePoolTest;
use Symfony\Component\Cache\Adapter\RedisAdapter;
abstract class AbstractRedisAdapterTest extends CachePoolTest
abstract class AbstractRedisAdapterTest extends AdapterTestCase
{
protected static $redis;
public function createCachePool()
public function createCachePool($defaultLifetime = 0)
{
if (defined('HHVM_VERSION')) {
$this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM';
}
return new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__));
return new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
}
public static function setupBeforeClass()

View File

@ -0,0 +1,40 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Cache\Tests\Adapter;
use Cache\IntegrationTests\CachePoolTest;
abstract class AdapterTestCase extends CachePoolTest
{
public function testDefaultLifeTime()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
return;
}
$this->cache = $this->createCachePool(2);
$item = $this->cache->getItem('key.dlt');
$item->set('value');
$this->cache->save($item);
sleep(1);
$item = $this->cache->getItem('key.dlt');
$this->assertTrue($item->isHit());
sleep(2);
$item = $this->cache->getItem('key.dlt');
$this->assertFalse($item->isHit());
}
}

View File

@ -11,12 +11,11 @@
namespace Symfony\Component\Cache\Tests\Adapter;
use Cache\IntegrationTests\CachePoolTest;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
class ApcuAdapterTest extends CachePoolTest
class ApcuAdapterTest extends AdapterTestCase
{
public function createCachePool()
public function createCachePool($defaultLifetime = 0)
{
if (defined('HHVM_VERSION')) {
$this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM';
@ -28,7 +27,7 @@ class ApcuAdapterTest extends CachePoolTest
$this->markTestSkipped('Fails transiently on Windows.');
}
return new ApcuAdapter(str_replace('\\', '.', __CLASS__));
return new ApcuAdapter(str_replace('\\', '.', __CLASS__), $defaultLifetime);
}
public function testUnserializable()

View File

@ -11,21 +11,20 @@
namespace Symfony\Component\Cache\Tests\Adapter;
use Cache\IntegrationTests\CachePoolTest;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
/**
* @group time-sensitive
*/
class ArrayAdapterTest extends CachePoolTest
class ArrayAdapterTest extends AdapterTestCase
{
protected $skippedTests = array(
'testDeferredSaveWithoutCommit' => 'Assumes a shared cache which ArrayAdapter is not.',
'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayAdapter is not.',
);
public function createCachePool()
public function createCachePool($defaultLifetime = 0)
{
return new ArrayAdapter();
return new ArrayAdapter($defaultLifetime);
}
}

View File

@ -11,7 +11,6 @@
namespace Symfony\Component\Cache\Tests\Adapter;
use Cache\IntegrationTests\CachePoolTest;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\ChainAdapter;
@ -20,15 +19,15 @@ use Symfony\Component\Cache\Tests\Fixtures\ExternalAdapter;
/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class ChainAdapterTest extends CachePoolTest
class ChainAdapterTest extends AdapterTestCase
{
public function createCachePool()
public function createCachePool($defaultLifetime = 0)
{
if (defined('HHVM_VERSION')) {
$this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM';
}
return new ChainAdapter(array(new ArrayAdapter(), new ExternalAdapter(), new FilesystemAdapter()));
return new ChainAdapter(array(new ArrayAdapter($defaultLifetime), new ExternalAdapter(), new FilesystemAdapter('', $defaultLifetime)), $defaultLifetime);
}
/**

View File

@ -11,22 +11,21 @@
namespace Symfony\Component\Cache\Tests\Adapter;
use Cache\IntegrationTests\CachePoolTest;
use Doctrine\Common\Cache\ArrayCache;
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
/**
* @group time-sensitive
*/
class DoctrineAdapterTest extends CachePoolTest
class DoctrineAdapterTest extends AdapterTestCase
{
protected $skippedTests = array(
'testDeferredSaveWithoutCommit' => 'Assumes a shared cache which ArrayCache is not.',
'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayCache is not.',
);
public function createCachePool()
public function createCachePool($defaultLifetime = 0)
{
return new DoctrineAdapter(new ArrayCache());
return new DoctrineAdapter(new ArrayCache(), '', $defaultLifetime);
}
}

View File

@ -11,21 +11,20 @@
namespace Symfony\Component\Cache\Tests\Adapter;
use Cache\IntegrationTests\CachePoolTest;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
/**
* @group time-sensitive
*/
class FilesystemAdapterTest extends CachePoolTest
class FilesystemAdapterTest extends AdapterTestCase
{
public function createCachePool()
public function createCachePool($defaultLifetime = 0)
{
if (defined('HHVM_VERSION')) {
$this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM';
}
return new FilesystemAdapter('sf-cache');
return new FilesystemAdapter('', $defaultLifetime);
}
public static function tearDownAfterClass()

View File

@ -19,8 +19,8 @@ use Symfony\Component\Cache\Adapter\ProxyAdapter;
*/
class NamespacedProxyAdapterTest extends ProxyAdapterTest
{
public function createCachePool()
public function createCachePool($defaultLifetime = 0)
{
return new ProxyAdapter(new ArrayAdapter(), 'foo');
return new ProxyAdapter(new ArrayAdapter(), 'foo', $defaultLifetime);
}
}

View File

@ -11,7 +11,6 @@
namespace Symfony\Component\Cache\Tests\Adapter;
use Cache\IntegrationTests\CachePoolTest;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\ProxyAdapter;
@ -20,16 +19,16 @@ use Symfony\Component\Cache\CacheItem;
/**
* @group time-sensitive
*/
class ProxyAdapterTest extends CachePoolTest
class ProxyAdapterTest extends AdapterTestCase
{
protected $skippedTests = array(
'testDeferredSaveWithoutCommit' => 'Assumes a shared cache which ArrayAdapter is not.',
'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayAdapter is not.',
);
public function createCachePool()
public function createCachePool($defaultLifetime = 0)
{
return new ProxyAdapter(new ArrayAdapter());
return new ProxyAdapter(new ArrayAdapter(), '', $defaultLifetime);
}
/**