From 5f612b78336547b921237da6018ba80db303d188 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 13 Jan 2011 20:27:10 -0500 Subject: [PATCH] Making memcache cache adapter, set the expiry time of any value greater than 30 days to never expire. This works around the 30 day limitation of the PHP extension. Test case added. Fixes #1451 --- cake/libs/cache/memcache.php | 6 +++-- cake/tests/cases/libs/cache/memcache.test.php | 24 ++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/cake/libs/cache/memcache.php b/cake/libs/cache/memcache.php index 32df6f0ab13..c9616ea7a8f 100644 --- a/cake/libs/cache/memcache.php +++ b/cake/libs/cache/memcache.php @@ -119,8 +119,7 @@ function _parseServerString($server) { /** * Write data for key into cache. When using memcache as your cache engine * remember that the Memcache pecl extension does not support cache expiry times greater - * than 30 days in the future. If you wish to create cache entries that do not expire, set the duration - * to `0` in your cache configuration. + * than 30 days in the future. Any duration greater than 30 days will be treated as never expiring. * * @param string $key Identifier for the data * @param mixed $value Data to be cached @@ -130,6 +129,9 @@ function _parseServerString($server) { * @access public */ function write($key, &$value, $duration) { + if ($duration > 30 * DAY) { + $duration = 0; + } return $this->__Memcache->set($key, $value, $this->settings['compress'], $duration); } diff --git a/cake/tests/cases/libs/cache/memcache.test.php b/cake/tests/cases/libs/cache/memcache.test.php index 9c566c1a1f5..8f7715c3cc8 100644 --- a/cake/tests/cases/libs/cache/memcache.test.php +++ b/cake/tests/cases/libs/cache/memcache.test.php @@ -33,8 +33,14 @@ class TestMemcacheEngine extends MemcacheEngine { function parseServerString($server) { return $this->_parseServerString($server); } + + function setMemcache(&$memcache) { + $this->__Memcache = $memcache; + } } +Mock::generate('Memcache', 'MemcacheMockMemcache'); + /** * MemcacheEngineTest class * @@ -361,7 +367,23 @@ function testZeroDuration() { $this->assertTrue($result, 'Could not write with duration 0'); $result = Cache::read('test_key', 'memcache'); $this->assertEqual($result, 'written!'); - + } + +/** + * test that durations greater than 30 days never expire + * + * @return void + */ + function testLongDurationEqualToZero() { + $memcache =& new TestMemcacheEngine(); + $memcache->settings['compress'] = false; + + $mock = new MemcacheMockMemcache(); + $memcache->setMemcache($mock); + $mock->expectAt(0, 'set', array('key', 'value', false, 0)); + + $value = 'value'; + $memcache->write('key', $value, 50 * DAY); } }