Permalink
Browse files

MDL-36466 cache: renamed disable to disable_stores and added tests

  • Loading branch information...
1 parent c9f40e7 commit 82afd05c3500d137d6b815635c3a37d28a8734f1 Sam Hemelryk committed Nov 12, 2012
View
@@ -56,6 +56,20 @@ If a data source had been specified in the definition, the following would be al
$cache = cache::make('core', 'string');
$component = $cache->get('component');
+Disabling the cache stores.
+There are times in code when you will want to disable the cache stores.
+While the cache API must still be functional in order for calls to it to work it is possible to disable the use of the cache stores separately so that you can be sure only the cache will function in all circumstances.
+
+ // Disable the cache store at the start of your script with:
+ define('CACHE_DISABLE_STORES', true);
+
+ // Disable the cache within your script when you want with:
+ cache_factory::disable_stores();
+ // If you disabled it using the above means you can re-enable it with:
+ cache_factory::reset();
+
+
+
Cache API parts
---------------
View
@@ -429,7 +429,7 @@ public function get_stores($mode, $requirements = 0) {
public function get_stores_for_definition(cache_definition $definition) {
// Check if MUC has been disabled.
$factory = cache_factory::instance();
- if ($factory->is_disabled()) {
+ if ($factory->stores_disabled()) {
// Yip its been disabled.
// To facilitate this we are going to always return an empty array of stores to use.
// This will force all cache instances to use the cachestore_dummy.
@@ -107,6 +107,11 @@ public static function get_supported_modes(array $configuration = array()) {
*/
public function initialise(cache_definition $definition) {
// If the definition isn't persistent then we need to be persistent here.
+ // The reasoning behind this is that:
+ // - If the definition is persistent then the cache loader is going to
+ // store things in its persistent cache.
+ // - If the definition is not persistent then the cache loader won't try to store anything
+ // and we will need to store it here in order to make sure it is accessible.
$this->persist = !$definition->should_be_persistent();
}
View
@@ -52,6 +52,8 @@ class cache_factory {
const STATE_ERROR_INITIALISING = 9;
/** The cache has been disabled. */
const STATE_DISABLED = 10;
+ /** The cache stores have been disabled */
+ const STATE_STORES_DISABLED = 11;
/**
* An instance of the cache_factory class created upon the first request.
@@ -112,7 +114,7 @@ public static function instance($forcereload = false) {
self::$instance = new cache_factory();
if (defined('CACHE_DISABLE_STORES') && CACHE_DISABLE_STORES !== false) {
// The cache stores have been disabled.
- self::$instance->set_state(self::STATE_DISABLED);;
+ self::$instance->set_state(self::STATE_STORES_DISABLED);;
}
}
return self::$instance;
@@ -410,4 +412,33 @@ public function set_state($state) {
public function is_disabled() {
return $this->state === self::STATE_DISABLED;
}
+
+ /**
+ * Returns true if the cache stores have been disabled.
+ *
+ * @return bool
+ */
+ public function stores_disabled() {
+ return $this->state === self::STATE_STORES_DISABLED;
+ }
+
+ /**
+ * Disables cache stores.
+ *
+ * The cache API will continue to function however none of the actual stores will be used.
+ * Instead the dummy store will be provided for all cache requests.
+ * This is useful in situations where you cannot be sure any stores are working.
+ *
+ * In order to re-enable the cache you must call the cache factories static reset method:
+ * <code>
+ * // Disable the cache factory.
+ * cache_factory::disable_stores();
+ * // Re-enable the cache factory by resetting it.
+ * cache_factory::reset();
+ * </code>
+ */
+ public static function disable_stores() {
+ $factory = self::instance();
+ $factory->set_state(self::STATE_STORES_DISABLED);
+ }
}
View
@@ -664,4 +664,33 @@ public function test_alt_cache_path() {
$instance = cache_config_phpunittest::instance();
$this->assertInstanceOf('cache_config', $instance);
}
+
+ /**
+ * Test disabling the cache.
+ */
+ public function test_disable_stores() {
+ $instance = cache_config_phpunittest::instance();
+ $instance->phpunit_add_definition('phpunit/disabletest', array(
+ 'mode' => cache_store::MODE_APPLICATION,
+ 'component' => 'phpunit',
+ 'area' => 'disabletest'
+ ));
+ $cache = cache::make('phpunit', 'disabletest');
+ $this->assertInstanceOf('cache_phpunit_application', $cache);
+ $this->assertEquals('cachestore_file', $cache->phpunit_get_store_class());
+
+ $this->assertFalse($cache->get('test'));
+ $this->assertTrue($cache->set('test', 'test'));
+ $this->assertEquals('test', $cache->get('test'));
+
+ cache_factory::disable_stores();
+
+ $cache = cache::make('phpunit', 'disabletest');
+ $this->assertInstanceOf('cache_phpunit_application', $cache);
+ $this->assertEquals('cachestore_dummy', $cache->phpunit_get_store_class());
+
+ $this->assertFalse($cache->get('test'));
+ $this->assertTrue($cache->set('test', 'test'));
+ $this->assertEquals('test', $cache->get('test'));
+ }
}
@@ -41,6 +41,19 @@ class cache_config_phpunittest extends cache_config_writer {
* @param array $properties
*/
public function phpunit_add_definition($area, array $properties) {
+ if (!array_key_exists('overrideclass', $properties)) {
+ switch ($properties['mode']) {
+ case cache_store::MODE_APPLICATION:
+ $properties['overrideclass'] = 'cache_phpunit_application';
+ break;
+ case cache_store::MDOE_SESSION:
+ $properties['overrideclass'] = 'cache_phpunit_session';
+ break;
+ case cache_store::MODE_REQUEST:
+ $properties['overrideclass'] = 'cache_phpunit_request';
+ break;
+ }
+ }
$this->configdefinitions[$area] = $properties;
}
@@ -138,6 +151,64 @@ public function load_many_for_cache(array $keys) {
}
/**
+ * PHPUnit application cache loader.
+ *
+ * Used to expose things we could not otherwise see within an application cache.
+ *
+ * @copyright 2012 Sam Hemelryk
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class cache_phpunit_application extends cache_application {
+
+ /**
+ * Returns the class of the store immediately associated with this cache.
+ * @return string
+ */
+ public function phpunit_get_store_class() {
+ return get_class($this->get_store());
+ }
+
+}
+
+/**
+ * PHPUnit session cache loader.
+ *
+ * Used to expose things we could not otherwise see within an session cache.
+ *
+ * @copyright 2012 Sam Hemelryk
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class cache_phpunit_session extends cache_session {
+
+ /**
+ * Returns the class of the store immediately associated with this cache.
+ * @return string
+ */
+ public function phpunit_get_store_class() {
+ return get_class($this->get_store());
+ }
+}
+
+/**
+ * PHPUnit request cache loader.
+ *
+ * Used to expose things we could not otherwise see within an request cache.
+ *
+ * @copyright 2012 Sam Hemelryk
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class cache_phpunit_request extends cache_request {
+
+ /**
+ * Returns the class of the store immediately associated with this cache.
+ * @return string
+ */
+ public function phpunit_get_store_class() {
+ return get_class($this->get_store());
+ }
+}
+
+/**
* Dummy overridden cache loader class that we can use to test overriding loader functionality.
*
* @copyright 2012 Sam Hemelryk

0 comments on commit 82afd05

Please sign in to comment.