From 2348d64b86fba16ca631f7fdbdf41ecf02534300 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 24 Aug 2017 08:50:28 +0200 Subject: [PATCH] [Cache] Fix >30 days expirations with Memcached --- .../Cache/Tests/Adapter/AdapterTestCase.php | 20 +++++++++++++++++++ .../Tests/Adapter/MemcachedAdapterTest.php | 1 - .../Component/Cache/Traits/MemcachedTrait.php | 4 ++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php b/src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php index c3cbd3bef7e54..faecda830cd7c 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php @@ -45,6 +45,26 @@ public function testDefaultLifeTime() $this->assertFalse($item->isHit()); } + public function testExpiration() + { + if (isset($this->skippedTests[__FUNCTION__])) { + $this->markTestSkipped($this->skippedTests[__FUNCTION__]); + } + + $cache = $this->createCachePool(); + $cache->save($cache->getItem('k1')->set('v1')->expiresAfter(2)); + $cache->save($cache->getItem('k2')->set('v2')->expiresAfter(366 * 86400)); + + sleep(3); + $item = $cache->getItem('k1'); + $this->assertFalse($item->isHit()); + $this->assertNull($item->get(), "Item's value must be null when isHit() is false."); + + $item = $cache->getItem('k2'); + $this->assertTrue($item->isHit()); + $this->assertSame('v2', $item->get()); + } + public function testNotUnserializable() { if (isset($this->skippedTests[__FUNCTION__])) { diff --git a/src/Symfony/Component/Cache/Tests/Adapter/MemcachedAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/MemcachedAdapterTest.php index 82b41c3b4d870..00e129398ed9a 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/MemcachedAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/MemcachedAdapterTest.php @@ -17,7 +17,6 @@ class MemcachedAdapterTest extends AdapterTestCase { protected $skippedTests = array( - 'testExpiration' => 'Testing expiration slows down the test suite', 'testHasItemReturnsFalseWhenDeferredItemIsExpired' => 'Testing expiration slows down the test suite', 'testDefaultLifeTime' => 'Testing expiration slows down the test suite', ); diff --git a/src/Symfony/Component/Cache/Traits/MemcachedTrait.php b/src/Symfony/Component/Cache/Traits/MemcachedTrait.php index c2832946f98c2..9dc44355c042a 100644 --- a/src/Symfony/Component/Cache/Traits/MemcachedTrait.php +++ b/src/Symfony/Component/Cache/Traits/MemcachedTrait.php @@ -187,6 +187,10 @@ public static function createConnection($servers, array $options = array()) */ protected function doSave(array $values, $lifetime) { + if ($lifetime && $lifetime > 30 * 86400) { + $lifetime += time(); + } + return $this->checkResultCode($this->client->setMulti($values, $lifetime)); }