From c6e81ff213244397068b6523f72ed165d12f36fd Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 4 Sep 2020 14:17:24 +0800 Subject: [PATCH 1/2] MDL-42012 cache: Allow data sources to work when caching is disabled --- cache/classes/factory.php | 3 ++- cache/classes/loaders.php | 27 ++++++++++++++++++--- cache/disabledlib.php | 51 +++++++++++++++++++++++++++++++++------ 3 files changed, 68 insertions(+), 13 deletions(-) diff --git a/cache/classes/factory.php b/cache/classes/factory.php index 9abfe0ee0b7..07d7b358280 100644 --- a/cache/classes/factory.php +++ b/cache/classes/factory.php @@ -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); } diff --git a/cache/classes/loaders.php b/cache/classes/loaders.php index 2b86132987c..a557a3e9b69 100644 --- a/cache/classes/loaders.php +++ b/cache/classes/loaders.php @@ -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. * diff --git a/cache/disabledlib.php b/cache/disabledlib.php index 2bcc1caf8ad..1883fccdb72 100644 --- a/cache/disabledlib.php +++ b/cache/disabledlib.php @@ -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. } -} \ No newline at end of file +} -- 2.26.2 From 75d006590923937de6a2d9ed9ca0ce7cb0f2bcda Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 9 Sep 2020 13:50:37 +0800 Subject: [PATCH 2/2] MDL-42012 cache: Valid definitions are required even when disabled --- cache/tests/cache_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cache/tests/cache_test.php b/cache/tests/cache_test.php index a23672ea05b..82c9d605280 100644 --- a/cache/tests/cache_test.php +++ b/cache/tests/cache_test.php @@ -1335,7 +1335,7 @@ class core_cache_testcase extends advanced_testcase { $this->assertInstanceOf('cache_config_disabled', $config); // Check we get the expected disabled caches. - $cache = cache::make('phpunit', 'disable'); + $cache = cache::make('core', 'string'); $this->assertInstanceOf('cache_disabled', $cache); // Test an application cache. -- 2.26.2