From 3680c61a3c489cd388202cefd605868b81730b5a Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Tue, 13 Nov 2012 14:59:19 +1300 Subject: [PATCH] MDL-36466 cache: implemented functionality to disable the bulk of the cache API --- cache/README.md | 9 +- cache/classes/factory.php | 19 +- cache/disabledlib.php | 459 ++++++++++++++++++++++++++++++++++ cache/tests/cache_test.php | 53 +++- cache/tests/locallib_test.php | 16 ++ 5 files changed, 552 insertions(+), 4 deletions(-) create mode 100644 cache/disabledlib.php diff --git a/cache/README.md b/cache/README.md index 8d250cb3d58..ef8c82a63a6 100644 --- a/cache/README.md +++ b/cache/README.md @@ -68,7 +68,14 @@ While the cache API must still be functional in order for calls to it to work it // If you disabled it using the above means you can re-enable it with: cache_factory::reset(); - +Disabling the cache entirely. +Like above there are times when you want the cache to avoid initialising anything it doesn't absolutely need. Things such as installation and upgrade require this functionality. +When the cache API is disabled it is still functional however special "disabled" classes will be used instead of the regular classes that make the Cache API tick. +These disabled classes do the least work possible and through this means we avoid all manner of intialisation and configuration. +Once disabled its not recommened to reneable the Cache API. Instead if you need it, redirect. + + // To disable the cache entirely call the following: + cache_factory::disable(); Cache API parts --------------- diff --git a/cache/classes/factory.php b/cache/classes/factory.php index 798a9a7b32e..3ea7a5b37c1 100644 --- a/cache/classes/factory.php +++ b/cache/classes/factory.php @@ -233,7 +233,7 @@ class cache_factory { * @param string $name The name of the store (must be unique remember) * @param array $details * @param cache_definition $definition The definition to instantiate it for. - * @return boolean + * @return boolean|cache_store */ public function create_store_from_config($name, array $details, cache_definition $definition) { if (!array_key_exists($name, $this->stores)) { @@ -413,13 +413,28 @@ class cache_factory { return $this->state === self::STATE_DISABLED; } + /** + * Disables as much of the cache API as possible. + * + * All of the magic associated with the disabled cache is wrapped into this function. + * In switching out the factory for the disabled factory it gains full control over the initialisation of objects + * and can use all of the disabled alternatives. + * Simple! + */ + public static function disable() { + global $CFG; + require_once($CFG->dirroot.'/cache/disabledlib.php'); + self::$instance = null; + self::$instance = new cache_factory_disabled(); + } + /** * Returns true if the cache stores have been disabled. * * @return bool */ public function stores_disabled() { - return $this->state === self::STATE_STORES_DISABLED; + return $this->state === self::STATE_STORES_DISABLED || $this->is_disabled(); } /** diff --git a/cache/disabledlib.php b/cache/disabledlib.php new file mode 100644 index 00000000000..ea703295ffb --- /dev/null +++ b/cache/disabledlib.php @@ -0,0 +1,459 @@ +. + +/** + * This file contains classes that are used by the Cache API only when it is disabled. + * + * These classes are derivatives of other significant classes used by the Cache API customised specifically + * to only do what is absolutely necessary when initialising and using the Cache API when its been disabled. + * + * @package core + * @category cache + * @copyright 2012 Sam Hemelryk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Required as it is needed for cache_config_disabled which extends cache_config_writer. + */ +require_once($CFG->dirroot.'/cache/locallib.php'); + +/** + * The cache loader class used when the Cache has been disabled. + * + * @copyright 2012 Sam Hemelryk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class cache_disabled extends cache { + + /** + * Constructs the cache. + * + * @param cache_definition $definition + * @param cache_store $store + * @param null $loader Unused. + */ + public function __construct(cache_definition $definition, cache_store $store, $loader = null) { + $this->definition = $definition; + $this->store = $store; + } + + /** + * Gets a key from the cache. + * + * @param int|string $key + * @param int $strictness Unused. + * @return bool + */ + public function get($key, $strictness = IGNORE_MISSING) { + return false; + } + + /** + * Gets many keys at once from the cache. + * + * @param array $keys + * @param int $strictness Unused. + * @return array + */ + public function get_many(array $keys, $strictness = IGNORE_MISSING) { + $return = array(); + foreach ($keys as $key) { + $return[$key] = false; + } + return $return; + } + + /** + * Sets a key value pair in the cache. + * + * @param int|string $key Unused. + * @param mixed $data Unused. + * @return bool + */ + public function set($key, $data) { + return false; + } + + /** + * Sets many key value pairs in the cache at once. + * + * @param array $keyvaluearray Unused. + * @return int + */ + public function set_many(array $keyvaluearray) { + return 0; + } + + /** + * Deletes an item from the cache. + * + * @param int|string $key Unused. + * @param bool $recurse Unused. + * @return bool + */ + public function delete($key, $recurse = true) { + return false; + } + + /** + * Deletes many items at once from the cache. + * + * @param array $keys Unused. + * @param bool $recurse Unused. + * @return int + */ + public function delete_many(array $keys, $recurse = true) { + return 0; + } + + /** + * Checks if the cache has the requested key. + * + * @param int|string $key Unused. + * @return bool + */ + public function has($key) { + return false; + } + + /** + * Checks if the cache has all of the requested keys. + * @param array $keys Unused. + * @return bool + */ + public function has_all(array $keys) { + return false; + } + + /** + * Checks if the cache has any of the requested keys. + * + * @param array $keys Unused. + * @return bool + */ + public function has_any(array $keys) { + return false; + } + + /** + * Purges all items from the cache. + * + * @return bool + */ + public function purge() { + return true; + } +} + +/** + * The cache factory class used when the Cache has been disabled. + * + * @copyright 2012 Sam Hemelryk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class cache_factory_disabled extends cache_factory { + + /** + * Returns an instance of the cache_factor method. + * + * @param bool $forcereload Unused. + * @return cache_factory + * @throws coding_exception + */ + public static function instance($forcereload = false) { + throw new coding_exception('You must not call to this cache factory within your code.'); + } + + /** + * Creates a definition instance or returns the existing one if it has already been created. + * + * @param string $component + * @param string $area + * @param string $aggregate Unused. + * @return cache_definition + */ + public function create_definition($component, $area, $aggregate = null) { + return cache_definition::load_adhoc(cache_store::MODE_REQUEST, $component, $area); + } + + /** + * Common public method to create a cache instance given a definition. + * + * @param cache_definition $definition + * @return cache_application|cache_session|cache_store + * @throws coding_exception + */ + public function create_cache(cache_definition $definition) { + return new cache_disabled($definition, $this->create_dummy_store($definition)); + } + + /** + * Creates a cache object given the parameters for a definition. + * + * @param string $component + * @param string $area + * @param array $identifiers + * @param string $aggregate + * @return cache_application|cache_session|cache_request + */ + public function create_cache_from_definition($component, $area, array $identifiers = array(), $aggregate = null) { + $definition = $this->create_definition($component, $area, $aggregate); + $cache = $this->create_cache($definition, $identifiers); + return $cache; + } + + /** + * Creates an ad-hoc cache from the given param. + * + * @param int $mode + * @param string $component + * @param string $area + * @param array $identifiers + * @param bool $persistent Unused. + * @return cache_application|cache_session|cache_request + */ + public function create_cache_from_params($mode, $component, $area, array $identifiers = array(), $persistent = false) { + $definition = cache_definition::load_adhoc($mode, $component, $area); + $cache = $this->create_cache($definition, $identifiers); + return $cache; + } + + /** + * Creates a store instance given its name and configuration. + * + * @param string $name Unused. + * @param array $details Unused. + * @param cache_definition $definition + * @return boolean|cache_store + */ + public function create_store_from_config($name, array $details, cache_definition $definition) { + return $this->create_dummy_store($definition); + } + + /** + * Creates a cache config instance with the ability to write if required. + * + * @param bool $writer Unused. + * @return cache_config|cache_config_writer + */ + public function create_config_instance($writer = false) { + $class = 'cache_config_disabled'; + if (!array_key_exists($class, $this->configs)) { + self::set_state(self::STATE_INITIALISING); + $configuration = $class::create_default_configuration(); + $this->configs[$class] = new $class; + $this->configs[$class]->load($configuration); + } + self::set_state(self::STATE_READY); + + // Return the instance. + return $this->configs[$class]; + } +} + +/** + * The cache config class used when the Cache has been disabled. + * + * @copyright 2012 Sam Hemelryk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class cache_config_disabled extends cache_config_writer { + + /** + * Returns an instance of the configuration writer. + * + * @return cache_config_disabled + */ + public static function instance() { + $factory = cache_factory::instance(); + return $factory->create_config_instance(true); + } + + /** + * Saves the current configuration. + */ + protected function config_save() { + // Nothing to do here. + } + + /** + * Generates a configuration array suitable to be written to the config file. + * + * @return array + */ + protected function generate_configuration_array() { + $configuration = array(); + $configuration['stores'] = $this->configstores; + $configuration['modemappings'] = $this->configmodemappings; + $configuration['definitions'] = $this->configdefinitions; + $configuration['definitionmappings'] = $this->configdefinitionmappings; + $configuration['locks'] = $this->configlocks; + return $configuration; + } + + /** + * Adds a plugin instance. + * + * @param string $name Unused. + * @param string $plugin Unused. + * @param array $configuration Unused. + * @return bool + * @throws cache_exception + */ + public function add_store_instance($name, $plugin, array $configuration = array()) { + return false; + } + + /** + * Sets the mode mappings. + * + * @param array $modemappings Unused. + * @return bool + * @throws cache_exception + */ + public function set_mode_mappings(array $modemappings) { + return false; + } + + /** + * Edits a give plugin instance. + * + * @param string $name Unused. + * @param string $plugin Unused. + * @param array $configuration Unused. + * @return bool + * @throws cache_exception + */ + public function edit_store_instance($name, $plugin, $configuration) { + return false; + } + + /** + * Deletes a store instance. + * + * @param string $name Unused. + * @return bool + * @throws cache_exception + */ + public function delete_store_instance($name) { + return false; + } + + /** + * Creates the default configuration and saves it. + * + * @return array + */ + public static function create_default_configuration() { + global $CFG; + + // HACK ALERT. + // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the + // default store plugins are protected from deletion. + require_once($CFG->dirroot.'/cache/stores/file/lib.php'); + require_once($CFG->dirroot.'/cache/stores/session/lib.php'); + require_once($CFG->dirroot.'/cache/stores/static/lib.php'); + + $writer = new self; + $writer->configstores = array( + 'default_application' => array( + 'name' => 'default_application', + 'plugin' => 'file', + 'configuration' => array(), + 'features' => cachestore_file::get_supported_features(), + 'modes' => cache_store::MODE_APPLICATION, + 'default' => true, + ), + 'default_session' => array( + 'name' => 'default_session', + 'plugin' => 'session', + 'configuration' => array(), + 'features' => cachestore_session::get_supported_features(), + 'modes' => cache_store::MODE_SESSION, + 'default' => true, + ), + 'default_request' => array( + 'name' => 'default_request', + 'plugin' => 'static', + 'configuration' => array(), + 'features' => cachestore_static::get_supported_features(), + 'modes' => cache_store::MODE_REQUEST, + 'default' => true, + ) + ); + $writer->configdefinitions = array(); + $writer->configmodemappings = array( + array( + 'mode' => cache_store::MODE_APPLICATION, + 'store' => 'default_application', + 'sort' => -1 + ), + array( + 'mode' => cache_store::MODE_SESSION, + 'store' => 'default_session', + 'sort' => -1 + ), + array( + 'mode' => cache_store::MODE_REQUEST, + 'store' => 'default_request', + 'sort' => -1 + ) + ); + $writer->configlocks = array( + 'default_file_lock' => array( + 'name' => 'cachelock_file_default', + 'type' => 'cachelock_file', + 'dir' => 'filelocks', + 'default' => true + ) + ); + + return $writer->generate_configuration_array(); + } + + /** + * Updates the definition in the configuration from those found in the cache files. + * + * @param bool $coreonly Unused. + */ + public static function update_definitions($coreonly = false) { + // Nothing to do here. + } + + /** + * Locates all of the definition files. + * + * @param bool $coreonly Unused. + * @return array + */ + protected static function locate_definitions($coreonly = false) { + return array(); + } + + /** + * Sets the mappings for a given definition. + * + * @param string $definition Unused. + * @param array $mappings Unused. + * @throws coding_exception + */ + public function set_definition_mappings($definition, $mappings) { + // Nothing to do here. + } +} \ No newline at end of file diff --git a/cache/tests/cache_test.php b/cache/tests/cache_test.php index 51b1126fef4..af7fa36dd49 100644 --- a/cache/tests/cache_test.php +++ b/cache/tests/cache_test.php @@ -50,6 +50,14 @@ class cache_phpunit_tests extends advanced_testcase { cache_config_phpunittest::create_default_configuration(); } + /** + * Final task is to reset the cache system + */ + public static function tearDownAfterClass() { + parent::tearDownAfterClass(); + cache_factory::reset(); + } + /** * Tests cache configuration */ @@ -666,7 +674,7 @@ class cache_phpunit_tests extends advanced_testcase { } /** - * Test disabling the cache. + * Test disabling the cache stores. */ public function test_disable_stores() { $instance = cache_config_phpunittest::instance(); @@ -693,4 +701,47 @@ class cache_phpunit_tests extends advanced_testcase { $this->assertTrue($cache->set('test', 'test')); $this->assertEquals('test', $cache->get('test')); } + + /** + * Test disabling the cache. + */ + public function test_disable() { + global $CFG; + + $configfile = $CFG->dataroot.'/muc/config.php'; + + // That's right, we're deleting the config file. + $this->assertTrue(@unlink($configfile)); + + // Disable the cache + cache_factory::disable(); + + // Check we get the expected disabled factory. + $factory = cache_factory::instance(); + $this->assertInstanceOf('cache_factory_disabled', $factory); + + // Check we get the expected disabled config. + $config = $factory->create_config_instance(); + $this->assertInstanceOf('cache_config_disabled', $config); + + // Check we get the expected disabled caches. + $cache = cache::make('phpunit', 'disable'); + $this->assertInstanceOf('cache_disabled', $cache); + + $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'disable'); + $this->assertInstanceOf('cache_disabled', $cache); + + $this->assertFalse(file_exists($configfile)); + + $this->assertFalse($cache->get('test')); + $this->assertFalse($cache->set('test', 'test')); + $this->assertFalse($cache->delete('test')); + $this->assertTrue($cache->purge()); + + cache_factory::reset(); + + $factory = cache_factory::instance(true); + $config = $factory->create_config_instance(); + $this->assertEquals('cache_config_phpunittest', get_class($config)); + } } \ No newline at end of file diff --git a/cache/tests/locallib_test.php b/cache/tests/locallib_test.php index 3e6ba79c7bf..004a5378086 100644 --- a/cache/tests/locallib_test.php +++ b/cache/tests/locallib_test.php @@ -50,6 +50,14 @@ class cache_config_writer_phpunit_tests extends advanced_testcase { cache_config_phpunittest::create_default_configuration(); } + /** + * Final task is to reset the cache system + */ + public static function tearDownAfterClass() { + parent::tearDownAfterClass(); + cache_factory::reset(); + } + /** * Test getting an instance. Pretty basic. */ @@ -297,6 +305,14 @@ class cache_administration_helper_phpunit_tests extends advanced_testcase { cache_config_phpunittest::create_default_configuration(); } + /** + * Final task is to reset the cache system + */ + public static function tearDownAfterClass() { + parent::tearDownAfterClass(); + cache_factory::reset(); + } + /** * Test the numerous summaries the helper can produce. */ -- 2.43.0