From 3d6dd54ad196af15eb5ac2b27c6b3452c85e2b6e Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Fri, 1 Mar 2013 11:37:27 +1300 Subject: [PATCH] MDL-38165 cache: fixed issues purging peristent caches by event --- cache/classes/factory.php | 24 ++++++++++++++++++++++++ cache/classes/helper.php | 19 +++++++++++-------- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/cache/classes/factory.php b/cache/classes/factory.php index 24c7c1ee64ee0..43f075b38fd1c 100644 --- a/cache/classes/factory.php +++ b/cache/classes/factory.php @@ -75,6 +75,12 @@ class cache_factory { */ protected $cachesfromparams = array(); + /** + * An array of stores organised by definitions. + * @var array + */ + protected $definitionstores = array(); + /** * An array of instantiated stores. * @var array @@ -272,9 +278,27 @@ public function create_store_from_config($name, array $details, cache_definition // order to address the issues. $store = $this->stores[$name]->create_clone($details); $store->initialise($definition); + $definitionid = $definition->get_id(); + if (!isset($this->definitionstores[$definitionid])) { + $this->definitionstores[$definitionid] = array(); + } + $this->definitionstores[$definitionid][] = $store; return $store; } + /** + * Returns an array of cache stores that have been initialised for use in definitions. + * @param cache_definition $definition + * @return array + */ + public function get_store_instances_in_use(cache_definition $definition) { + $id = $definition->get_id(); + if (!isset($this->definitionstores[$id])) { + return array(); + } + return $this->definitionstores[$id]; + } + /** * Creates a cache config instance with the ability to write if required. * diff --git a/cache/classes/helper.php b/cache/classes/helper.php index a8f3a26f62757..20e2f6d51baa1 100644 --- a/cache/classes/helper.php +++ b/cache/classes/helper.php @@ -312,15 +312,18 @@ public static function purge_by_event($event) { foreach ($instance->get_definitions() as $name => $definitionarr) { $definition = cache_definition::load($name, $definitionarr); if ($definition->invalidates_on_event($event)) { - // Create the cache. - $cache = $factory->create_cache($definition); - // Initialise, in case of a store. - if ($cache instanceof cache_store) { - $cache->initialise($definition); + // Check if this definition would result in a persistent loader being in use. + if ($definition->should_be_persistent()) { + // There may be a persistent cache loader. Lets purge that first so that any persistent data is removed. + $cache = $factory->create_cache_from_definition($definition->get_component(), $definition->get_area()); + $cache->purge(); + } + // Get all of the store instances that are in use for this store. + $stores = $factory->get_store_instances_in_use($definition); + foreach ($stores as $store) { + // Purge each store individually. + $store->purge(); } - // Purge the cache. - $cache->purge(); - // We need to flag the event in the "Event invalidation" cache if it hasn't already happened. if ($invalidationeventset === false) { // Get the event invalidation cache.