8000 [Cache] Fix default lifetime being ignored by nicolas-grekas · Pull Request #19442 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Fix default lifetime being ignored #19442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Cache] Fix default lifetime being ignored
  • Loading branch information
nicolas-grekas committed Jul 27, 2016
commit 35ba478680119cc99b1c8ccef55525949db4b97e
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function ($deferred, $namespace, &$expiredIds) {

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe max(0, $item->defaultLifetime) would be more readable? Maybe it's not a good idea because it implies a function call?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's save a function call yes

} elseif ($item->expiry > $now) {
$byLifetime[$item->expiry - $now][$namespace.$key] = $item->value;
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ public function save(CacheItemInterface $item)
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;
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ private function doSave(CacheItemInterface $item, $method)
}
$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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
40 changes: 40 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
7 changes: 3 additions & 4 deletions src/Symfony/Component/Cache/Tests/Adapter/ApcuAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -28,7 +27,7 @@ public function createCachePool()
$this->markTestSkipped('Fails transiently on Windows.');
}

return new ApcuAdapter(str_replace('\\', '.', __CLASS__));
return new ApcuAdapter(str_replace('\\', '.', __CLASS__), $defaultLifetime);
}

public function testUnserializable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,15 +19,15 @@
/**
* @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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
*/
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,16 +19,16 @@
/**
* @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);
}

/**
Expand Down
0