RedisEngineTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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\TestSuite\TestCase;
  21. /**
  22. * RedisEngineTest class
  23. */
  24. class RedisEngineTest extends TestCase
  25. {
  26. /**
  27. * setUp method
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. $this->skipIf(!class_exists('Redis'), 'Redis extension is not installed or configured properly.');
  35. // @codingStandardsIgnoreStart
  36. $socket = @fsockopen('127.0.0.1', 6379, $errno, $errstr, 1);
  37. // @codingStandardsIgnoreEnd
  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. * testConfigDsn method
  97. *
  98. * @return void
  99. */
  100. public function testConfigDsn()
  101. {
  102. Cache::config('redis_dsn', [
  103. 'url' => 'redis://localhost:6379?database=1&prefix=redis_'
  104. ]);
  105. $config = Cache::engine('redis_dsn')->config();
  106. $expecting = [
  107. 'prefix' => 'redis_',
  108. 'duration' => 3600,
  109. 'probability' => 100,
  110. 'groups' => [],
  111. 'server' => 'localhost',
  112. 'port' => 6379,
  113. 'timeout' => 0,
  114. 'persistent' => true,
  115. 'password' => false,
  116. 'database' => '1',
  117. 'unix_socket' => false,
  118. 'host' => 'localhost',
  119. 'scheme' => 'redis',
  120. ];
  121. $this->assertEquals($expecting, $config);
  122. Cache::drop('redis_dsn');
  123. }
  124. /**
  125. * testConnect method
  126. *
  127. * @return void
  128. */
  129. public function testConnect()
  130. {
  131. $Redis = new RedisEngine();
  132. $this->assertTrue($Redis->init(Cache::engine('redis')->config()));
  133. }
  134. /**
  135. * testMultiDatabaseOperations method
  136. *
  137. * @return void
  138. */
  139. public function testMultiDatabaseOperations()
  140. {
  141. Cache::config('redisdb0', [
  142. 'engine' => 'Redis',
  143. 'prefix' => 'cake2_',
  144. 'duration' => 3600,
  145. 'persistent' => false,
  146. ]);
  147. Cache::config('redisdb1', [
  148. 'engine' => 'Redis',
  149. 'database' => 1,
  150. 'prefix' => 'cake2_',
  151. 'duration' => 3600,
  152. 'persistent' => false,
  153. ]);
  154. $result = Cache::write('save_in_0', true, 'redisdb0');
  155. $exist = Cache::read('save_in_0', 'redisdb0');
  156. $this->assertTrue($result);
  157. $this->assertTrue($exist);
  158. $result = Cache::write('save_in_1', true, 'redisdb1');
  159. $this->assertTrue($result);
  160. $exist = Cache::read('save_in_0', 'redisdb1');
  161. $this->assertFalse($exist);
  162. $exist = Cache::read('save_in_1', 'redisdb1');
  163. $this->assertTrue($exist);
  164. Cache::delete('save_in_0', 'redisdb0');
  165. $exist = Cache::read('save_in_0', 'redisdb0');
  166. $this->assertFalse($exist);
  167. Cache::delete('save_in_1', 'redisdb1');
  168. $exist = Cache::read('save_in_1', 'redisdb1');
  169. $this->assertFalse($exist);
  170. Cache::drop('redisdb0');
  171. Cache::drop('redisdb1');
  172. }
  173. /**
  174. * test write numbers method
  175. *
  176. * @return void
  177. */
  178. public function testWriteNumbers()
  179. {
  180. $result = Cache::write('test-counter', 1, 'redis');
  181. $this->assertSame(1, Cache::read('test-counter', 'redis'));
  182. $result = Cache::write('test-counter', 0, 'redis');
  183. $this->assertSame(0, Cache::read('test-counter', 'redis'));
  184. $result = Cache::write('test-counter', -1, 'redis');
  185. $this->assertSame(-1, Cache::read('test-counter', 'redis'));
  186. }
  187. /**
  188. * testReadAndWriteCache method
  189. *
  190. * @return void
  191. */
  192. public function testReadAndWriteCache()
  193. {
  194. $this->_configCache(['duration' => 1]);
  195. $result = Cache::read('test', 'redis');
  196. $expecting = '';
  197. $this->assertEquals($expecting, $result);
  198. $data = 'this is a test of the emergency broadcasting system';
  199. $result = Cache::write('test', $data, 'redis');
  200. $this->assertTrue($result);
  201. $result = Cache::read('test', 'redis');
  202. $expecting = $data;
  203. $this->assertEquals($expecting, $result);
  204. $data = [1, 2, 3];
  205. $this->assertTrue(Cache::write('array_data', $data, 'redis'));
  206. $this->assertEquals($data, Cache::read('array_data', 'redis'));
  207. Cache::delete('test', 'redis');
  208. }
  209. /**
  210. * testExpiry method
  211. *
  212. * @return void
  213. */
  214. public function testExpiry()
  215. {
  216. $this->_configCache(['duration' => 1]);
  217. $result = Cache::read('test', 'redis');
  218. $this->assertFalse($result);
  219. $data = 'this is a test of the emergency broadcasting system';
  220. $result = Cache::write('other_test', $data, 'redis');
  221. $this->assertTrue($result);
  222. sleep(2);
  223. $result = Cache::read('other_test', 'redis');
  224. $this->assertFalse($result);
  225. $this->_configCache(['duration' => '+1 second']);
  226. $data = 'this is a test of the emergency broadcasting system';
  227. $result = Cache::write('other_test', $data, 'redis');
  228. $this->assertTrue($result);
  229. sleep(2);
  230. $result = Cache::read('other_test', 'redis');
  231. $this->assertFalse($result);
  232. sleep(2);
  233. $result = Cache::read('other_test', 'redis');
  234. $this->assertFalse($result);
  235. $this->_configCache(['duration' => '+29 days']);
  236. $data = 'this is a test of the emergency broadcasting system';
  237. $result = Cache::write('long_expiry_test', $data, 'redis');
  238. $this->assertTrue($result);
  239. sleep(2);
  240. $result = Cache::read('long_expiry_test', 'redis');
  241. $expecting = $data;
  242. $this->assertEquals($expecting, $result);
  243. }
  244. /**
  245. * testDeleteCache method
  246. *
  247. * @return void
  248. */
  249. public function testDeleteCache()
  250. {
  251. $data = 'this is a test of the emergency broadcasting system';
  252. $result = Cache::write('delete_test', $data, 'redis');
  253. $this->assertTrue($result);
  254. $result = Cache::delete('delete_test', 'redis');
  255. $this->assertTrue($result);
  256. }
  257. /**
  258. * testDecrement method
  259. *
  260. * @return void
  261. */
  262. public function testDecrement()
  263. {
  264. Cache::delete('test_decrement', 'redis');
  265. $result = Cache::write('test_decrement', 5, 'redis');
  266. $this->assertTrue($result);
  267. $result = Cache::decrement('test_decrement', 1, 'redis');
  268. $this->assertEquals(4, $result);
  269. $result = Cache::read('test_decrement', 'redis');
  270. $this->assertEquals(4, $result);
  271. $result = Cache::decrement('test_decrement', 2, 'redis');
  272. $this->assertEquals(2, $result);
  273. $result = Cache::read('test_decrement', 'redis');
  274. $this->assertEquals(2, $result);
  275. }
  276. /**
  277. * testIncrement method
  278. *
  279. * @return void
  280. */
  281. public function testIncrement()
  282. {
  283. Cache::delete('test_increment', 'redis');
  284. $result = Cache::increment('test_increment', 1, 'redis');
  285. $this->assertEquals(1, $result);
  286. $result = Cache::read('test_increment', 'redis');
  287. $this->assertEquals(1, $result);
  288. $result = Cache::increment('test_increment', 2, 'redis');
  289. $this->assertEquals(3, $result);
  290. $result = Cache::read('test_increment', 'redis');
  291. $this->assertEquals(3, $result);
  292. }
  293. /**
  294. * test clearing redis.
  295. *
  296. * @return void
  297. */
  298. public function testClear()
  299. {
  300. Cache::config('redis2', [
  301. 'engine' => 'Redis',
  302. 'prefix' => 'cake2_',
  303. 'duration' => 3600
  304. ]);
  305. Cache::write('some_value', 'cache1', 'redis');
  306. $result = Cache::clear(true, 'redis');
  307. $this->assertTrue($result);
  308. $this->assertEquals('cache1', Cache::read('some_value', 'redis'));
  309. Cache::write('some_value', 'cache2', 'redis2');
  310. $result = Cache::clear(false, 'redis');
  311. $this->assertTrue($result);
  312. $this->assertFalse(Cache::read('some_value', 'redis'));
  313. $this->assertEquals('cache2', Cache::read('some_value', 'redis2'));
  314. Cache::clear(false, 'redis2');
  315. }
  316. /**
  317. * test that a 0 duration can successfully write.
  318. *
  319. * @return void
  320. */
  321. public function testZeroDuration()
  322. {
  323. $this->_configCache(['duration' => 0]);
  324. $result = Cache::write('test_key', 'written!', 'redis');
  325. $this->assertTrue($result);
  326. $result = Cache::read('test_key', 'redis');
  327. $this->assertEquals('written!', $result);
  328. }
  329. /**
  330. * Tests that configuring groups for stored keys return the correct values when read/written
  331. * Shows that altering the group value is equivalent to deleting all keys under the same
  332. * group
  333. *
  334. * @return void
  335. */
  336. public function testGroupReadWrite()
  337. {
  338. Cache::config('redis_groups', [
  339. 'engine' => 'Redis',
  340. 'duration' => 3600,
  341. 'groups' => ['group_a', 'group_b'],
  342. 'prefix' => 'test_'
  343. ]);
  344. Cache::config('redis_helper', [
  345. 'engine' => 'Redis',
  346. 'duration' => 3600,
  347. 'prefix' => 'test_'
  348. ]);
  349. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  350. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  351. Cache::increment('group_a', 1, 'redis_helper');
  352. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  353. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  354. $this->assertEquals('value2', Cache::read('test_groups', 'redis_groups'));
  355. Cache::increment('group_b', 1, 'redis_helper');
  356. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  357. $this->assertTrue(Cache::write('test_groups', 'value3', 'redis_groups'));
  358. $this->assertEquals('value3', Cache::read('test_groups', 'redis_groups'));
  359. }
  360. /**
  361. * Tests that deleting from a groups-enabled config is possible
  362. *
  363. * @return void
  364. */
  365. public function testGroupDelete()
  366. {
  367. Cache::config('redis_groups', [
  368. 'engine' => 'Redis',
  369. 'duration' => 3600,
  370. 'groups' => ['group_a', 'group_b']
  371. ]);
  372. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  373. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  374. $this->assertTrue(Cache::delete('test_groups', 'redis_groups'));
  375. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  376. }
  377. /**
  378. * Test clearing a cache group
  379. *
  380. * @return void
  381. */
  382. public function testGroupClear()
  383. {
  384. Cache::config('redis_groups', [
  385. 'engine' => 'Redis',
  386. 'duration' => 3600,
  387. 'groups' => ['group_a', 'group_b']
  388. ]);
  389. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  390. $this->assertTrue(Cache::clearGroup('group_a', 'redis_groups'));
  391. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  392. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  393. $this->assertTrue(Cache::clearGroup('group_b', 'redis_groups'));
  394. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  395. }
  396. /**
  397. * Test add
  398. *
  399. * @return void
  400. */
  401. public function testAdd()
  402. {
  403. Cache::delete('test_add_key', 'redis');
  404. $result = Cache::add('test_add_key', 'test data', 'redis');
  405. $this->assertTrue($result);
  406. $expected = 'test data';
  407. $result = Cache::read('test_add_key', 'redis');
  408. $this->assertEquals($expected, $result);
  409. $result = Cache::add('test_add_key', 'test data 2', 'redis');
  410. $this->assertFalse($result);
  411. }
  412. }