10000 bug #19442 [Cache] Fix default lifetime being ignored (nicolas-grekas) · symfony/symfony@aaff0d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit aaff0d1

Browse files
bug #19442 [Cache] Fix default lifetime being ignored (nicolas-grekas)
This PR was merged into the 3.1 branch. Discussion ---------- [Cache] Fix default lifetime being ignored | Q | A | ------------- | --- | Branch? | 3.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony/cache#1 | License | MIT | Doc PR | - Courtesy of @ecanovas Commits ------- 35ba478 [Cache] Fix default lifetime being ignored
2 parents 26dfa2f + 35ba478 commit aaff0d1

12 files changed

+70
-31
lines changed

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function ($deferred, $namespace, &$expiredIds) {
5353

5454
foreach ($deferred as $key => $item) {
5555
if (null === $item->expiry) {
56-
$byLifetime[0][$namespace.$key] = $item->value;
56+
$byLifetime[0 < $item->defaultLifetime ? $item->defaultLifetime : 0][$namespace.$key] = $item->value;
5757
} elseif ($item->expiry > $now) {
5858
$byLifetime[$item->expiry - $now][$namespace.$key] = $item->value;
5959
} else {

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ public function save(CacheItemInterface $item)
151151
return false;
152152
}
153153
}
154+
if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
155+
$expiry = time() + $item["\0*\0defaultLifetime"];
156+
}
154157

155158
$this->values[$key] = $value;
156159
$this->expiries[$key] = null !== $expiry ? $expiry : PHP_INT_MAX;

src/Symfony/Component/Cache/Adapter/ProxyAdapter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ private function doSave(CacheItemInterface $item, $method)
144144
}
145145
$item = (array) $item;
146146
$expiry = $item["\0*\0expiry"];
147+
if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
148+
$expiry = time() + $item["\0*\0defaultLifetime"];
149+
}
147150
$innerItem = $item["\0*\0poolHash"] === $this->poolHash ? $item["\0*\0innerItem"] : $this->pool->getItem($this->namespace.$item["\0*\0key"]);
148151
$innerItem->set($item["\0*\0value"]);
149152
$innerItem->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null);

src/Symfony/Component/Cache/Tests/Adapter/AbstractRedisAdapterTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,19 @@
1111

1212
namespace Symfony\Component\Cache\Tests\Adapter;
1313

14-
use Cache\IntegrationTests\CachePoolTest;
1514
use Symfony\Component\Cache\Adapter\RedisAdapter;
1615

17-
abstract class AbstractRedisAdapterTest extends CachePoolTest
16+
abstract class AbstractRedisAdapterTest extends AdapterTestCase
1817
{
1918
protected static $redis;
2019

21-
public function createCachePool()
20+
public function createCachePool($defaultLifetime = 0)
2221
{
2322
if (defined('HHVM_VERSION')) {
2423
$this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM';
2524
}
2625

27-
return new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__));
26+
return new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
2827
}
2928

3029
public static function setupBeforeClass()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests\Adapter;
13+
14+
use Cache\IntegrationTests\CachePoolTest;
15+
16+
abstract class AdapterTestCase extends CachePoolTest
17+
{
18+
public function testDefaultLifeTime()
19+
{
20+
if (isset($this->skippedTests[__FUNCTION__])) {
21+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
22+
23+
return;
24+
}
25+
26+
$this->cache = $this->createCachePool(2);
27+
28+
$item = $this->cache->getItem('key.dlt');
29+
$item->set('value');
30+
$this->cache->save($item);
31+
sleep(1);
32+
33+
$item = $this->cache->getItem('key.dlt');
34+
$this->assertTrue($item->isHit());
35+
36+
sleep(2);
37+
$item = $this->cache->getItem('key.dlt');
38+
$this->assertFalse($item->isHit());
39+
}
40+
}

src/Symfony/Component/Cache/Tests/Adapter/ApcuAdapterTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111

1212
namespace Symfony\Component\Cache\Tests\Adapter;
1313

14-
use Cache\IntegrationTests\CachePoolTest;
1514
use Symfony\Component\Cache\Adapter\ApcuAdapter;
1615

