diff --git a/cache/classes/loaders.php b/cache/classes/loaders.php index 9d623e2faf6..e0a7723e079 100644 --- a/cache/classes/loaders.php +++ b/cache/classes/loaders.php @@ -1270,7 +1270,9 @@ class cache_application extends cache implements cache_loader_with_locking { $this->delete_many($todelete); } // Set the time of the last invalidation. - $this->set('lastinvalidation', cache::now()); + if ($purgeall || !empty($todelete)) { + $this->set('lastinvalidation', cache::now()); + } } } @@ -1669,7 +1671,9 @@ class cache_session extends cache { $this->delete_many($todelete); } // Set the time of the last invalidation. - $this->set('lastsessioninvalidation', cache::now()); + if ($purgeall || !empty($todelete)) { + $this->set('lastsessioninvalidation', cache::now()); + } } } diff --git a/cache/tests/cache_test.php b/cache/tests/cache_test.php index 0d6daa71cd8..6c9f14ccae1 100644 --- a/cache/tests/cache_test.php +++ b/cache/tests/cache_test.php @@ -989,6 +989,48 @@ class core_cache_testcase extends advanced_testcase { )); $cache = cache::make('phpunit', 'eventinvalidationtest'); $this->assertFalse($cache->get('testkey1')); + + // Test 3: Verify that an existing lastinvalidation cache file is updated when needed. + + // Make a new cache class. This should should invalidate testkey2. + $cache = cache::make('phpunit', 'eventinvalidationtest'); + // Timestamp should have updated to cache::now(). + $this->assertEquals(cache::now(), $cache->get('lastinvalidation')); + + // Set testkey2 data. + $cache->set('testkey2', 'test data 2'); + // Backdate the event invalidation time by 30 seconds. + $invalidationcache = cache::make('core', 'eventinvalidation'); + $invalidationcache->set('crazyevent', array('testkey2' => cache::now() - 30)); + // Lastinvalidation should already be cache::now(). + $this->assertEquals(cache::now(), $cache->get('lastinvalidation')); + // Set it to 15 seconds ago so that we know if it changes. + $cache->set('lastinvalidation', cache::now() - 15); + // Make a new cache class. This should not invalidate anything. + cache_factory::instance()->reset_cache_instances(); + $cache = cache::make('phpunit', 'eventinvalidationtest'); + // Lastinvalidation shouldn't change since it was already newer than invalidation event. + $this->assertEquals(cache::now() - 15, $cache->get('lastinvalidation')); + + // Now set the event invalidation to newer than the lastinvalidation time. + $invalidationcache->set('crazyevent', array('testkey2' => cache::now() - 5)); + // Make a new cache class. This should should invalidate testkey2. + cache_factory::instance()->reset_cache_instances(); + $cache = cache::make('phpunit', 'eventinvalidationtest'); + // Lastinvalidation timestamp should have updated to cache::now(). + $this->assertEquals(cache::now(), $cache->get('lastinvalidation')); + + // Now simulate a purge_by_event 5 seconds ago. + $invalidationcache = cache::make('core', 'eventinvalidation'); + $invalidationcache->set('crazyevent', array('purged' => cache::now() - 5)); + // Set our lastinvalidation timestamp to 15 seconds ago. + $cache->set('lastinvalidation', cache::now() - 15); + // Make a new cache class. This should invalidate the cache. + cache_factory::instance()->reset_cache_instances(); + $cache = cache::make('phpunit', 'eventinvalidationtest'); + // Lastinvalidation timestamp should have updated to cache::now(). + $this->assertEquals(cache::now(), $cache->get('lastinvalidation')); + } /**