RedisEngineTest.php 12 KB

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