| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* RedisEngineTest file
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
7 |
*
|
|
|
8 |
* Licensed under The MIT License
|
|
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
10 |
* Redistributions of files must retain the above copyright notice
|
|
|
11 |
*
|
|
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
13 |
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
|
|
14 |
* @package Cake.Test.Case.Cache.Engine
|
|
|
15 |
* @since CakePHP(tm) v 2.2
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('Cache', 'Cache');
|
|
|
20 |
App::uses('RedisEngine', 'Cache/Engine');
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* RedisEngineTest class
|
|
|
24 |
*
|
|
|
25 |
* @package Cake.Test.Case.Cache.Engine
|
|
|
26 |
*/
|
|
|
27 |
class RedisEngineTest extends CakeTestCase {
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* setUp method
|
|
|
31 |
*
|
|
|
32 |
* @return void
|
|
|
33 |
*/
|
|
|
34 |
public function setUp() {
|
|
|
35 |
parent::setUp();
|
|
|
36 |
$this->skipIf(!class_exists('Redis'), 'Redis is not installed or configured properly.');
|
|
|
37 |
|
|
|
38 |
$this->_cacheDisable = Configure::read('Cache.disable');
|
|
|
39 |
Configure::write('Cache.disable', false);
|
|
|
40 |
Cache::config('redis', array(
|
|
|
41 |
'engine' => 'Redis',
|
|
|
42 |
'prefix' => 'cake_',
|
|
|
43 |
'duration' => 3600
|
|
|
44 |
));
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* tearDown method
|
|
|
49 |
*
|
|
|
50 |
* @return void
|
|
|
51 |
*/
|
|
|
52 |
public function tearDown() {
|
|
|
53 |
parent::tearDown();
|
|
|
54 |
Configure::write('Cache.disable', $this->_cacheDisable);
|
|
|
55 |
Cache::drop('');
|
|
|
56 |
Cache::drop('redis_groups');
|
|
|
57 |
Cache::drop('redis_helper');
|
|
|
58 |
Cache::config('default');
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* testSettings method
|
|
|
63 |
*
|
|
|
64 |
* @return void
|
|
|
65 |
*/
|
|
|
66 |
public function testSettings() {
|
|
|
67 |
$settings = Cache::settings('redis');
|
|
|
68 |
$expecting = array(
|
|
|
69 |
'prefix' => 'cake_',
|
|
|
70 |
'duration' => 3600,
|
|
|
71 |
'probability' => 100,
|
|
|
72 |
'groups' => array(),
|
|
|
73 |
'engine' => 'Redis',
|
|
|
74 |
'server' => '127.0.0.1',
|
|
|
75 |
'port' => 6379,
|
|
|
76 |
'timeout' => 0,
|
|
|
77 |
'persistent' => true,
|
|
|
78 |
'password' => false,
|
|
|
79 |
);
|
|
|
80 |
$this->assertEquals($expecting, $settings);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* testConnect method
|
|
|
85 |
*
|
|
|
86 |
* @return void
|
|
|
87 |
*/
|
|
|
88 |
public function testConnect() {
|
|
|
89 |
$Redis = new RedisEngine();
|
|
|
90 |
$this->assertTrue($Redis->init(Cache::settings('redis')));
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* testReadAndWriteCache method
|
|
|
95 |
*
|
|
|
96 |
* @return void
|
|
|
97 |
*/
|
|
|
98 |
public function testReadAndWriteCache() {
|
|
|
99 |
Cache::set(array('duration' => 1), null, 'redis');
|
|
|
100 |
|
|
|
101 |
$result = Cache::read('test', 'redis');
|
|
|
102 |
$expecting = '';
|
|
|
103 |
$this->assertEquals($expecting, $result);
|
|
|
104 |
|
|
|
105 |
$data = 'this is a test of the emergency broadcasting system';
|
|
|
106 |
$result = Cache::write('test', $data, 'redis');
|
|
|
107 |
$this->assertTrue($result);
|
|
|
108 |
|
|
|
109 |
$result = Cache::read('test', 'redis');
|
|
|
110 |
$expecting = $data;
|
|
|
111 |
$this->assertEquals($expecting, $result);
|
|
|
112 |
|
|
|
113 |
$data = array(1, 2, 3);
|
|
|
114 |
$this->assertTrue(Cache::write('array_data', $data, 'redis'));
|
|
|
115 |
$this->assertEquals($data, Cache::read('array_data', 'redis'));
|
|
|
116 |
|
|
|
117 |
Cache::delete('test', 'redis');
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* testExpiry method
|
|
|
122 |
*
|
|
|
123 |
* @return void
|
|
|
124 |
*/
|
|
|
125 |
public function testExpiry() {
|
|
|
126 |
Cache::set(array('duration' => 1), 'redis');
|
|
|
127 |
|
|
|
128 |
$result = Cache::read('test', 'redis');
|
|
|
129 |
$this->assertFalse($result);
|
|
|
130 |
|
|
|
131 |
$data = 'this is a test of the emergency broadcasting system';
|
|
|
132 |
$result = Cache::write('other_test', $data, 'redis');
|
|
|
133 |
$this->assertTrue($result);
|
|
|
134 |
|
|
|
135 |
sleep(2);
|
|
|
136 |
$result = Cache::read('other_test', 'redis');
|
|
|
137 |
$this->assertFalse($result);
|
|
|
138 |
|
|
|
139 |
Cache::set(array('duration' => "+1 second"), 'redis');
|
|
|
140 |
|
|
|
141 |
$data = 'this is a test of the emergency broadcasting system';
|
|
|
142 |
$result = Cache::write('other_test', $data, 'redis');
|
|
|
143 |
$this->assertTrue($result);
|
|
|
144 |
|
|
|
145 |
sleep(2);
|
|
|
146 |
$result = Cache::read('other_test', 'redis');
|
|
|
147 |
$this->assertFalse($result);
|
|
|
148 |
|
|
|
149 |
Cache::config('redis', array('duration' => '+1 second'));
|
|
|
150 |
sleep(2);
|
|
|
151 |
|
|
|
152 |
$result = Cache::read('other_test', 'redis');
|
|
|
153 |
$this->assertFalse($result);
|
|
|
154 |
|
|
|
155 |
Cache::config('redis', array('duration' => '+29 days'));
|
|
|
156 |
$data = 'this is a test of the emergency broadcasting system';
|
|
|
157 |
$result = Cache::write('long_expiry_test', $data, 'redis');
|
|
|
158 |
$this->assertTrue($result);
|
|
|
159 |
|
|
|
160 |
sleep(2);
|
|
|
161 |
$result = Cache::read('long_expiry_test', 'redis');
|
|
|
162 |
$expecting = $data;
|
|
|
163 |
$this->assertEquals($expecting, $result);
|
|
|
164 |
|
|
|
165 |
Cache::config('redis', array('duration' => 3600));
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
/**
|
|
|
169 |
* testDeleteCache method
|
|
|
170 |
*
|
|
|
171 |
* @return void
|
|
|
172 |
*/
|
|
|
173 |
public function testDeleteCache() {
|
|
|
174 |
$data = 'this is a test of the emergency broadcasting system';
|
|
|
175 |
$result = Cache::write('delete_test', $data, 'redis');
|
|
|
176 |
$this->assertTrue($result);
|
|
|
177 |
|
|
|
178 |
$result = Cache::delete('delete_test', 'redis');
|
|
|
179 |
$this->assertTrue($result);
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* testDecrement method
|
|
|
184 |
*
|
|
|
185 |
* @return void
|
|
|
186 |
*/
|
|
|
187 |
public function testDecrement() {
|
|
|
188 |
Cache::delete('test_decrement', 'redis');
|
|
|
189 |
$result = Cache::write('test_decrement', 5, 'redis');
|
|
|
190 |
$this->assertTrue($result);
|
|
|
191 |
|
|
|
192 |
$result = Cache::decrement('test_decrement', 1, 'redis');
|
|
|
193 |
$this->assertEquals(4, $result);
|
|
|
194 |
|
|
|
195 |
$result = Cache::read('test_decrement', 'redis');
|
|
|
196 |
$this->assertEquals(4, $result);
|
|
|
197 |
|
|
|
198 |
$result = Cache::decrement('test_decrement', 2, 'redis');
|
|
|
199 |
$this->assertEquals(2, $result);
|
|
|
200 |
|
|
|
201 |
$result = Cache::read('test_decrement', 'redis');
|
|
|
202 |
$this->assertEquals(2, $result);
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
/**
|
|
|
206 |
* testIncrement method
|
|
|
207 |
*
|
|
|
208 |
* @return void
|
|
|
209 |
*/
|
|
|
210 |
public function testIncrement() {
|
|
|
211 |
Cache::delete('test_increment', 'redis');
|
|
|
212 |
$result = Cache::increment('test_increment', 1, 'redis');
|
|
|
213 |
$this->assertEquals(1, $result);
|
|
|
214 |
|
|
|
215 |
$result = Cache::read('test_increment', 'redis');
|
|
|
216 |
$this->assertEquals(1, $result);
|
|
|
217 |
|
|
|
218 |
$result = Cache::increment('test_increment', 2, 'redis');
|
|
|
219 |
$this->assertEquals(3, $result);
|
|
|
220 |
|
|
|
221 |
$result = Cache::read('test_increment', 'redis');
|
|
|
222 |
$this->assertEquals(3, $result);
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
/**
|
|
|
226 |
* test clearing redis.
|
|
|
227 |
*
|
|
|
228 |
* @return void
|
|
|
229 |
*/
|
|
|
230 |
public function testClear() {
|
|
|
231 |
Cache::config('redis2', array(
|
|
|
232 |
'engine' => 'Redis',
|
|
|
233 |
'prefix' => 'cake2_',
|
|
|
234 |
'duration' => 3600
|
|
|
235 |
));
|
|
|
236 |
|
|
|
237 |
Cache::write('some_value', 'cache1', 'redis');
|
|
|
238 |
$result = Cache::clear(true, 'redis');
|
|
|
239 |
$this->assertTrue($result);
|
|
|
240 |
$this->assertEquals('cache1', Cache::read('some_value', 'redis'));
|
|
|
241 |
|
|
|
242 |
Cache::write('some_value', 'cache2', 'redis2');
|
|
|
243 |
$result = Cache::clear(false, 'redis');
|
|
|
244 |
$this->assertTrue($result);
|
|
|
245 |
$this->assertFalse(Cache::read('some_value', 'redis'));
|
|
|
246 |
$this->assertEquals('cache2', Cache::read('some_value', 'redis2'));
|
|
|
247 |
|
|
|
248 |
Cache::clear(false, 'redis2');
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
/**
|
|
|
252 |
* test that a 0 duration can successfully write.
|
|
|
253 |
*
|
|
|
254 |
* @return void
|
|
|
255 |
*/
|
|
|
256 |
public function testZeroDuration() {
|
|
|
257 |
Cache::config('redis', array('duration' => 0));
|
|
|
258 |
$result = Cache::write('test_key', 'written!', 'redis');
|
|
|
259 |
|
|
|
260 |
$this->assertTrue($result);
|
|
|
261 |
$result = Cache::read('test_key', 'redis');
|
|
|
262 |
$this->assertEquals('written!', $result);
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
/**
|
|
|
266 |
* Tests that configuring groups for stored keys return the correct values when read/written
|
|
|
267 |
* Shows that altering the group value is equivalent to deleting all keys under the same
|
|
|
268 |
* group
|
|
|
269 |
*
|
|
|
270 |
* @return void
|
|
|
271 |
*/
|
|
|
272 |
public function testGroupReadWrite() {
|
|
|
273 |
Cache::config('redis_groups', array(
|
|
|
274 |
'engine' => 'Redis',
|
|
|
275 |
'duration' => 3600,
|
|
|
276 |
'groups' => array('group_a', 'group_b'),
|
|
|
277 |
'prefix' => 'test_'
|
|
|
278 |
));
|
|
|
279 |
Cache::config('redis_helper', array(
|
|
|
280 |
'engine' => 'Redis',
|
|
|
281 |
'duration' => 3600,
|
|
|
282 |
'prefix' => 'test_'
|
|
|
283 |
));
|
|
|
284 |
$this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
|
|
|
285 |
$this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
|
|
|
286 |
|
|
|
287 |
Cache::increment('group_a', 1, 'redis_helper');
|
|
|
288 |
$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
|
|
|
289 |
$this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
|
|
|
290 |
$this->assertEquals('value2', Cache::read('test_groups', 'redis_groups'));
|
|
|
291 |
|
|
|
292 |
Cache::increment('group_b', 1, 'redis_helper');
|
|
|
293 |
$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
|
|
|
294 |
$this->assertTrue(Cache::write('test_groups', 'value3', 'redis_groups'));
|
|
|
295 |
$this->assertEquals('value3', Cache::read('test_groups', 'redis_groups'));
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
/**
|
|
|
299 |
* Tests that deleteing from a groups-enabled config is possible
|
|
|
300 |
*
|
|
|
301 |
* @return void
|
|
|
302 |
*/
|
|
|
303 |
public function testGroupDelete() {
|
|
|
304 |
Cache::config('redis_groups', array(
|
|
|
305 |
'engine' => 'Redis',
|
|
|
306 |
'duration' => 3600,
|
|
|
307 |
'groups' => array('group_a', 'group_b')
|
|
|
308 |
));
|
|
|
309 |
$this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
|
|
|
310 |
$this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
|
|
|
311 |
$this->assertTrue(Cache::delete('test_groups', 'redis_groups'));
|
|
|
312 |
|
|
|
313 |
$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
/**
|
|
|
317 |
* Test clearing a cache group
|
|
|
318 |
*
|
|
|
319 |
* @return void
|
|
|
320 |
*/
|
|
|
321 |
public function testGroupClear() {
|
|
|
322 |
Cache::config('redis_groups', array(
|
|
|
323 |
'engine' => 'Redis',
|
|
|
324 |
'duration' => 3600,
|
|
|
325 |
'groups' => array('group_a', 'group_b')
|
|
|
326 |
));
|
|
|
327 |
|
|
|
328 |
$this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
|
|
|
329 |
$this->assertTrue(Cache::clearGroup('group_a', 'redis_groups'));
|
|
|
330 |
$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
|
|
|
331 |
|
|
|
332 |
$this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
|
|
|
333 |
$this->assertTrue(Cache::clearGroup('group_b', 'redis_groups'));
|
|
|
334 |
$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
|
|
|
335 |
}
|
|
|
336 |
|
|
|
337 |
}
|