RedisEngineTest.php 10.0 KB

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