RedisEngineTest.php 12 KB

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