From d4f667ed1e6cd53873bc534f4e559c607efbce6b Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Wed, 29 Jul 2015 11:23:01 +0800 Subject: [PATCH] MDL-50932 cache: fix bug in prescanned file store Thanks to Mark Nielsen for providing a patch --- cache/stores/file/lib.php | 4 ++-- cache/stores/file/tests/file_test.php | 30 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/cache/stores/file/lib.php b/cache/stores/file/lib.php index 514a4bd4fb6..e044c91609b 100644 --- a/cache/stores/file/lib.php +++ b/cache/stores/file/lib.php @@ -341,8 +341,8 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i $maxtime = cache::now() - $ttl; } $readfile = false; - if ($this->prescan && array_key_exists($key, $this->keys)) { - if (!$ttl || $this->keys[$filename] >= $maxtime && file_exists($file)) { + if ($this->prescan && array_key_exists($filename, $this->keys)) { + if ((!$ttl || $this->keys[$filename] >= $maxtime) && file_exists($file)) { $readfile = true; } else { $this->delete($key); diff --git a/cache/stores/file/tests/file_test.php b/cache/stores/file/tests/file_test.php index be321f21180..887c5454f7c 100644 --- a/cache/stores/file/tests/file_test.php +++ b/cache/stores/file/tests/file_test.php @@ -44,4 +44,34 @@ class cachestore_file_test extends cachestore_tests { protected function get_class_name() { return 'cachestore_file'; } + + /** + * Testing cachestore_file::get with prescan enabled and with + * deleting the cache between the prescan and the call to get. + * + * The deleting of cache simulates some other process purging + * the cache. + */ + public function test_cache_get_with_prescan_and_purge() { + global $CFG; + + $definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'cachestore_file', 'phpunit_test'); + $name = 'File test'; + + $path = make_cache_directory('cachestore_file_test'); + $cache = new cachestore_file($name, array('path' => $path, 'prescan' => true)); + $cache->initialise($definition); + + $cache->set('testing', 'value'); + + $path = make_cache_directory('cachestore_file_test'); + $cache = new cachestore_file($name, array('path' => $path, 'prescan' => true)); + $cache->initialise($definition); + + // Let's pretend that some other process purged caches. + remove_dir($CFG->cachedir.'/cachestore_file_test', true); + make_cache_directory('cachestore_file_test'); + + $cache->get('testing'); + } } \ No newline at end of file