Merge pull request #766 from halkyon/cache_lifetime

Allow setting default cache lifetime
This commit is contained in:
Ingo Schommer 2012-09-03 14:35:05 -07:00
commit 0585f5d6ce
2 changed files with 34 additions and 10 deletions

27
cache/Cache.php vendored
View File

@ -65,7 +65,7 @@ class SS_Cache {
protected static $backends = array(); protected static $backends = array();
protected static $backend_picks = array(); protected static $backend_picks = array();
protected static $cache_lifetime = array(); protected static $cache_lifetime = array();
/** /**
@ -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);
} }
} }
@ -109,10 +110,18 @@ 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
* *
* @param string $for The name of the cache to set this lifetime for (or 'any' for all backends) * @param string $for The name of the cache to set this lifetime for (or 'any' for all backends)
* @param integer $lifetime The lifetime of an item of the cache, in seconds, or -1 to disable caching * @param integer $lifetime The lifetime of an item of the cache, in seconds, or -1 to disable caching
* @param integer $priority The priority. The highest priority setting is used. Unlike backends, 'any' is not special in terms of priority. * @param integer $priority The priority. The highest priority setting is used. Unlike backends, 'any' is not special in terms of priority.
@ -169,9 +178,11 @@ 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) {
$backend_name = self::$backend_picks[$name]['name']; $backend_name = self::$backend_picks[$name]['name'];
@ -182,7 +193,7 @@ class SS_Cache {
$cache_lifetime = self::$cache_lifetime[$name]['lifetime']; $cache_lifetime = self::$cache_lifetime[$name]['lifetime'];
$lifetime_priority = self::$cache_lifetime[$name]['priority']; $lifetime_priority = self::$cache_lifetime[$name]['priority'];
} }
} }
$backend = self::$backends[$backend_name]; $backend = self::$backends[$backend_name];
@ -190,7 +201,7 @@ class SS_Cache {
if ($cache_lifetime >= 0) $basicOptions['lifetime'] = $cache_lifetime; if ($cache_lifetime >= 0) $basicOptions['lifetime'] = $cache_lifetime;
else $basicOptions['caching'] = false; else $basicOptions['caching'] = false;
$frontendOptions = $frontendOptions ? array_merge($basicOptions, $frontendOptions) : $basicOptions; $frontendOptions = $frontendOptions ? array_merge($basicOptions, $frontendOptions) : $basicOptions;
require_once 'Zend/Cache.php'; require_once 'Zend/Cache.php';

View File

@ -22,7 +22,8 @@ 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'));
@ -30,7 +31,7 @@ class CacheTest extends SapphireTest {
$this->assertFalse($cache->load('cachekey')); $this->assertFalse($cache->load('cachekey'));
} }
function testCacheSeperation() { function testCacheSeperation() {
$cache1 = SS_Cache::factory('test1'); $cache1 = SS_Cache::factory('test1');
$cache2 = SS_Cache::factory('test2'); $cache2 = SS_Cache::factory('test2');
@ -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'));
}
} }