Adding Cache::configured() and Cache::unconfig(). configured() allows introspection on the currently configured settings. unconfig() allows for the removal of cache configs.

Tests added.
This commit is contained in:
mark_story 2009-11-14 13:09:43 -05:00
parent 59a7cee89d
commit caedbdf98a
2 changed files with 91 additions and 4 deletions

View file

@ -122,6 +122,44 @@ class Cache {
return compact('engine', 'settings');
}
/**
* Returns an array containing the currently configured Cache settings.
*
* @return array
**/
function configured() {
$_this = Cache::getInstance();
return array_keys($_this->__config);
}
/**
* Unconfigures a cache engine. Deletes the cache configuration information
* If the deleted configuration is the last configuration using an certain engine,
* the Engine instance is also unset.
*
* @param string $name A currently configured cache config you wish to remove.
* @return boolen success of the removal, returns false when the config does not exist.
**/
function unconfig($name) {
$_this = Cache::getInstance();
if (!isset($_this->__config[$name])) {
return false;
}
$last = true;
$engine = $_this->__config[$name]['engine'];
unset($_this->__config[$name]);
foreach ($_this->__config as $name => $settings) {
if ($settings['engine'] == $engine) {
$last = false;
break;
}
}
if ($last) {
unset($_this->_Engine[$engine]);
}
return true;
}
/**
* Set the cache engine to use or modify settings for one instance
*
@ -165,14 +203,15 @@ class Cache {
*/
function __loadEngine($name, $plugin = null) {
if ($plugin) {
App::import('Lib', $plugin . '.cache' . DS . $name);
return App::import('Lib', $plugin . '.cache' . DS . $name);
} else {
if (!App::import('Lib', 'cache' . DS . $name)) {
App::import('Core', 'cache' . DS . $name);
}
$app = App::import('Lib', 'cache' . DS . $name);
if (!$app) {
return App::import('Core', 'cache' . DS . $name);
}
return true;
}
}
/**

View file

@ -89,6 +89,11 @@ class CacheTest extends CakeTestCase {
$settings = array('engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_');
$result = Cache::config('pluginLibEngine', $settings);
$this->assertEqual($result, Cache::config('pluginLibEngine'));
Cache::unconfig('libEngine');
Cache::unconfig('pluginLibEngine');
App::build();
}
/**
@ -157,6 +162,18 @@ class CacheTest extends CakeTestCase {
Cache::config('sessions', $_cacheConfigSessions['settings']);
}
/**
* test that configured returns an array of the currently configured cache
* settings
*
* @return void
**/
function testConfigured() {
$result = Cache::configured();
$this->assertTrue(in_array('_cake_core_', $result));
$this->assertTrue(in_array('default', $result));
}
/**
* testInitSettings method
*
@ -182,6 +199,37 @@ class CacheTest extends CakeTestCase {
Cache::engine('File');
}
/**
* test that unconfig removes cache configs, and that further attempts to use that config
* do not work.
*
* @return void
**/
function testUnconfig() {
App::build(array(
'libs' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'libs' . DS),
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
), true);
$result = Cache::unconfig('some_config_that_does_not_exist');
$this->assertFalse($result);
$_testsConfig = Cache::config('tests');
$result = Cache::unconfig('tests');
$this->assertTrue($result);
Cache::config('unconfigTest', array(
'engine' => 'TestAppCache'
));
$this->assertTrue(Cache::isInitialized('TestAppCache'));
$this->assertTrue(Cache::unconfig('unconfigTest'));
$this->assertFalse(Cache::isInitialized('TestAppCache'));
Cache::config('tests', $_testsConfig);
App::build();
}
/**
* testWriteEmptyValues method
*