統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / Cache / Engine / RedisEngineTest.php @ 635eef61

履歴 | 表示 | アノテート | ダウンロード (10.001 KB)

1 635eef61 spyder1211
<?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
                        'database' => 0,
80
                        'unix_socket' => false,
81
                );
82
                $this->assertEquals($expecting, $settings);
83
        }
84
85
/**
86
 * testConnect method
87
 *
88
 * @return void
89
 */
90
        public function testConnect() {
91
                $Redis = new RedisEngine();
92
                $this->assertTrue($Redis->init(Cache::settings('redis')));
93
        }
94
95
/**
96
 * testMultiDatabaseOperations method
97
 *
98
 * @return void
99
 */
100
        public function testMultiDatabaseOperations() {
101
                Cache::config('redisdb0', array(
102
                        'engine' => 'Redis',
103
                        'prefix' => 'cake2_',
104
                        'duration' => 3600,
105
                        'persistent' => false,
106
                ));
107
108
                Cache::config('redisdb1', array(
109
                        'engine' => 'Redis',
110
                        'database' => 1,
111
                        'prefix' => 'cake2_',
112
                        'duration' => 3600,
113
                        'persistent' => false,
114
                ));
115
116
                $result = Cache::write('save_in_0', true, 'redisdb0');
117
                $exist = Cache::read('save_in_0', 'redisdb0');
118
                $this->assertTrue($result);
119
                $this->assertTrue($exist);
120
121
                $result = Cache::write('save_in_1', true, 'redisdb1');
122
                $this->assertTrue($result);
123
                $exist = Cache::read('save_in_0', 'redisdb1');
124
                $this->assertFalse($exist);
125
                $exist = Cache::read('save_in_1', 'redisdb1');
126
                $this->assertTrue($exist);
127
128
                Cache::delete('save_in_0', 'redisdb0');
129
                $exist = Cache::read('save_in_0', 'redisdb0');
130
                $this->assertFalse($exist);
131
132
                Cache::delete('save_in_1', 'redisdb1');
133
                $exist = Cache::read('save_in_1', 'redisdb1');
134
                $this->assertFalse($exist);
135
136
                Cache::drop('redisdb0');
137
                Cache::drop('redisdb1');
138
        }
139
140
/**
141
 * testReadAndWriteCache method
142
 *
143
 * @return void
144
 */
145
        public function testReadAndWriteCache() {
146
                Cache::set(array('duration' => 1), null, 'redis');
147
148
                $result = Cache::read('test', 'redis');
149
                $expecting = '';
150
                $this->assertEquals($expecting, $result);
151
152
                $data = 'this is a test of the emergency broadcasting system';
153
                $result = Cache::write('test', $data, 'redis');
154
                $this->assertTrue($result);
155
156
                $result = Cache::read('test', 'redis');
157
                $expecting = $data;
158
                $this->assertEquals($expecting, $result);
159
160
                $data = array(1, 2, 3);
161
                $this->assertTrue(Cache::write('array_data', $data, 'redis'));
162
                $this->assertEquals($data, Cache::read('array_data', 'redis'));
163
164
                Cache::delete('test', 'redis');
165
        }
166
167
/**
168
 * testExpiry method
169
 *
170
 * @return void
171
 */
172
        public function testExpiry() {
173
                Cache::set(array('duration' => 1), 'redis');
174
175
                $result = Cache::read('test', 'redis');
176
                $this->assertFalse($result);
177
178
                $data = 'this is a test of the emergency broadcasting system';
179
                $result = Cache::write('other_test', $data, 'redis');
180
                $this->assertTrue($result);
181
182
                sleep(2);
183
                $result = Cache::read('other_test', 'redis');
184
                $this->assertFalse($result);
185
186
                Cache::set(array('duration' => "+1 second"), 'redis');
187
188
                $data = 'this is a test of the emergency broadcasting system';
189
                $result = Cache::write('other_test', $data, 'redis');
190
                $this->assertTrue($result);
191
192
                sleep(2);
193
                $result = Cache::read('other_test', 'redis');
194
                $this->assertFalse($result);
195
196
                Cache::config('redis', array('duration' => '+1 second'));
197
                sleep(2);
198
199
                $result = Cache::read('other_test', 'redis');
200
                $this->assertFalse($result);
201
202
                Cache::config('redis', array('duration' => '+29 days'));
203
                $data = 'this is a test of the emergency broadcasting system';
204
                $result = Cache::write('long_expiry_test', $data, 'redis');
205
                $this->assertTrue($result);
206
207
                sleep(2);
208
                $result = Cache::read('long_expiry_test', 'redis');
209
                $expecting = $data;
210
                $this->assertEquals($expecting, $result);
211
212
                Cache::config('redis', array('duration' => 3600));
213
        }
214
215
/**
216
 * testDeleteCache method
217
 *
218
 * @return void
219
 */
220
        public function testDeleteCache() {
221
                $data = 'this is a test of the emergency broadcasting system';
222
                $result = Cache::write('delete_test', $data, 'redis');
223
                $this->assertTrue($result);
224
225
                $result = Cache::delete('delete_test', 'redis');
226
                $this->assertTrue($result);
227
        }
