NEW Allow setting default cache lifetime, and test that it's being used correctly on new caches

This commit is contained in:
Sean Harvey 2012-09-03 12:55:19 +12:00
parent dc9a21b875
commit b6a6c4670e
2 changed files with 34 additions and 10 deletions

15
cache/Cache.php vendored
View File

@ -76,6 +76,7 @@ class SS_Cache {
$cachedir = TEMP_FOLDER . DIRECTORY_SEPARATOR . 'cache'; $cachedir = TEMP_FOLDER . DIRECTORY_SEPARATOR . 'cache';
if (!is_dir($cachedir)) mkdir($cachedir); if (!is_dir($cachedir)) mkdir($cachedir);
self::$backends['default'] = array('File', array('cache_dir' => TEMP_FOLDER . DIRECTORY_SEPARATOR . 'cache')); self::$backends['default'] = array('File', array('cache_dir' => TEMP_FOLDER . DIRECTORY_SEPARATOR . 'cache'));
self::$cache_lifetime['default'] = array('lifetime' => 600, 'priority' => 1);
} }
} }
@ -110,6 +111,14 @@ class SS_Cache {
if ($priority >= $current) self::$backend_picks[$for] = array('name' => $name, 'priority' => $priority); if ($priority >= $current) self::$backend_picks[$for] = array('name' => $name, 'priority' => $priority);
} }
/**
* Return the cache lifetime for a particular named cache.
* @return array
*/
static function get_cache_lifetime($for) {
return (isset(self::$cache_lifetime[$for])) ? self::$cache_lifetime[$for] : false;
}
/** /**
* Set the cache lifetime for a particular named cache * Set the cache lifetime for a particular named cache
* *
@ -169,8 +178,10 @@ class SS_Cache {
static function factory($for, $frontend='Output', $frontendOptions=null) { static function factory($for, $frontend='Output', $frontendOptions=null) {
self::init(); self::init();
$backend_name = 'default'; $backend_priority = -1; $backend_name = 'default';
$cache_lifetime = 600; $lifetime_priority = -1; $backend_priority = -1;
$cache_lifetime = self::$cache_lifetime['default']['lifetime'];
$lifetime_priority = -1;
foreach (array('any', $for) as $name) { foreach (array('any', $for) as $name) {
if (isset(self::$backend_picks[$name]) && self::$backend_picks[$name]['priority'] > $backend_priority) { if (isset(self::$backend_picks[$name]) && self::$backend_picks[$name]['priority'] > $backend_priority) {

View File

@ -22,6 +22,7 @@ class CacheTest extends SapphireTest {
SS_Cache::set_cache_lifetime('test', 0.5, 20); SS_Cache::set_cache_lifetime('test', 0.5, 20);
$cache = SS_Cache::factory('test'); $cache = SS_Cache::factory('test');
$this->assertEquals(0.5, $cache->getOption('lifetime'));
$cache->save('Good', 'cachekey'); $cache->save('Good', 'cachekey');
$this->assertEquals('Good', $cache->load('cachekey')); $this->assertEquals('Good', $cache->load('cachekey'));
@ -44,5 +45,17 @@ class CacheTest extends SapphireTest {
$this->assertFalse($cache1->load('cachekey')); $this->assertFalse($cache1->load('cachekey'));
$this->assertEquals('Bar', $cache2->load('cachekey')); $this->assertEquals('Bar', $cache2->load('cachekey'));
} }
function testCacheDefault() {
SS_Cache::set_cache_lifetime('default', 1200);
$default = SS_Cache::get_cache_lifetime('default');
$this->assertEquals(1200, $default['lifetime']);
$cache = SS_Cache::factory('somethingnew');
$this->assertEquals(1200, $cache->getOption('lifetime'));
}
} }