Permalink
Browse files

NEW Allow setting default cache lifetime, and test that it's being us…

…ed correctly on new caches
  • Loading branch information...
1 parent dc9a21b commit b6a6c4670e14379c536ba388fe80d805e0968138 @halkyon halkyon committed Sep 3, 2012
Showing with 34 additions and 10 deletions.
  1. +19 −8 cache/Cache.php
  2. +15 −2 tests/cache/CacheTest.php
View
@@ -65,7 +65,7 @@ class SS_Cache {
protected static $backends = array();
protected static $backend_picks = array();
-
+
protected static $cache_lifetime = array();
/**
@@ -76,6 +76,7 @@ protected static function init(){
$cachedir = TEMP_FOLDER . DIRECTORY_SEPARATOR . 'cache';
if (!is_dir($cachedir)) mkdir($cachedir);
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 @@ static function pick_backend($name, $for, $priority=1) {
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
- *
+ *
* @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 $priority The priority. The highest priority setting is used. Unlike backends, 'any' is not special in terms of priority.
@@ -169,9 +178,11 @@ static function set_cache_lifetime($for, $lifetime=600, $priority=1) {
static function factory($for, $frontend='Output', $frontendOptions=null) {
self::init();
- $backend_name = 'default'; $backend_priority = -1;
- $cache_lifetime = 600; $lifetime_priority = -1;
-
+ $backend_name = 'default';
+ $backend_priority = -1;
+ $cache_lifetime = self::$cache_lifetime['default']['lifetime'];
+ $lifetime_priority = -1;
+
foreach (array('any', $for) as $name) {
if (isset(self::$backend_picks[$name]) && self::$backend_picks[$name]['priority'] > $backend_priority) {
$backend_name = self::$backend_picks[$name]['name'];
@@ -182,15 +193,15 @@ static function factory($for, $frontend='Output', $frontendOptions=null) {
$cache_lifetime = self::$cache_lifetime[$name]['lifetime'];
$lifetime_priority = self::$cache_lifetime[$name]['priority'];
}
- }
+ }
$backend = self::$backends[$backend_name];
$basicOptions = array('cache_id_prefix' => $for);
if ($cache_lifetime >= 0) $basicOptions['lifetime'] = $cache_lifetime;
else $basicOptions['caching'] = false;
-
+
$frontendOptions = $frontendOptions ? array_merge($basicOptions, $frontendOptions) : $basicOptions;
require_once 'Zend/Cache.php';
View
@@ -22,15 +22,16 @@ function testCacheLifetime() {
SS_Cache::set_cache_lifetime('test', 0.5, 20);
$cache = SS_Cache::factory('test');
-
+ $this->assertEquals(0.5, $cache->getOption('lifetime'));
+
$cache->save('Good', 'cachekey');
$this->assertEquals('Good', $cache->load('cachekey'));
sleep(1);
$this->assertFalse($cache->load('cachekey'));
}
-
+
function testCacheSeperation() {
$cache1 = SS_Cache::factory('test1');
$cache2 = SS_Cache::factory('test2');
@@ -44,5 +45,17 @@ function testCacheSeperation() {
$this->assertFalse($cache1->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'));
+ }
+
}

0 comments on commit b6a6c46

Please sign in to comment.