mirror of
https://github.com/moodle/moodle.git
synced 2025-01-29 19:50:14 +01:00
MDL-51482 caching: Fix unnecessary updates to lastinvalidation time
This commit is contained in:
parent
35d3e8b00b
commit
2351ed55e8
8
cache/classes/loaders.php
vendored
8
cache/classes/loaders.php
vendored
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
42
cache/tests/cache_test.php
vendored
42
cache/tests/cache_test.php
vendored
@ -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'));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user