17-
class ApcuAdapterTest extends CachePoolTest
16+
class ApcuAdapterTest extends AdapterTestCase
1817
{
19-
public function createCachePool()
18+
public function createCachePool($defaultLifetime = 0)
2019
{
2120
if (defined('HHVM_VERSION')) {
2221
$this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM';
@@ -28,7 +27,7 @@ public function createCachePool()
2827
$this->markTestSkipped('Fails transiently on Windows.');
2928
}
3029

31-
return new ApcuAdapter(str_replace('\\', '.', __CLASS__));
30+
return new ApcuAdapter(str_replace('\\', '.', __CLASS__), $defaultLifetime);
3231
}
3332

3433
public function testUnserializable()

src/Symfony/Component/Cache/Tests/Adapter/ArrayAdapterTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@
1111

1212
namespace Symfony\Component\Cache\Tests\Adapter;
1313

14-
use Cache\IntegrationTests\CachePoolTest;
1514
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1615

1716
/**
1817
* @group time-sensitive
1918
*/
20-
class ArrayAdapterTest extends CachePoolTest
19+
class ArrayAdapterTest extends AdapterTestCase
2120
{
2221
protected $skippedTests = array(
2322
'testDeferredSaveWithoutCommit' => 'Assumes a shared cache which ArrayAdapter is not.',
2423
'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayAdapter is not.',
2524
);
2625

27-
public function createCachePool()
26+
public function createCachePool($defaultLifetime = 0)
2827
{
29-
return new ArrayAdapter();
28+
return new ArrayAdapter($defaultLifetime);
3029
}
3130
}

src/Symfony/Component/Cache/Tests/Adapter/ChainAdapterTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Cache\Tests\Adapter;
1313

14-
use Cache\I F438 ntegrationTests\CachePoolTest;
1514
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
1615
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1716
use Symfony\Component\Cache\Adapter\ChainAdapter;
@@ -20,15 +19,15 @@
2019
/**
2120
* @author Kévin Dunglas <dunglas@gmail.com>
2221
*/
23-
class ChainAdapterTest extends CachePoolTest
22+
class ChainAdapterTest extends AdapterTestCase
2423
{
25-
public function createCachePool()
24+
public function createCachePool($defaultLifetime = 0)
2625
{
2726
if (defined('HHVM_VERSION')) {
2827
$this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM';
2928
}
3029

31-
return new ChainAdapter(array(new ArrayAdapter(), new ExternalAdapter(), new FilesystemAdapter()));
30+
return new ChainAdapter(array(new ArrayAdapter($defaultLifetime), new ExternalAdapter(), new FilesystemAdapter('', $defaultLifetime)), $defaultLifetime);
3231
}
3332

3433
/**

src/Symfony/Component/Cache/Tests/Adapter/DoctrineAdapterTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,21 @@
1111

1212
namespace Symfony\Component\Cache\Tests\Adapter;
1313

14-
use Cache\IntegrationTests\CachePoolTest;
1514
use Doctrine\Common\Cache\ArrayCache;
1615
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
1716

1817
/**
1918
* @group time-sensitive
2019
*/
21-
class DoctrineAdapterTest extends CachePoolTest
20+
class DoctrineAdapterTest extends AdapterTestCase
2221
{
2322
protected $skippedTests = array(
2423
'testDeferredSaveWithoutCommit' => 'Assumes a shared cache which ArrayCache is not.',
2524
'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayCache is not.',
2625
);
2726

28-
public function createCachePool()
27+
public function createCachePool($defaultLifetime = 0)
2928
{
30-
return new DoctrineAdapter(new ArrayCache());
29+
return new DoctrineAdapter(new ArrayCache(), '', $defaultLifetime);
3130
}
3231
}

src/Symfony/Component/Cache/Tests/Adapter/FilesystemAdapterTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@
1111

1212
namespace Symfony\Component\Cache\Tests\Adapter;
1313

14-
use Cache\IntegrationTests\CachePoolTest;
1514
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
1615

1716
/**
1817
* @group time-sensitive
1918
*/
20-
class FilesystemAdapterTest extends CachePoolTest
19+
class FilesystemAdapterTest extends AdapterTestCase
2120
{
22-
public function createCachePool()
21+
public function createCachePool($defaultLifetime = 0)
2322
{
2423
if (defined('HHVM_VERSION')) {
2524
$this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM';
2625
}
2726

28-
return new FilesystemAdapter('sf-cache');
27+
return new FilesystemAdapter('', $defaultLifetime);
2928
}
3029

3130
public static function tearDownAfterClass()

src/Symfony/Component/Cache/Tests/Adapter/NamespacedProxyAdapterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
*/
2020
class NamespacedProxyAdapterTest extends ProxyAdapterTest
2121
{
22-
public function createCachePool()
22+
public function createCachePool($defaultLifetime = 0)
2323
{
24-
return new ProxyAdapter(new ArrayAdapter(), 'foo');
24+
return new ProxyAdapter(new ArrayAdapter(), 'foo', $defaultLifetime);
2525
}
2626
}

src/Symfony/Component/Cache/Tests/Adapter/ProxyAdapterTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Cache\Tests\Adapter;
1313

14-
use Cache\IntegrationTests\CachePoolTest;
1514
use Psr\Cache\CacheItemInterface;
1615
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1716
use Symfony\Component\Cache\Adapter\ProxyAdapter;
@@ -20,16 +19,16 @@
2019
/**
2120
* @group time-sensitive
2221
*/
23-
class ProxyAdapterTest extends CachePoolTest
22+
class ProxyAdapterTest extends AdapterTestCase
2423
{
2524
protected $skippedTests = array(
2625
'testDeferredSaveWithoutCommit' => 'Assumes a shared cache which ArrayAdapter is not.',
2726
'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayAdapter is not.',
2827
);
2928

30-
public function createCachePool()
29+
public function createCachePool($defaultLifetime = 0)
3130
{
32-
return new ProxyAdapter(new ArrayAdapter());
31+
return new ProxyAdapter(new ArrayAdapter(), '', $defaultLifetime);
3332
}
3433

3534
/**

0 commit comments

Comments
 (0)
0