228
229
/**
230
 * testDecrement method
231
 *
232
 * @return void
233
 */
234
        public function testDecrement() {
235
                Cache::delete('test_decrement', 'redis');
236
                $result = Cache::write('test_decrement', 5, 'redis');
237
                $this->assertTrue($result);
238
239
                $result = Cache::decrement('test_decrement', 1, 'redis');
240
                $this->assertEquals(4, $result);
241
242
                $result = Cache::read('test_decrement', 'redis');
243
                $this->assertEquals(4, $result);
244
245
                $result = Cache::decrement('test_decrement', 2, 'redis');
246
                $this->assertEquals(2, $result);
247
248
                $result = Cache::read('test_decrement', 'redis');
249
                $this->assertEquals(2, $result);
250
        }
251
252
/**
253
 * testIncrement method
254
 *
255
 * @return void
256
 */
257
        public function testIncrement() {
258
                Cache::delete('test_increment', 'redis');
259
                $result = Cache::increment('test_increment', 1, 'redis');
260
                $this->assertEquals(1, $result);
261
262
                $result = Cache::read('test_increment', 'redis');
263
                $this->assertEquals(1, $result);
264
265
                $result = Cache::increment('test_increment', 2, 'redis');
266
                $this->assertEquals(3, $result);
267
268
                $result = Cache::read('test_increment', 'redis');
269
                $this->assertEquals(3, $result);
270
        }
271
272
/**
273
 * test clearing redis.
274
 *
275
 * @return void
276
 */
277
        public function testClear() {
278
                Cache::config('redis2', array(
279
                        'engine' => 'Redis',
280
                        'prefix' => 'cake2_',
281
                        'duration' => 3600
282
                ));
283
284
                Cache::write('some_value', 'cache1', 'redis');
285
                $result = Cache::clear(true, 'redis');
286
                $this->assertTrue($result);
287
                $this->assertEquals('cache1', Cache::read('some_value', 'redis'));
288
289
                Cache::write('some_value', 'cache2', 'redis2');
290
                $result = Cache::clear(false, 'redis');
291
                $this->assertTrue($result);
292
                $this->assertFalse(Cache::read('some_value', 'redis'));
293
                $this->assertEquals('cache2', Cache::read('some_value', 'redis2'));
294
295
                Cache::clear(false, 'redis2');
296
        }
297
298
/**
299
 * test that a 0 duration can successfully write.
300
 *
301
 * @return void
302
 */
303
        public function testZeroDuration() {
304
                Cache::config('redis', array('duration' => 0));
305
                $result = Cache::write('test_key', 'written!', 'redis');
306
307
                $this->assertTrue($result);
308
                $result = Cache::read('test_key', 'redis');
309
                $this->assertEquals('written!', $result);
310
        }
311
312
/**
313
 * Tests that configuring groups for stored keys return the correct values when read/written
314
 * Shows that altering the group value is equivalent to deleting all keys under the same
315
 * group
316
 *
317
 * @return void
318
 */
319
        public function testGroupReadWrite() {
320
                Cache::config('redis_groups', array(
321
                        'engine' => 'Redis',
322
                        'duration' => 3600,
323
                        'groups' => array('group_a', 'group_b'),
324
                        'prefix' => 'test_'
325
                ));
326
                Cache::config('redis_helper', array(
327
                        'engine' => 'Redis',
328
                        'duration' => 3600,
329
                        'prefix' => 'test_'
330
                ));
331
                $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
332
                $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
333
334
                Cache::increment('group_a', 1, 'redis_helper');
335
                $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
336
                $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
337
                $this->assertEquals('value2', Cache::read('test_groups', 'redis_groups'));
338
339
                Cache::increment('group_b', 1, 'redis_helper');
340
                $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
341
                $this->assertTrue(Cache::write('test_groups', 'value3', 'redis_groups'));
342
                $this->assertEquals('value3', Cache::read('test_groups', 'redis_groups'));
343
        }
344
345
/**
346
 * Tests that deleteing from a groups-enabled config is possible
347
 *
348
 * @return void
349
 */
350
        public function testGroupDelete() {
351
                Cache::config('redis_groups', array(
352
                        'engine' => 'Redis',
353
                        'duration' => 3600,
354
                        'groups' => array('group_a', 'group_b')
355
                ));
356
                $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
357
                $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
358
                $this->assertTrue(Cache::delete('test_groups', 'redis_groups'));
359
360
                $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
361
        }
362
363
/**
364
 * Test clearing a cache group
365
 *
366
 * @return void
367
 */
368
        public function testGroupClear() {
369
                Cache::config('redis_groups', array(
370
                        'engine' => 'Redis',
371
                        'duration' => 3600,
372
                        'groups' => array('group_a', 'group_b')
373
                ));
374
375
                $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
376
                $this->assertTrue(Cache::clearGroup('group_a', 'redis_groups'));
377
                $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
378
379
                $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
380
                $this->assertTrue(Cache::clearGroup('group_b', 'redis_groups'));
381
                $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
382
        }
383
384
}