2014-08-15 08:53:05 +02:00
|
|
|
<?php
|
2010-04-12 07:04:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Core\Tests;
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Core\Cache;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
class CacheTest extends SapphireTest {
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testCacheBasics() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$cache = 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() {
|
2016-09-09 08:43:05 +02:00
|
|
|
Cache::set_cache_lifetime('test', -1, 10);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$cache = 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() {
|
2016-09-09 08:43:05 +02:00
|
|
|
Cache::set_cache_lifetime('test', 0.5, 20);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$cache = 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() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$cache1 = Cache::factory('test1');
|
|
|
|
$cache2 = 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() {
|
2016-09-09 08:43:05 +02:00
|
|
|
Cache::set_cache_lifetime('default', 1200);
|
|
|
|
$default = Cache::get_cache_lifetime('default');
|
2012-09-03 02:55:19 +02:00
|
|
|
|
|
|
|
$this->assertEquals(1200, $default['lifetime']);
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$cache = Cache::factory('somethingnew');
|
2012-09-03 02:55:19 +02:00
|
|
|
|
|
|
|
$this->assertEquals(1200, $cache->getOption('lifetime'));
|
|
|
|
}
|
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|