From fb0eaa3fdafb5a499d01c9b66f975b974f732eb3 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Tue, 17 Sep 2013 10:56:52 +1200 Subject: [PATCH 1/1] MDL-40903 cache: fixed up event invalidation --- cache/classes/helper.php | 48 ++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/cache/classes/helper.php b/cache/classes/helper.php index 15870acc218..96c66019a2a 100644 --- a/cache/classes/helper.php +++ b/cache/classes/helper.php @@ -238,20 +238,24 @@ class cache_helper { $instance = cache_config::instance(); $invalidationeventset = false; $factory = cache_factory::instance(); + $inuse = $factory->get_caches_in_use(); foreach ($instance->get_definitions() as $name => $definitionarr) { $definition = cache_definition::load($name, $definitionarr); if ($definition->invalidates_on_event($event)) { - // OK at this point we know that the definition has information to invalidate on the event. - // There are two routes, either its an application cache in which case we can invalidate it now. - // or it is a persistent cache that also needs to be invalidated now. - $applicationcache = $definition->get_mode() === cache_store::MODE_APPLICATION; - if ($applicationcache || $definition->data_should_be_persistent()) { - $cache = $factory->create_cache_from_definition($definition->get_component(), $definition->get_area()); - $cache->delete_many($keys); + // First up check if there is a cache loader for this definition already. + // If there is we need to invalidate the keys from there. + $definitionkey = $definition->get_component().'/'.$definition->get_area(); + if (isset($inuse[$definitionkey])) { + $inuse[$definitionkey]->delete_many($keys); } - // We need to flag the event in the "Event invalidation" cache if it hasn't already happened. - if ($invalidationeventset === false) { + // We should only log events for application and session caches. + // Request caches shouldn't have events as all data is lost at the end of the request. + // Events should only be logged once of course and likely several definitions are watching so we + // track its logging with $invalidationeventset. + $logevent = ($invalidationeventset === false && $definition->get_mode() !== cache_store::MODE_REQUEST); + + if ($logevent) { // Get the event invalidation cache. $cache = cache::make('core', 'eventinvalidation'); // Get any existing invalidated keys for this cache. @@ -313,23 +317,25 @@ class cache_helper { $instance = cache_config::instance(); $invalidationeventset = false; $factory = cache_factory::instance(); + $inuse = $factory->get_caches_in_use(); foreach ($instance->get_definitions() as $name => $definitionarr) { $definition = cache_definition::load($name, $definitionarr); if ($definition->invalidates_on_event($event)) { - // Check if this definition would result in a loader with persistent data being in use. - if ($definition->data_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(); + // First up check if there is a cache loader for this definition already. + // If there is we need to invalidate the keys from there. + $definitionkey = $definition->get_component().'/'.$definition->get_area(); + if (isset($inuse[$definitionkey])) { + $inuse[$definitionkey]->purge(); } + + // We should only log events for application and session caches. + // Request caches shouldn't have events as all data is lost at the end of the request. + // Events should only be logged once of course and likely several definitions are watching so we + // track its logging with $invalidationeventset. + $logevent = ($invalidationeventset === false && $definition->get_mode() !== cache_store::MODE_REQUEST); + // We need to flag the event in the "Event invalidation" cache if it hasn't already happened. - if ($invalidationeventset === false) { + if ($logevent && $invalidationeventset === false) { // Get the event invalidation cache. $cache = cache::make('core', 'eventinvalidation'); // Create a key to invalidate all. -- 2.43.0