|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class CacheTest extends SapphireTest { |
|
4
|
|
|
|
|
5
|
|
|
public function setUpOnce() { |
|
6
|
|
|
parent::setUpOnce(); |
|
7
|
|
|
Versioned::set_reading_mode('Stage.Live'); |
|
8
|
|
|
} |
|
9
|
|
|
|
|
10
|
|
|
public function testCacheBasics() { |
|
11
|
|
|
$cache = SS_Cache::factory('test'); |
|
12
|
|
|
|
|
13
|
|
|
$cache->save('Good', 'cachekey'); |
|
14
|
|
|
$this->assertEquals('Good', $cache->load('cachekey')); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function testCacheCanBeDisabled() { |
|
18
|
|
|
SS_Cache::set_cache_lifetime('test', -1, 10); |
|
19
|
|
|
|
|
20
|
|
|
$cache = SS_Cache::factory('test'); |
|
21
|
|
|
|
|
22
|
|
|
$cache->save('Good', 'cachekey'); |
|
23
|
|
|
$this->assertFalse($cache->load('cachekey')); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function testCacheLifetime() { |
|
27
|
|
|
SS_Cache::set_cache_lifetime('test', 0.5, 20); |
|
28
|
|
|
|
|
29
|
|
|
$cache = SS_Cache::factory('test'); |
|
30
|
|
|
$this->assertEquals(0.5, $cache->getOption('lifetime')); |
|
31
|
|
|
|
|
32
|
|
|
$cache->save('Good', 'cachekey'); |
|
33
|
|
|
$this->assertEquals('Good', $cache->load('cachekey')); |
|
34
|
|
|
|
|
35
|
|
|
// As per documentation, sleep may not sleep for the amount of time you tell it to sleep for |
|
36
|
|
|
// This loop can make sure it *does* sleep for that long |
|
37
|
|
|
$endtime = time() + 2; |
|
38
|
|
|
while (time() < $endtime) { |
|
39
|
|
|
// Sleep for another 2 seconds! |
|
40
|
|
|
// This may end up sleeping for 4 seconds, but it's awwwwwwwright. |
|
41
|
|
|
sleep(2); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$this->assertFalse($cache->load('cachekey')); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testCacheSeperation() { |
|
48
|
|
|
$cache1 = SS_Cache::factory('test1'); |
|
49
|
|
|
$cache2 = SS_Cache::factory('test2'); |
|
50
|
|
|
|
|
51
|
|
|
$cache1->save('Foo', 'cachekey'); |
|
52
|
|
|
$cache2->save('Bar', 'cachekey'); |
|
53
|
|
|
$this->assertEquals('Foo', $cache1->load('cachekey')); |
|
54
|
|
|
$this->assertEquals('Bar', $cache2->load('cachekey')); |
|
55
|
|
|
|
|
56
|
|
|
$cache1->remove('cachekey'); |
|
57
|
|
|
$this->assertFalse($cache1->load('cachekey')); |
|
58
|
|
|
$this->assertEquals('Bar', $cache2->load('cachekey')); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testCacheDefault() { |
|
62
|
|
|
SS_Cache::set_cache_lifetime('default', 1200); |
|
63
|
|
|
$default = SS_Cache::get_cache_lifetime('default'); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertEquals(1200, $default['lifetime']); |
|
66
|
|
|
|
|
67
|
|
|
$cache = SS_Cache::factory('somethingnew'); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertEquals(1200, $cache->getOption('lifetime')); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testVersionedCacheSegmentation() { |
|
73
|
|
|
$cacheInstance = SS_Cache::factory('versioned'); |
|
74
|
|
|
$cacheInstance->clean(); |
|
75
|
|
|
|
|
76
|
|
|
Versioned::set_reading_mode('Stage.Live'); |
|
77
|
|
|
$result = $cacheInstance->load('shared_key'); |
|
78
|
|
|
$this->assertFalse($result); |
|
79
|
|
|
$cacheInstance->save('uncle', 'shared_key'); |
|
80
|
|
|
// Shared key is cached on LIVE |
|
81
|
|
|
$this->assertEquals('uncle', $cacheInstance->load('shared_key')); |
|
82
|
|
|
|
|
83
|
|
|
Versioned::set_reading_mode('Stage.Stage'); |
|
84
|
|
|
|
|
85
|
|
|
// Shared key does not exist on STAGE |
|
86
|
|
|
$this->assertFalse($cacheInstance->load('shared_key')); |
|
87
|
|
|
|
|
88
|
|
|
$cacheInstance->save('cheese', 'shared_key'); |
|
89
|
|
|
$cacheInstance->save('bar', 'stage_key'); |
|
90
|
|
|
|
|
91
|
|
|
// Shared key has its own value on STAGE |
|
92
|
|
|
$this->assertEquals('cheese', $cacheInstance->load('shared_key')); |
|
93
|
|
|
// New key is cached on STAGE |
|
94
|
|
|
$this->assertEquals('bar', $cacheInstance->load('stage_key')); |
|
95
|
|
|
|
|
96
|
|
|
Versioned::set_reading_mode('Stage.Live'); |
|
97
|
|
|
|
|
98
|
|
|
// New key does not exist on LIVE |
|
99
|
|
|
$this->assertFalse($cacheInstance->load('stage_key')); |
|
100
|
|
|
// Shared key retains its own value on LIVE |
|
101
|
|
|
$this->assertEquals('uncle', $cacheInstance->load('shared_key')); |
|
102
|
|
|
|
|
103
|
|
|
$cacheInstance->clean(); |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testDisableVersionedCacheSegmentation() { |
|
108
|
|
|
$cacheInstance = SS_Cache::factory('versioned_disabled', 'Output', array('disable-segmentation' => true)); |
|
109
|
|
|
$cacheInstance->clean(); |
|
110
|
|
|
|
|
111
|
|
|
Versioned::set_reading_mode('Stage.Live'); |
|
112
|
|
|
$result = $cacheInstance->load('shared_key'); |
|
113
|
|
|
$this->assertFalse($result); |
|
114
|
|
|
$cacheInstance->save('uncle', 'shared_key'); |
|
115
|
|
|
// Shared key is cached on LIVE |
|
116
|
|
|
$this->assertEquals('uncle', $cacheInstance->load('shared_key')); |
|
117
|
|
|
|
|
118
|
|
|
Versioned::set_reading_mode('Stage.Stage'); |
|
119
|
|
|
|
|
120
|
|
|
// Shared key is same on STAGE |
|
121
|
|
|
$this->assertEquals('uncle', $cacheInstance->load('shared_key')); |
|
122
|
|
|
|
|
123
|
|
|
$cacheInstance->save('cheese', 'shared_key'); |
|
124
|
|
|
$cacheInstance->save('bar', 'stage_key'); |
|
125
|
|
|
|
|
126
|
|
|
// Shared key is overwritten on STAGE |
|
127
|
|
|
$this->assertEquals('cheese', $cacheInstance->load('shared_key')); |
|
128
|
|
|
// New key is written on STAGE |
|
129
|
|
|
$this->assertEquals('bar', $cacheInstance->load('stage_key')); |
|
130
|
|
|
|
|
131
|
|
|
Versioned::set_reading_mode('Stage.Live'); |
|
132
|
|
|
// New key has same value on LIVE |
|
133
|
|
|
$this->assertEquals('bar', $cacheInstance->load('stage_key')); |
|
134
|
|
|
// New value for existing key is same on LIVE |
|
135
|
|
|
$this->assertEquals('cheese', $cacheInstance->load('shared_key')); |
|
136
|
|
|
|
|
137
|
|
|
$cacheInstance->clean(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
|