Skip to content

Commit 2348d64

Browse files
[Cache] Fix >30 days expirations with Memcached
1 parent 420f089 commit 2348d64

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ public function testDefaultLifeTime()
4545
$this->assertFalse($item->isHit());
4646
}
4747

48+
public function testExpiration()
49+
{
50+
if (isset($this->skippedTests[__FUNCTION__])) {
51+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
52+
}
53+
54+
$cache = $this->createCachePool();
55+
$cache->save($cache->getItem('k1')->set('v1')->expiresAfter(2));
56+
$cache->save($cache->getItem('k2')->set('v2')->expiresAfter(366 * 86400));
57+
58+
sleep(3);
59+
$item = $cache->getItem('k1');
60+
$this->assertFalse($item->isHit());
61+
$this->assertNull($item->get(), "Item's value must be null when isHit() is false.");
62+
63+
$item = $cache->getItem('k2');
64+
$this->assertTrue($item->isHit());
65+
$this->assertSame('v2', $item->get());
66+
}
67+
4868
public function testNotUnserializable()
4969
{
5070
if (isset($this->skippedTests[__FUNCTION__])) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
class MemcachedAdapterTest extends AdapterTestCase
1818
{
1919
protected $skippedTests = array(
20-
'testExpiration' => 'Testing expiration slows down the test suite',
2120
'testHasItemReturnsFalseWhenDeferredItemIsExpired' => 'Testing expiration slows down the test suite',
2221
'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
2322
);

src/Symfony/Component/Cache/Traits/MemcachedTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ public static function createConnection($servers, array $options = array())
187187
*/
188188
protected function doSave(array $values, $lifetime)
189189
{
190+
if ($lifetime && $lifetime > 30 * 86400) {
191+
$lifetime += time();
192+
}
193+
190194
return $this->checkResultCode($this->client->setMulti($values, $lifetime));
191195
}
192196

0 commit comments

Comments
 (0)