diff --git a/cake/libs/cache.php b/cake/libs/cache.php index 320cca446..46d6af7fc 100644 --- a/cake/libs/cache.php +++ b/cake/libs/cache.php @@ -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,13 +203,14 @@ 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; } - return true; } diff --git a/cake/tests/cases/libs/cache.test.php b/cake/tests/cases/libs/cache.test.php index e80715629..732dac8aa 100644 --- a/cake/tests/cases/libs/cache.test.php +++ b/cake/tests/cases/libs/cache.test.php @@ -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 *