Skip to content

Commit

Permalink
Making Cache methods always need a config name. This allows the remov…
Browse files Browse the repository at this point in the history
…al of hidden and often confusing state that gets remembered by Cache each time config() is called. Removing the hidden state makes Cache more predictable.
  • Loading branch information
markstory committed Sep 18, 2010
1 parent 1707c92 commit dc65f23
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 99 deletions.
112 changes: 34 additions & 78 deletions cake/libs/cache.php
Expand Up @@ -36,13 +36,6 @@ class Cache {
*/
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();
*
Expand All @@ -60,8 +53,7 @@ class Cache {
/**
* Set the cache configuration to use. config() can
* both create new configurations, return the settings for already configured
* configurations. It also sets the 'default' configuration to use for subsequent
* operations.
* configurations.
*
* To create a new configuration:
*
Expand All @@ -82,10 +74,6 @@ public static function config($name = null, $settings = array()) {
$settings = $name;
}

if ($name === null || !is_string($name)) {
$name = self::$_name;
}

$current = array();
if (isset(self::$_config[$name])) {
$current = self::$_config[$name];
Expand All @@ -100,12 +88,11 @@ public static function config($name = null, $settings = array()) {
}

$engine = self::$_config[$name]['engine'];
self::$_name = $name;

if (!isset(self::$_engines[$name])) {
self::_buildEngine($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;
}
return compact('engine', 'settings');
Expand Down Expand Up @@ -190,44 +177,45 @@ protected static function _loadEngine($name, $plugin = null) {
*
* @param mixed $settings Optional string for simple name-value pair or array
* @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.
*/
public static function set($settings = array(), $value = null) {
if (!isset(self::$_config[self::$_name]) || !isset(self::$_engines[self::$_name])) {
public static function set($settings = array(), $value = null, $config = 'default') {
if (!isset(self::$_config[$config]) || !isset(self::$_engines[$config])) {
return false;
}
$name = self::$_name;
if (!empty($settings)) {
self::$_reset = true;
}

if (self::$_reset === true) {
if (empty($settings)) {
self::$_reset = false;
$settings = self::$_config[$name];
$settings = self::$_config[$config];
} else {
if (is_string($settings) && $value !== null) {
$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'])) {
$settings['duration'] = strtotime($settings['duration']) - time();
}
}
self::$_engines[$name]->settings = $settings;
self::$_engines[$config]->settings = $settings;
}
return self::settings($name);
return self::settings($config);
}

/**
* Garbage collection
*
* Permanently remove all expired and deleted data
*
* @param string $config The config name you wish to have garbage collected. Defaults to 'default'
* @return void
*/
public static function gc() {
self::$_engines[self::$_name]->gc();
public static function gc($config = 'default') {
self::$_engines[$config]->gc();
}

/**
Expand All @@ -247,13 +235,10 @@ public static function gc() {
*
* @param string $key Identifier for the data
* @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
*/
public static function write($key, $value, $config = null) {
if (!$config) {
$config = self::$_name;
}
public static function write($key, $value, $config = 'default') {
$settings = self::settings($config);

if (empty($settings)) {
Expand All @@ -269,7 +254,7 @@ public static function write($key, $value, $config = null) {
}

$success = self::$_engines[$config]->write($settings['prefix'] . $key, $value, $settings['duration']);
self::set();
self::set(array(), null, $config);
if ($success === false && $value !== '') {
trigger_error(
sprintf(__("%s cache was unable to write '%s' to cache", true), $config, $key),
Expand All @@ -295,13 +280,10 @@ public static function write($key, $value, $config = null) {
* `Cache::read('my_data', 'long_term');`
*
* @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
*/
public static function read($key, $config = null) {
if (!$config) {
$config = self::$_name;
}
public static function read($key, $config = 'default') {
$settings = self::settings($config);

if (empty($settings)) {
Expand All @@ -316,9 +298,7 @@ public static function read($key, $config = null) {
}
$success = self::$_engines[$config]->read($settings['prefix'] . $key);

if ($config !== null && $config !== self::$_name) {
self::set();
}
self::set(array(), null, $config);
return $success;
}

Expand All @@ -327,15 +307,11 @@ public static function read($key, $config = null) {
*
* @param string $key Identifier for the data
* @param integer $offset How much to add
* @param string $config Optional string configuration name. If not specified the current
* default config will be used.
* @param string $config Optional string configuration name. Defaults to 'default'
* @return mixed new value, or false if the data doesn't exist, is not integer,
* or if there was an error fetching it.
*/
public static function increment($key, $offset = 1, $config = null) {
if (!$config) {
$config = self::$_name;
}
public static function increment($key, $offset = 1, $config = 'default') {
$settings = self::settings($config);

if (empty($settings)) {
Expand All @@ -350,23 +326,19 @@ public static function increment($key, $offset = 1, $config = null) {
return false;
}
$success = self::$_engines[$config]->increment($settings['prefix'] . $key, $offset);
self::set();
self::set(array(), null, $config);
return $success;
}
/**
* Decrement a number under the key and return decremented value.
*
* @param string $key Identifier for the data
* @param integer $offset How much to substract
* @param string $config Optional string configuration name, if not specified the current
* default config will be used.
* @param string $config Optional string configuration name. Defaults to 'default'
* @return mixed new value, or false if the data doesn't exist, is not integer,
* or if there was an error fetching it
*/
public static function decrement($key, $offset = 1, $config = null) {
if (!$config) {
$config = self::$_name;
}
public static function decrement($key, $offset = 1, $config = 'default') {
$settings = self::settings($config);

if (empty($settings)) {
Expand All @@ -381,13 +353,11 @@ public static function decrement($key, $offset = 1, $config = null) {
return false;
}
$success = self::$_engines[$config]->decrement($settings['prefix'] . $key, $offset);
self::set();
self::set(array(), null, $config);
return $success;
}
/**
* Delete a key from the cache. Will automatically use the currently
* active cache configuration. To set the currently active configuration use
* Cache::config()
* Delete a key from the cache.
*
* ### Usage:
*
Expand All @@ -400,13 +370,10 @@ public static function decrement($key, $offset = 1, $config = null) {
* `Cache::delete('my_data', 'long_term');`
*
* @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
*/
public static function delete($key, $config = null) {
if (!$config) {
$config = self::$_name;
}
public static function delete($key, $config = 'default') {
$settings = self::settings($config);

if (empty($settings)) {
Expand All @@ -421,21 +388,18 @@ public static function delete($key, $config = null) {
}

$success = self::$_engines[$config]->delete($settings['prefix'] . $key);
self::set();
self::set(array(), null, $config);
return $success;
}

/**
* Delete all keys from the cache.
*
* @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
*/
public static function clear($check = false, $config = null) {
if (!$config) {
$config = self::$_name;
}
public static function clear($check = false, $config = 'default') {
$settings = self::settings($config);

if (empty($settings)) {
Expand All @@ -446,7 +410,7 @@ public static function clear($check = false, $config = null) {
return false;
}
$success = self::$_engines[$config]->clear($check);
self::set();
self::set(array(), null, $config);
return $success;
}

Expand All @@ -457,31 +421,23 @@ public static function clear($check = false, $config = null) {
* @param string $config Name of the configuration setting
* @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')) {
return false;
}
if (!$name && isset(self::$_config[self::$_name])) {
$name = self::$_name;
}
return isset(self::$_engines[$name]);
}

/**
* Return the settings for current cache engine. If no name is supplied the settings
* for the 'active default' configuration will be returned. To set the 'active default'
* configuration use `Cache::config()`
* Return the settings for the named cache engine.
*
* @param string $engine Name of the configuration to get settings for.
* @return array list of settings for this engine
* @see Cache::config()
* @access public
* @static
*/
public static function settings($name = null) {
if (!$name && isset(self::$_config[self::$_name])) {
$name = self::$_name;
}
public static function settings($name = 'default') {
if (!empty(self::$_engines[$name])) {
return self::$_engines[$name]->settings();
}
Expand Down
38 changes: 17 additions & 21 deletions cake/tests/cases/libs/cache.test.php
Expand Up @@ -172,26 +172,22 @@ function testConfigChange() {
function testConfigSettingDefaultConfigKey() {
Cache::config('test_name', array('engine' => 'File', 'prefix' => 'test_name_'));

Cache::config('test_name');
Cache::write('value_one', 'I am cached');
$result = Cache::read('value_one');
Cache::write('value_one', 'I am cached', 'test_name');
$result = Cache::read('value_one', 'test_name');
$this->assertEqual($result, 'I am cached');

Cache::config('default');
$result = Cache::read('value_one');
$this->assertEqual($result, null);

Cache::write('value_one', 'I am in default config!');
$result = Cache::read('value_one');
$this->assertEqual($result, 'I am in default config!');

Cache::config('test_name');
$result = Cache::read('value_one');
$result = Cache::read('value_one', 'test_name');
$this->assertEqual($result, 'I am cached');

Cache::delete('value_one');
Cache::config('default');
Cache::delete('value_one');
Cache::delete('value_one', 'test_name');
Cache::delete('value_one', 'default');
}

/**
Expand Down Expand Up @@ -345,34 +341,34 @@ function testCacheDisable() {
Configure::write('Cache.disable', false);
Cache::config('test_cache_disable_1', array('engine'=> 'File', 'path' => TMP . 'tests'));

$this->assertTrue(Cache::write('key_1', 'hello'));
$this->assertIdentical(Cache::read('key_1'), 'hello');
$this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
$this->assertIdentical(Cache::read('key_1', 'test_cache_disable_1'), 'hello');

Configure::write('Cache.disable', true);

$this->assertFalse(Cache::write('key_2', 'hello'));
$this->assertFalse(Cache::read('key_2'));
$this->assertFalse(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
$this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));

Configure::write('Cache.disable', false);

$this->assertTrue(Cache::write('key_3', 'hello'));
$this->assertIdentical(Cache::read('key_3'), 'hello');
$this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
$this->assertIdentical(Cache::read('key_3', 'test_cache_disable_1'), 'hello');

Configure::write('Cache.disable', true);
Cache::config('test_cache_disable_2', array('engine'=> 'File', 'path' => TMP . 'tests'));

$this->assertFalse(Cache::write('key_4', 'hello'));
$this->assertFalse(Cache::read('key_4'));
$this->assertFalse(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
$this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));

Configure::write('Cache.disable', false);

$this->assertTrue(Cache::write('key_5', 'hello'));
$this->assertIdentical(Cache::read('key_5'), 'hello');
$this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
$this->assertIdentical(Cache::read('key_5', 'test_cache_disable_2'), 'hello');

Configure::write('Cache.disable', true);

$this->assertFalse(Cache::write('key_6', 'hello'));
$this->assertFalse(Cache::read('key_6'));
$this->assertFalse(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
$this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
}

/**
Expand Down

0 comments on commit dc65f23

Please sign in to comment.