MDL-42012 cache: Allow data sources to work when caching is disabled

This commit is contained in:
Andrew Nicols
2020-09-09 14:05:32 +08:00
parent 28a155e712
commit d2dd04ff2d
4 changed files with 68 additions and 19 deletions
+2 -1
View File
@@ -449,7 +449,8 @@ class cache_factory {
$definition = $instance->get_definition_by_id($id);
if (!$definition) {
throw new coding_exception('The requested cache definition does not exist.'. $id, $id);
} else if (!$this->is_disabled()) {
}
if (!$this->is_disabled() && !($this instanceof cache_factory_disabled)) {
debugging('Cache definitions reparsed causing cache reset in order to locate definition.
You should bump the version number to ensure definitions are reprocessed.', DEBUG_DEVELOPER);
}
+23 -4
View File
@@ -223,11 +223,9 @@ class cache implements cache_loader {
$this->storetype = get_class($store);
$this->perfdebug = (!empty($CFG->perfdebug) and $CFG->perfdebug > 7);
if ($loader instanceof cache_loader) {
$this->loader = $loader;
// Mark the loader as a sub (chained) loader.
$this->loader->set_is_sub_loader(true);
$this->set_loader($loader);
} else if ($loader instanceof cache_data_source) {
$this->datasource = $loader;
$this->set_data_source($loader);
}
$this->definition->generate_definition_hash();
$this->staticacceleration = $this->definition->use_static_acceleration();
@@ -237,6 +235,27 @@ class cache implements cache_loader {
$this->hasattl = ($this->definition->get_ttl() > 0);
}
/**
* Set the loader for this cache.
*
* @param cache_loader $loader
*/
protected function set_loader(cache_loader $loader): void {
$this->loader = $loader;
// Mark the loader as a sub (chained) loader.
$this->loader->set_is_sub_loader(true);
}
/**
* Set the data source for this cache.
*
* @param cache_data_source $datasource
*/
protected function set_data_source(cache_data_source $datasource): void {
$this->datasource = $datasource;
}
/**
* Used to inform the loader of its state as a sub loader, or as the top of the chain.
*
+43 -8
View File
@@ -49,7 +49,12 @@ class cache_disabled extends cache {
* @param null $loader Unused.
*/
public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
// Nothing to do here.
if ($loader instanceof cache_data_source) {
// Set the data source to allow data sources to work when caching is entirely disabled.
$this->set_data_source($loader);
}
// No other features are handled.
}
/**
@@ -60,6 +65,10 @@ class cache_disabled extends cache {
* @return bool
*/
public function get($key, $strictness = IGNORE_MISSING) {
if ($this->get_datasource() !== false) {
return $this->get_datasource()->load_for_cache($key);
}
return false;
}
@@ -71,10 +80,10 @@ class cache_disabled extends cache {
* @return array
*/
public function get_many(array $keys, $strictness = IGNORE_MISSING) {
$return = array();
foreach ($keys as $key) {
$return[$key] = false;
if ($this->get_datasource() !== false) {
return $this->get_datasource()->load_many_for_cache($keys);
}
return $return;
}
@@ -129,7 +138,9 @@ class cache_disabled extends cache {
* @return bool
*/
public function has($key, $tryloadifpossible = false) {
return false;
$result = $this->get($key);
return $result !== false;
}
/**
@@ -138,7 +149,16 @@ class cache_disabled extends cache {
* @return bool
*/
public function has_all(array $keys) {
return false;
if (!$this->get_datasource()) {
return false;
}
foreach ($keys as $key) {
if (!$this->has($key)) {
return false;
}
}
return true;
}
/**
@@ -148,6 +168,12 @@ class cache_disabled extends cache {
* @return bool
*/
public function has_any(array $keys) {
foreach ($keys as $key) {
if ($this->has($key)) {
return true;
}
}
return false;
}
@@ -189,6 +215,11 @@ class cache_factory_disabled extends cache_factory {
* @return cache_definition
*/
public function create_definition($component, $area, $unused = null) {
$definition = parent::create_definition($component, $area);
if ($definition->has_data_source()) {
return $definition;
}
return cache_definition::load_adhoc(cache_store::MODE_REQUEST, $component, $area);
}
@@ -200,7 +231,11 @@ class cache_factory_disabled extends cache_factory {
* @throws coding_exception
*/
public function create_cache(cache_definition $definition) {
return new cache_disabled($definition, $this->create_dummy_store($definition));
$loader = null;
if ($definition->has_data_source()) {
$loader = $definition->get_data_source();
}
return new cache_disabled($definition, $this->create_dummy_store($definition), $loader);
}
/**
@@ -484,4 +519,4 @@ class cache_config_disabled extends cache_config_writer {
public function set_definition_mappings($definition, $mappings) {
// Nothing to do here.
}
}
}
-6
View File
@@ -1342,8 +1342,6 @@ class core_cache_testcase extends advanced_testcase {
$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'));
@@ -1353,8 +1351,6 @@ class core_cache_testcase extends advanced_testcase {
$cache = cache::make_from_params(cache_store::MODE_SESSION, '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'));
@@ -1364,8 +1360,6 @@ class core_cache_testcase extends advanced_testcase {
$cache = cache::make_from_params(cache_store::MODE_REQUEST, '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'));