Making Cache methods always need a config name. This allows the removal of hidden and often confusing state that gets remembered by Cache each time config() is called. Removing the hidden state makes Cache more predictable.

This commit is contained in:
mark_story 2010-09-18 00:45:29 -04:00
parent 1707c92aa1
commit dc65f2306b
2 changed files with 51 additions and 99 deletions

View file

@ -36,13 +36,6 @@ class Cache {
*/ */
protected static $_config = array(); protected static $_config = array();
/**
* Holds name of the current configuration name being used.
*
* @var array
*/
protected static $_name = 'default';
/** /**
* Whether to reset the settings with the next call to Cache::set(); * Whether to reset the settings with the next call to Cache::set();
* *
@ -60,8 +53,7 @@ class Cache {
/** /**
* Set the cache configuration to use. config() can * Set the cache configuration to use. config() can
* both create new configurations, return the settings for already configured * both create new configurations, return the settings for already configured
* configurations. It also sets the 'default' configuration to use for subsequent * configurations.
* operations.
* *
* To create a new configuration: * To create a new configuration:
* *
@ -82,10 +74,6 @@ class Cache {
$settings = $name; $settings = $name;
} }
if ($name === null || !is_string($name)) {
$name = self::$_name;
}
$current = array(); $current = array();
if (isset(self::$_config[$name])) { if (isset(self::$_config[$name])) {
$current = self::$_config[$name]; $current = self::$_config[$name];
@ -100,12 +88,11 @@ class Cache {
} }
$engine = self::$_config[$name]['engine']; $engine = self::$_config[$name]['engine'];
self::$_name = $name;
if (!isset(self::$_engines[$name])) { if (!isset(self::$_engines[$name])) {
self::_buildEngine($name); self::_buildEngine($name);
$settings = self::$_config[$name] = self::settings($name); $settings = self::$_config[$name] = self::settings($name);
} elseif ($settings = self::set(self::$_config[$name])) { } elseif ($settings = self::set(self::$_config[$name], null, $name)) {
self::$_config[$name] = $settings; self::$_config[$name] = $settings;
} }
return compact('engine', 'settings'); return compact('engine', 'settings');
@ -190,13 +177,13 @@ class Cache {
* *
* @param mixed $settings Optional string for simple name-value pair or array * @param mixed $settings Optional string for simple name-value pair or array
* @param string $value Optional for a simple name-value pair * @param string $value Optional for a simple name-value pair
* @param string $config The configuration name you are changing. Defaults to 'default'
* @return array Array of settings. * @return array Array of settings.
*/ */
public static function set($settings = array(), $value = null) { public static function set($settings = array(), $value = null, $config = 'default') {
if (!isset(self::$_config[self::$_name]) || !isset(self::$_engines[self::$_name])) { if (!isset(self::$_config[$config]) || !isset(self::$_engines[$config])) {
return false; return false;
} }
$name = self::$_name;
if (!empty($settings)) { if (!empty($settings)) {
self::$_reset = true; self::$_reset = true;
} }
@ -204,19 +191,19 @@ class Cache {
if (self::$_reset === true) { if (self::$_reset === true) {
if (empty($settings)) { if (empty($settings)) {
self::$_reset = false; self::$_reset = false;
$settings = self::$_config[$name]; $settings = self::$_config[$config];
} else { } else {
if (is_string($settings) && $value !== null) { if (is_string($settings) && $value !== null) {
$settings = array($settings => $value); $settings = array($settings => $value);
} }
$settings = array_merge(self::$_config[$name], $settings); $settings = array_merge(self::$_config[$config], $settings);
if (isset($settings['duration']) && !is_numeric($settings['duration'])) { if (isset($settings['duration']) && !is_numeric($settings['duration'])) {
$settings['duration'] = strtotime($settings['duration']) - time(); $settings['duration'] = strtotime($settings['duration']) - time();
} }
} }
self::$_engines[$name]->settings = $settings; self::$_engines[$config]->settings = $settings;
} }
return self::settings($name); return self::settings($config);
} }
/** /**
@ -224,10 +211,11 @@ class Cache {
* *
* Permanently remove all expired and deleted data * Permanently remove all expired and deleted data
* *
* @param string $config The config name you wish to have garbage collected. Defaults to 'default'
* @return void * @return void
*/ */
public static function gc() { public static function gc($config = 'default') {
self::$_engines[self::$_name]->gc(); self::$_engines[$config]->gc();
} }
/** /**
@ -247,13 +235,10 @@ class Cache {
* *
* @param string $key Identifier for the data * @param string $key Identifier for the data
* @param mixed $value Data to be cached - anything except a resource * @param mixed $value Data to be cached - anything except a resource
* @param string $config Optional string configuration name to write to. * @param string $config Optional string configuration name to write to. Defaults to 'default'
* @return boolean True if the data was successfully cached, false on failure * @return boolean True if the data was successfully cached, false on failure
*/ */
public static function write($key, $value, $config = null) { public static function write($key, $value, $config = 'default') {
if (!$config) {
$config = self::$_name;
}
$settings = self::settings($config); $settings = self::settings($config);
if (empty($settings)) { if (empty($settings)) {
@ -269,7 +254,7 @@ class Cache {
} }
$success = self::$_engines[$config]->write($settings['prefix'] . $key, $value, $settings['duration']); $success = self::$_engines[$config]->write($settings['prefix'] . $key, $value, $settings['duration']);
self::set(); self::set(array(), null, $config);
if ($success === false && $value !== '') { if ($success === false && $value !== '') {
trigger_error( trigger_error(
sprintf(__("%s cache was unable to write '%s' to cache", true), $config, $key), sprintf(__("%s cache was unable to write '%s' to cache", true), $config, $key),
@ -295,13 +280,10 @@ class Cache {
* `Cache::read('my_data', 'long_term');` * `Cache::read('my_data', 'long_term');`
* *
* @param string $key Identifier for the data * @param string $key Identifier for the data
* @param string $config optional name of the configuration to use. * @param string $config optional name of the configuration to use. Defaults to 'default'
* @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
*/ */
public static function read($key, $config = null) { public static function read($key, $config = 'default') {
if (!$config) {
$config = self::$_name;
}
$settings = self::settings($config); $settings = self::settings($config);
if (empty($settings)) { if (empty($settings)) {
@ -316,9 +298,7 @@ class Cache {
} }
$success = self::$_engines[$config]->read($settings['prefix'] . $key); $success = self::$_engines[$config]->read($settings['prefix'] . $key);
if ($config !== null && $config !== self::$_name) { self::set(array(), null, $config);
self::set();
}
return $success; return $success;
} }
@ -327,15 +307,11 @@ class Cache {
* *
* @param string $key Identifier for the data * @param string $key Identifier for the data
* @param integer $offset How much to add * @param integer $offset How much to add
* @param string $config Optional string configuration name. If not specified the current * @param string $config Optional string configuration name. Defaults to 'default'
* default config will be used.
* @return mixed new value, or false if the data doesn't exist, is not integer, * @return mixed new value, or false if the data doesn't exist, is not integer,
* or if there was an error fetching it. * or if there was an error fetching it.
*/ */
public static function increment($key, $offset = 1, $config = null) { public static function increment($key, $offset = 1, $config = 'default') {
if (!$config) {
$config = self::$_name;
}
$settings = self::settings($config); $settings = self::settings($config);
if (empty($settings)) { if (empty($settings)) {
@ -350,7 +326,7 @@ class Cache {
return false; return false;
} }
$success = self::$_engines[$config]->increment($settings['prefix'] . $key, $offset); $success = self::$_engines[$config]->increment($settings['prefix'] . $key, $offset);
self::set(); self::set(array(), null, $config);
return $success; return $success;
} }
/** /**
@ -358,15 +334,11 @@ class Cache {
* *
* @param string $key Identifier for the data * @param string $key Identifier for the data
* @param integer $offset How much to substract * @param integer $offset How much to substract
* @param string $config Optional string configuration name, if not specified the current * @param string $config Optional string configuration name. Defaults to 'default'
* default config will be used.
* @return mixed new value, or false if the data doesn't exist, is not integer, * @return mixed new value, or false if the data doesn't exist, is not integer,
* or if there was an error fetching it * or if there was an error fetching it
*/ */
public static function decrement($key, $offset = 1, $config = null) { public static function decrement($key, $offset = 1, $config = 'default') {
if (!$config) {
$config = self::$_name;
}
$settings = self::settings($config); $settings = self::settings($config);
if (empty($settings)) { if (empty($settings)) {
@ -381,13 +353,11 @@ class Cache {
return false; return false;
} }
$success = self::$_engines[$config]->decrement($settings['prefix'] . $key, $offset); $success = self::$_engines[$config]->decrement($settings['prefix'] . $key, $offset);
self::set(); self::set(array(), null, $config);
return $success; return $success;
} }
/** /**
* Delete a key from the cache. Will automatically use the currently * Delete a key from the cache.
* active cache configuration. To set the currently active configuration use
* Cache::config()
* *
* ### Usage: * ### Usage:
* *
@ -400,13 +370,10 @@ class Cache {
* `Cache::delete('my_data', 'long_term');` * `Cache::delete('my_data', 'long_term');`
* *
* @param string $key Identifier for the data * @param string $key Identifier for the data
* @param string $config name of the configuration to use * @param string $config name of the configuration to use. Defaults to 'default'
* @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
*/ */
public static function delete($key, $config = null) { public static function delete($key, $config = 'default') {
if (!$config) {
$config = self::$_name;
}
$settings = self::settings($config); $settings = self::settings($config);
if (empty($settings)) { if (empty($settings)) {
@ -421,7 +388,7 @@ class Cache {
} }
$success = self::$_engines[$config]->delete($settings['prefix'] . $key); $success = self::$_engines[$config]->delete($settings['prefix'] . $key);
self::set(); self::set(array(), null, $config);
return $success; return $success;
} }
@ -429,13 +396,10 @@ class Cache {
* Delete all keys from the cache. * Delete all keys from the cache.
* *
* @param boolean $check if true will check expiration, otherwise delete all * @param boolean $check if true will check expiration, otherwise delete all
* @param string $config name of the configuration to use * @param string $config name of the configuration to use. Defaults to 'default'
* @return boolean True if the cache was succesfully cleared, false otherwise * @return boolean True if the cache was succesfully cleared, false otherwise
*/ */
public static function clear($check = false, $config = null) { public static function clear($check = false, $config = 'default') {
if (!$config) {
$config = self::$_name;
}
$settings = self::settings($config); $settings = self::settings($config);
if (empty($settings)) { if (empty($settings)) {
@ -446,7 +410,7 @@ class Cache {
return false; return false;
} }
$success = self::$_engines[$config]->clear($check); $success = self::$_engines[$config]->clear($check);
self::set(); self::set(array(), null, $config);
return $success; return $success;
} }
@ -457,20 +421,15 @@ class Cache {
* @param string $config Name of the configuration setting * @param string $config Name of the configuration setting
* @return bool Whether or not the config name has been initialized. * @return bool Whether or not the config name has been initialized.
*/ */
public static function isInitialized($name = null) { public static function isInitialized($name) {
if (Configure::read('Cache.disable')) { if (Configure::read('Cache.disable')) {
return false; return false;
} }
if (!$name && isset(self::$_config[self::$_name])) {
$name = self::$_name;
}
return isset(self::$_engines[$name]); return isset(self::$_engines[$name]);
} }
/** /**
* Return the settings for current cache engine. If no name is supplied the settings * Return the settings for the named cache engine.
* for the 'active default' configuration will be returned. To set the 'active default'
* configuration use `Cache::config()`
* *
* @param string $engine Name of the configuration to get settings for. * @param string $engine Name of the configuration to get settings for.
* @return array list of settings for this engine * @return array list of settings for this engine
@ -478,10 +437,7 @@ class Cache {
* @access public * @access public
* @static * @static
*/ */
public static function settings($name = null) { public static function settings($name = 'default') {
if (!$name && isset(self::$_config[self::$_name])) {
$name = self::$_name;
}
if (!empty(self::$_engines[$name])) { if (!empty(self::$_engines[$name])) {
return self::$_engines[$name]->settings(); return self::$_engines[$name]->settings();
} }

View file

@ -172,12 +172,10 @@ class CacheTest extends CakeTestCase {
function testConfigSettingDefaultConfigKey() { function testConfigSettingDefaultConfigKey() {
Cache::config('test_name', array('engine' => 'File', 'prefix' => 'test_name_')); Cache::config('test_name', array('engine' => 'File', 'prefix' => 'test_name_'));
Cache::config('test_name'); Cache::write('value_one', 'I am cached', 'test_name');
Cache::write('value_one', 'I am cached'); $result = Cache::read('value_one', 'test_name');
$result = Cache::read('value_one');
$this->assertEqual($result, 'I am cached'); $this->assertEqual($result, 'I am cached');
Cache::config('default');
$result = Cache::read('value_one'); $result = Cache::read('value_one');
$this->assertEqual($result, null); $this->assertEqual($result, null);
@ -185,13 +183,11 @@ class CacheTest extends CakeTestCase {
$result = Cache::read('value_one'); $result = Cache::read('value_one');
$this->assertEqual($result, 'I am in default config!'); $this->assertEqual($result, 'I am in default config!');
Cache::config('test_name'); $result = Cache::read('value_one', 'test_name');
$result = Cache::read('value_one');
$this->assertEqual($result, 'I am cached'); $this->assertEqual($result, 'I am cached');
Cache::delete('value_one'); Cache::delete('value_one', 'test_name');
Cache::config('default'); Cache::delete('value_one', 'default');
Cache::delete('value_one');
} }
/** /**
@ -345,34 +341,34 @@ class CacheTest extends CakeTestCase {
Configure::write('Cache.disable', false); Configure::write('Cache.disable', false);
Cache::config('test_cache_disable_1', array('engine'=> 'File', 'path' => TMP . 'tests')); Cache::config('test_cache_disable_1', array('engine'=> 'File', 'path' => TMP . 'tests'));
$this->assertTrue(Cache::write('key_1', 'hello')); $this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
$this->assertIdentical(Cache::read('key_1'), 'hello'); $this->assertIdentical(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
Configure::write('Cache.disable', true); Configure::write('Cache.disable', true);
$this->assertFalse(Cache::write('key_2', 'hello')); $this->assertFalse(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
$this->assertFalse(Cache::read('key_2')); $this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
Configure::write('Cache.disable', false); Configure::write('Cache.disable', false);
$this->assertTrue(Cache::write('key_3', 'hello')); $this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
$this->assertIdentical(Cache::read('key_3'), 'hello'); $this->assertIdentical(Cache::read('key_3', 'test_cache_disable_1'), 'hello');
Configure::write('Cache.disable', true); Configure::write('Cache.disable', true);
Cache::config('test_cache_disable_2', array('engine'=> 'File', 'path' => TMP . 'tests')); Cache::config('test_cache_disable_2', array('engine'=> 'File', 'path' => TMP . 'tests'));
$this->assertFalse(Cache::write('key_4', 'hello')); $this->assertFalse(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
$this->assertFalse(Cache::read('key_4')); $this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
Configure::write('Cache.disable', false); Configure::write('Cache.disable', false);
$this->assertTrue(Cache::write('key_5', 'hello')); $this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
$this->assertIdentical(Cache::read('key_5'), 'hello'); $this->assertIdentical(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
Configure::write('Cache.disable', true); Configure::write('Cache.disable', true);
$this->assertFalse(Cache::write('key_6', 'hello')); $this->assertFalse(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
$this->assertFalse(Cache::read('key_6')); $this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
} }
/** /**