RedisEngineTest.php 12 KB

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