MDL-63101 cache: Improve accuracy of cache event invalidation
This commit is contained in:
Vendored
+65
-10
@@ -1003,7 +1003,7 @@ class core_cache_testcase extends advanced_testcase {
|
||||
$timefile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/las-cache/lastinvalidation-$hash.cache";
|
||||
// Make sure the file is correct.
|
||||
$this->assertTrue(file_exists($timefile));
|
||||
$timecont = serialize(cache::now() - 60); // Back 60sec in the past to force it to re-invalidate.
|
||||
$timecont = serialize(cache::now(true) - 60); // Back 60sec in the past to force it to re-invalidate.
|
||||
make_writable_directory(dirname($timefile));
|
||||
file_put_contents($timefile, $timecont);
|
||||
$this->assertTrue(file_exists($timefile));
|
||||
@@ -1029,6 +1029,7 @@ class core_cache_testcase extends advanced_testcase {
|
||||
|
||||
// Test 2: Rebuild and test the invalidation of the event via the invalidation cache.
|
||||
cache_factory::reset();
|
||||
|
||||
$instance = cache_config_testing::instance();
|
||||
$instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
|
||||
'mode' => cache_store::MODE_APPLICATION,
|
||||
@@ -1040,6 +1041,7 @@ class core_cache_testcase extends advanced_testcase {
|
||||
'crazyevent'
|
||||
)
|
||||
));
|
||||
|
||||
$cache = cache::make('phpunit', 'eventinvalidationtest');
|
||||
$this->assertFalse($cache->get('testkey1'));
|
||||
|
||||
@@ -1047,23 +1049,30 @@ class core_cache_testcase extends advanced_testcase {
|
||||
|
||||
// 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'));
|
||||
|
||||
// Invalidation token should have been reset.
|
||||
$this->assertEquals(cache::get_purge_token(), $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'));
|
||||
$this->assertEquals(cache::get_purge_token(), $cache->get('lastinvalidation'));
|
||||
|
||||
// Set it to 15 seconds ago so that we know if it changes.
|
||||
$cache->set('lastinvalidation', cache::now() - 15);
|
||||
$pasttime = cache::now(true) - 15;
|
||||
$cache->set('lastinvalidation', $pasttime);
|
||||
|
||||
// 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'));
|
||||
$this->assertEquals($pasttime, $cache->get('lastinvalidation'));
|
||||
|
||||
// Now set the event invalidation to newer than the lastinvalidation time.
|
||||
$invalidationcache->set('crazyevent', array('testkey2' => cache::now() - 5));
|
||||
@@ -1071,18 +1080,18 @@ class core_cache_testcase extends advanced_testcase {
|
||||
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'));
|
||||
$this->assertEquals(cache::get_purge_token(), $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));
|
||||
$invalidationcache->set('crazyevent', array('purged' => cache::now(true) - 5));
|
||||
// Set our lastinvalidation timestamp to 15 seconds ago.
|
||||
$cache->set('lastinvalidation', cache::now() - 15);
|
||||
$cache->set('lastinvalidation', cache::now(true) - 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'));
|
||||
$this->assertEquals(cache::get_purge_token(), $cache->get('lastinvalidation'));
|
||||
|
||||
}
|
||||
|
||||
@@ -2272,4 +2281,50 @@ class core_cache_testcase extends advanced_testcase {
|
||||
$this->assertArrayNotHasKey($sessionid, $endstats);
|
||||
$this->assertArrayNotHasKey($requestid, $endstats);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests session cache event purge and subsequent visit in the same request.
|
||||
*
|
||||
* This test simulates a cache being created, a value being set, then the value being purged.
|
||||
* A subsequent use of the same cache is started in the same request which fills the cache.
|
||||
* A new request is started a short time later.
|
||||
* The cache should be filled.
|
||||
*/
|
||||
public function test_session_event_purge_same_second() {
|
||||
$instance = cache_config_testing::instance();
|
||||
$instance->phpunit_add_definition('phpunit/eventpurgetest', array(
|
||||
'mode' => cache_store::MODE_SESSION,
|
||||
'component' => 'phpunit',
|
||||
'area' => 'eventpurgetest',
|
||||
'invalidationevents' => array(
|
||||
'crazyevent',
|
||||
)
|
||||
));
|
||||
|
||||
// Create the cache, set a value, and immediately purge it by event.
|
||||
$cache = cache::make('phpunit', 'eventpurgetest');
|
||||
$cache->set('testkey1', 'test data 1');
|
||||
$this->assertEquals('test data 1', $cache->get('testkey1'));
|
||||
cache_helper::purge_by_event('crazyevent');
|
||||
$this->assertFalse($cache->get('testkey1'));
|
||||
|
||||
// Set up the cache again in the same request and add a new value back in.
|
||||
$factory = \cache_factory::instance();
|
||||
$factory->reset_cache_instances();
|
||||
$cache = cache::make('phpunit', 'eventpurgetest');
|
||||
$cache->set('testkey1', 'test data 2');
|
||||
$this->assertEquals('test data 2', $cache->get('testkey1'));
|
||||
|
||||
// Trick the cache into thinking that this is a new request.
|
||||
cache_phpunit_cache::simulate_new_request();
|
||||
$factory = \cache_factory::instance();
|
||||
$factory->reset_cache_instances();
|
||||
|
||||
// Set up the cache again.
|
||||
// This is a subsequent request at a new time, so we instead the invalidation time will be checked.
|
||||
// The invalidation time should match the last purged time and the cache will not be re-purged.
|
||||
$cache = cache::make('phpunit', 'eventpurgetest');
|
||||
$this->assertEquals('test data 2', $cache->get('testkey1'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user