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

This commit is contained in:
Sam Hemelryk 2012-11-13 10:12:35 +13:00
parent c9f40e77b7
commit 82afd05c35
6 changed files with 152 additions and 2 deletions

14
cache/README.md vendored
View File

@ -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 File

@ -429,7 +429,7 @@ class cache_config {
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.

View File

@ -107,6 +107,11 @@ class cachestore_dummy implements cache_store {
*/
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 File

@ -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 @@ class cache_factory {
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 @@ class cache_factory {
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 File

@ -664,4 +664,33 @@ class cache_phpunit_tests extends advanced_testcase {
$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'));
}
}

View File

@ -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;
}
@ -137,6 +150,64 @@ class cache_phpunit_dummy_datasource implements cache_data_source {
}
}
/**
* 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.
*