2014-08-15 08:53:05 +02:00
|
|
|
<?php
|
2010-04-12 07:04:05 +02:00
|
|
|
|
|
|
|
class CacheTest extends SapphireTest {
|
|
|
|
|
2018-05-25 03:22:48 +02:00
|
|
|
public function setUpOnce() {
|
|
|
|
parent::setUpOnce();
|
|
|
|
Versioned::set_reading_mode('Stage.Live');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCacheBasics() {
|
2010-04-13 03:48:06 +02:00
|
|
|
$cache = SS_Cache::factory('test');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
$cache->save('Good', 'cachekey');
|
|
|
|
$this->assertEquals('Good', $cache->load('cachekey'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testCacheCanBeDisabled() {
|
2010-04-13 03:48:06 +02:00
|
|
|
SS_Cache::set_cache_lifetime('test', -1, 10);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-13 03:48:06 +02:00
|
|
|
$cache = SS_Cache::factory('test');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
$cache->save('Good', 'cachekey');
|
|
|
|
$this->assertFalse($cache->load('cachekey'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testCacheLifetime() {
|
2010-04-13 05:24:37 +02:00
|
|
|
SS_Cache::set_cache_lifetime('test', 0.5, 20);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-13 03:48:06 +02:00
|
|
|
$cache = SS_Cache::factory('test');
|
2012-09-03 02:55:19 +02:00
|
|
|
$this->assertEquals(0.5, $cache->getOption('lifetime'));
|
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
$cache->save('Good', 'cachekey');
|
|
|
|
$this->assertEquals('Good', $cache->load('cachekey'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-12-21 03:05:51 +01:00
|
|
|
// As per documentation, sleep may not sleep for the amount of time you tell it to sleep for
|
|
|
|
// This loop can make sure it *does* sleep for that long
|
|
|
|
$endtime = time() + 2;
|
|
|
|
while (time() < $endtime) {
|
|
|
|
// Sleep for another 2 seconds!
|
|
|
|
// This may end up sleeping for 4 seconds, but it's awwwwwwwright.
|
|
|
|
sleep(2);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
$this->assertFalse($cache->load('cachekey'));
|
|
|
|
}
|
2012-09-03 02:55:19 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testCacheSeperation() {
|
2010-04-13 03:48:06 +02:00
|
|
|
$cache1 = SS_Cache::factory('test1');
|
|
|
|
$cache2 = SS_Cache::factory('test2');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
$cache1->save('Foo', 'cachekey');
|
|
|
|
$cache2->save('Bar', 'cachekey');
|
|
|
|
$this->assertEquals('Foo', $cache1->load('cachekey'));
|
|
|
|
$this->assertEquals('Bar', $cache2->load('cachekey'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
$cache1->remove('cachekey');
|
|
|
|
$this->assertFalse($cache1->load('cachekey'));
|
|
|
|
$this->assertEquals('Bar', $cache2->load('cachekey'));
|
|
|
|
}
|
2012-09-03 02:55:19 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testCacheDefault() {
|
2012-09-03 02:55:19 +02:00
|
|
|
SS_Cache::set_cache_lifetime('default', 1200);
|
|
|
|
$default = SS_Cache::get_cache_lifetime('default');
|
|
|
|
|
|
|
|
$this->assertEquals(1200, $default['lifetime']);
|
|
|
|
|
|
|
|
$cache = SS_Cache::factory('somethingnew');
|
|
|
|
|
|
|
|
$this->assertEquals(1200, $cache->getOption('lifetime'));
|
|
|
|
}
|
|
|
|
|
2018-05-25 03:22:48 +02:00
|
|
|
public function testVersionedCacheSegmentation() {
|
|
|
|
$cacheInstance = SS_Cache::factory('versioned');
|
|
|
|
$cacheInstance->clean();
|
|
|
|
|
|
|
|
Versioned::set_reading_mode('Stage.Live');
|
2018-05-30 06:07:31 +02:00
|
|
|
$result = $cacheInstance->load('shared_key');
|
2018-05-25 03:22:48 +02:00
|
|
|
$this->assertFalse($result);
|
2018-05-30 06:07:31 +02:00
|
|
|
$cacheInstance->save('uncle', 'shared_key');
|
2018-05-30 06:16:46 +02:00
|
|
|
// Shared key is cached on LIVE
|
|
|
|
$this->assertEquals('uncle', $cacheInstance->load('shared_key'));
|
|
|
|
|
2018-05-25 03:22:48 +02:00
|
|
|
Versioned::set_reading_mode('Stage.Stage');
|
2018-05-30 06:16:46 +02:00
|
|
|
|
|
|
|
// Shared key does not exist on STAGE
|
|
|
|
$this->assertFalse($cacheInstance->load('shared_key'));
|
|
|
|
|
2018-05-30 06:07:31 +02:00
|
|
|
$cacheInstance->save('cheese', 'shared_key');
|
|
|
|
$cacheInstance->save('bar', 'stage_key');
|
2018-05-30 06:16:46 +02:00
|
|
|
|
|
|
|
// Shared key has its own value on STAGE
|
|
|
|
$this->assertEquals('cheese', $cacheInstance->load('shared_key'));
|
|
|
|
// New key is cached on STAGE
|
|
|
|
$this->assertEquals('bar', $cacheInstance->load('stage_key'));
|
|
|
|
|
2018-05-25 03:22:48 +02:00
|
|
|
Versioned::set_reading_mode('Stage.Live');
|
2018-05-30 06:16:46 +02:00
|
|
|
|
|
|
|
// New key does not exist on LIVE
|
|
|
|
$this->assertFalse($cacheInstance->load('stage_key'));
|
|
|
|
// Shared key retains its own value on LIVE
|
|
|
|
$this->assertEquals('uncle', $cacheInstance->load('shared_key'));
|
2018-05-25 03:22:48 +02:00
|
|
|
|
|
|
|
$cacheInstance->clean();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-26 11:53:03 +02:00
|
|
|
public function testDisableVersionedCacheSegmentation() {
|
2018-06-01 01:05:03 +02:00
|
|
|
$cacheInstance = SS_Cache::factory('versioned_disabled', 'Output', array('disable-segmentation' => true));
|
2018-05-26 11:53:03 +02:00
|
|
|
$cacheInstance->clean();
|
|
|
|
|
|
|
|
Versioned::set_reading_mode('Stage.Live');
|
2018-05-30 06:07:31 +02:00
|
|
|
$result = $cacheInstance->load('shared_key');
|
2018-05-26 11:53:03 +02:00
|
|
|
$this->assertFalse($result);
|
2018-05-30 06:07:31 +02:00
|
|
|
$cacheInstance->save('uncle', 'shared_key');
|
2018-05-30 06:16:46 +02:00
|
|
|
// Shared key is cached on LIVE
|
|
|
|
$this->assertEquals('uncle', $cacheInstance->load('shared_key'));
|
|
|
|
|
2018-05-26 11:53:03 +02:00
|
|
|
Versioned::set_reading_mode('Stage.Stage');
|
2018-05-30 06:16:46 +02:00
|
|
|
|
|
|
|
// Shared key is same on STAGE
|
|
|
|
$this->assertEquals('uncle', $cacheInstance->load('shared_key'));
|
|
|
|
|
2018-05-30 06:07:31 +02:00
|
|
|
$cacheInstance->save('cheese', 'shared_key');
|
|
|
|
$cacheInstance->save('bar', 'stage_key');
|
2018-05-30 06:16:46 +02:00
|
|
|
|
|
|
|
// Shared key is overwritten on STAGE
|
|
|
|
$this->assertEquals('cheese', $cacheInstance->load('shared_key'));
|
|
|
|
// New key is written on STAGE
|
|
|
|
$this->assertEquals('bar', $cacheInstance->load('stage_key'));
|
|
|
|
|
2018-05-26 11:53:03 +02:00
|
|
|
Versioned::set_reading_mode('Stage.Live');
|
2018-05-30 06:16:46 +02:00
|
|
|
// New key has same value on LIVE
|
|
|
|
$this->assertEquals('bar', $cacheInstance->load('stage_key'));
|
|
|
|
// New value for existing key is same on LIVE
|
|
|
|
$this->assertEquals('cheese', $cacheInstance->load('shared_key'));
|
2018-05-26 11:53:03 +02:00
|
|
|
|
|
|
|
$cacheInstance->clean();
|
|
|
|
}
|
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|