Skip to content

Commit

Permalink
Making memcache cache adapter, set the expiry time of any value great…
Browse files Browse the repository at this point in the history
…er than 30 days to never expire. This works around the 30 day limitation of the PHP extension. Test case added. Fixes #1451
  • Loading branch information
markstory committed Jan 14, 2011
1 parent 0f4c905 commit 5f612b7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 4 additions & 2 deletions cake/libs/cache/memcache.php
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
24 changes: 23 additions & 1 deletion cake/tests/cases/libs/cache/memcache.test.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}

}

0 comments on commit 5f612b7

Please sign in to comment.