diff --git a/cache/classes/factory.php b/cache/classes/factory.php index 6ae461d7181..9abfe0ee0b7 100644 --- a/cache/classes/factory.php +++ b/cache/classes/factory.php @@ -335,6 +335,15 @@ class cache_factory { return $this->cachesfromdefinitions; } + /** + * Gets all adhoc caches that have been used within this request. + * + * @return cache_store[] Caches currently in use + */ + public function get_adhoc_caches_in_use() { + return $this->cachesfromparams; + } + /** * 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 f15e2ee8fb5..c2aa399e554 100644 --- a/cache/classes/helper.php +++ b/cache/classes/helper.php @@ -484,6 +484,9 @@ class cache_helper { foreach ($config->get_all_stores() as $store) { self::purge_store($store['name'], $config); } + foreach ($factory->get_adhoc_caches_in_use() as $cache) { + $cache->purge(); + } } /** diff --git a/cache/tests/cache_test.php b/cache/tests/cache_test.php index 0a47e4f9b15..8d04cd18dfa 100644 --- a/cache/tests/cache_test.php +++ b/cache/tests/cache_test.php @@ -1730,6 +1730,16 @@ class core_cache_testcase extends advanced_testcase { } } + /** + * Tests that ad-hoc caches are correctly purged with a purge_all call. + */ + public function test_purge_all_with_adhoc_caches() { + $cache = \cache::make_from_params(\cache_store::MODE_REQUEST, 'core_cache', 'test'); + $cache->set('test', 123); + cache_helper::purge_all(); + $this->assertFalse($cache->get('test')); + } + /** * Test that the default stores all support searching. */