RedisEngineTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  12. * @since 2.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Cache\Engine;
  16. use Cake\Cache\Cache;
  17. use Cake\Cache\Engine\RedisEngine;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * RedisEngineTest class
  21. */
  22. class RedisEngineTest extends TestCase
  23. {
  24. /**
  25. * setUp method
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $this->skipIf(!class_exists('Redis'), 'Redis extension is not installed or configured properly.');
  33. $this->skipIf(version_compare(PHP_VERSION, '7.2.0dev', '>='), 'Redis is misbehaving in PHP7.2');
  34. // @codingStandardsIgnoreStart
  35. $socket = @fsockopen('127.0.0.1', 6379, $errno, $errstr, 1);
  36. // @codingStandardsIgnoreEnd
  37. $this->skipIf(!$socket, 'Redis is not running.');
  38. fclose($socket);
  39. Cache::enable();
  40. $this->_configCache();
  41. }
  42. /**
  43. * tearDown method
  44. *
  45. * @return void
  46. */
  47. public function tearDown()
  48. {
  49. parent::tearDown();
  50. Cache::drop('redis');
  51. Cache::drop('redis_groups');
  52. Cache::drop('redis_helper');
  53. }
  54. /**
  55. * Helper method for testing.
  56. *
  57. * @param array $config
  58. * @return void
  59. */
  60. protected function _configCache($config = [])
  61. {
  62. $defaults = [
  63. 'className' => 'Redis',
  64. 'prefix' => 'cake_',
  65. 'duration' => 3600
  66. ];
  67. Cache::drop('redis');
  68. Cache::setConfig('redis', array_merge($defaults, $config));
  69. }
  70. /**
  71. * testConfig method
  72. *
  73. * @return void
  74. */
  75. public function testConfig()
  76. {
  77. $config = Cache::engine('redis')->getConfig();
  78. $expecting = [
  79. 'prefix' => 'cake_',
  80. 'duration' => 3600,
  81. 'probability' => 100,
  82. 'groups' => [],
  83. 'server' => '127.0.0.1',
  84. 'port' => 6379,
  85. 'timeout' => 0,
  86. 'persistent' => true,
  87. 'password' => false,
  88. 'database' => 0,
  89. 'unix_socket' => false,
  90. 'host' => null,
  91. ];
  92. $this->assertEquals($expecting, $config);
  93. }
  94. /**
  95. * testConfigDsn method
  96. *
  97. * @return void
  98. */
  99. public function testConfigDsn()
  100. {
  101. Cache::setConfig('redis_dsn', [
  102. 'url' => 'redis://localhost:6379?database=1&prefix=redis_'
  103. ]);
  104. $config = Cache::engine('redis_dsn')->getConfig();
  105. $expecting = [
  106. 'prefix' => 'redis_',
  107. 'duration' => 3600,
  108. 'probability' => 100,
  109. 'groups' => [],
  110. 'server' => 'localhost',
  111. 'port' => 6379,
  112. 'timeout' => 0,
  113. 'persistent' => true,
  114. 'password' => false,
  115. 'database' => '1',
  116. 'unix_socket' => false,
  117. 'host' => 'localhost',
  118. 'scheme' => 'redis',
  119. ];
  120. $this->assertEquals($expecting, $config);
  121. Cache::drop('redis_dsn');
  122. }
  123. /**
  124. * testConnect method
  125. *
  126. * @return void
  127. */
  128. public function testConnect()
  129. {
  130. $Redis = new RedisEngine();
  131. $this->assertTrue($Redis->init(Cache::engine('redis')->getConfig()));
  132. }
  133. /**
  134. * testMultiDatabaseOperations method
  135. *
  136. * @return void
  137. */
  138. public function testMultiDatabaseOperations()
  139. {
  140. Cache::setConfig('redisdb0', [
  141. 'engine' => 'Redis',
  142. 'prefix' => 'cake2_',
  143. 'duration' => 3600,
  144. 'persistent' => false,
  145. ]);
  146. Cache::setConfig('redisdb1', [
  147. 'engine' => 'Redis',
  148. 'database' => 1,
  149. 'prefix' => 'cake2_',
  150. 'duration' => 3600,
  151. 'persistent' => false,
  152. ]);
  153. $result = Cache::write('save_in_0', true, 'redisdb0');
  154. $exist = Cache::read('save_in_0', 'redisdb0');
  155. $this->assertTrue($result);
  156. $this->assertTrue($exist);
  157. $result = Cache::write('save_in_1', true, 'redisdb1');
  158. $this->assertTrue($result);
  159. $exist = Cache::read('save_in_0', 'redisdb1');
  160. $this->assertFalse($exist);
  161. $exist = Cache::read('save_in_1', 'redisdb1');
  162. $this->assertTrue($exist);
  163. Cache::delete('save_in_0', 'redisdb0');
  164. $exist = Cache::read('save_in_0', 'redisdb0');
  165. $this->assertFalse($exist);
  166. Cache::delete('save_in_1', 'redisdb1');
  167. $exist = Cache::read('save_in_1', 'redisdb1');
  168. $this->assertFalse($exist);
  169. Cache::drop('redisdb0');
  170. Cache::drop('redisdb1');
  171. }
  172. /**
  173. * test write numbers method
  174. *
  175. * @return void
  176. */
  177. public function testWriteNumbers()
  178. {
  179. $result = Cache::write('test-counter', 1, 'redis');
  180. $this->assertSame(1, Cache::read('test-counter', 'redis'));
  181. $result = Cache::write('test-counter', 0, 'redis');
  182. $this->assertSame(0, Cache::read('test-counter', 'redis'));
  183. $result = Cache::write('test-counter', -1, 'redis');
  184. $this->assertSame(-1, Cache::read('test-counter', 'redis'));
  185. }
  186. /**
  187. * testReadAndWriteCache method
  188. *
  189. * @return void
  190. */
  191. public function testReadAndWriteCache()
  192. {
  193. $this->_configCache(['duration' => 1]);
  194. $result = Cache::read('test', 'redis');
  195. $expecting = '';
  196. $this->assertEquals($expecting, $result);
  197. $data = 'this is a test of the emergency broadcasting system';
  198. $result = Cache::write('test', $data, 'redis');
  199. $this->assertTrue($result);
  200. $result = Cache::read('test', 'redis');
  201. $expecting = $data;
  202. $this->assertEquals($expecting, $result);
  203. $data = [1, 2, 3];
  204. $this->assertTrue(Cache::write('array_data', $data, 'redis'));
  205. $this->assertEquals($data, Cache::read('array_data', 'redis'));
  206. Cache::delete('test', 'redis');
  207. }
  208. /**
  209. * testExpiry method
  210. *
  211. * @return void
  212. */
  213. public function testExpiry()
  214. {
  215. $this->_configCache(['duration' => 1]);
  216. $result = Cache::read('test', 'redis');
  217. $this->assertFalse($result);
  218. $data = 'this is a test of the emergency broadcasting system';
  219. $result = Cache::write('other_test', $data, 'redis');
  220. $this->assertTrue($result);
  221. sleep(2);
  222. $result = Cache::read('other_test', 'redis');
  223. $this->assertFalse($result);
  224. $this->_configCache(['duration' => '+1 second']);
  225. $data = 'this is a test of the emergency broadcasting system';
  226. $result = Cache::write('other_test', $data, 'redis');
  227. $this->assertTrue($result);
  228. sleep(2);
  229. $result = Cache::read('other_test', 'redis');
  230. $this->assertFalse($result);
  231. sleep(2);
  232. $result = Cache::read('other_test', 'redis');
  233. $this->assertFalse($result);
  234. $this->_configCache(['duration' => '+29 days']);
  235. $data = 'this is a test of the emergency broadcasting system';
  236. $result = Cache::write('long_expiry_test', $data, 'redis');
  237. $this->assertTrue($result);
  238. sleep(2);
  239. $result = Cache::read('long_expiry_test', 'redis');
  240. $expecting = $data;
  241. $this->assertEquals($expecting, $result);
  242. }
  243. /**
  244. * testDeleteCache method
  245. *
  246. * @return void
  247. */
  248. public function testDeleteCache()
  249. {
  250. $data = 'this is a test of the emergency broadcasting system';
  251. $result = Cache::write('delete_test', $data, 'redis');
  252. $this->assertTrue($result);
  253. $result = Cache::delete('delete_test', 'redis');
  254. $this->assertTrue($result);
  255. }
  256. /**
  257. * testDecrement method
  258. *
  259. * @return void
  260. */
  261. public function testDecrement()
  262. {
  263. Cache::delete('test_decrement', 'redis');
  264. $result = Cache::write('test_decrement', 5, 'redis');
  265. $this->assertTrue($result);
  266. $result = Cache::decrement('test_decrement', 1, 'redis');
  267. $this->assertEquals(4, $result);
  268. $result = Cache::read('test_decrement', 'redis');
  269. $this->assertEquals(4, $result);
  270. $result = Cache::decrement('test_decrement', 2, 'redis');
  271. $this->assertEquals(2, $result);
  272. $result = Cache::read('test_decrement', 'redis');
  273. $this->assertEquals(2, $result);
  274. }
  275. /**
  276. * testIncrement method
  277. *
  278. * @return void
  279. */
  280. public function testIncrement()
  281. {
  282. Cache::delete('test_increment', 'redis');
  283. $result = Cache::increment('test_increment', 1, 'redis');
  284. $this->assertEquals(1, $result);
  285. $result = Cache::read('test_increment', 'redis');
  286. $this->assertEquals(1, $result);
  287. $result = Cache::increment('test_increment', 2, 'redis');
  288. $this->assertEquals(3, $result);
  289. $result = Cache::read('test_increment', 'redis');
  290. $this->assertEquals(3, $result);
  291. }
  292. /**
  293. * Test that increment() and decrement() can live forever.
  294. *
  295. * @return void
  296. */
  297. public function testIncrementDecrementForvever()
  298. {
  299. $this->_configCache(['duration' => 0]);
  300. Cache::delete('test_increment', 'redis');
  301. Cache::delete('test_decrement', 'redis');
  302. $result = Cache::increment('test_increment', 1, 'redis');
  303. $this->assertEquals(1, $result);
  304. $result = Cache::decrement('test_decrement', 1, 'redis');
  305. $this->assertEquals(-1, $result);
  306. $this->assertEquals(1, Cache::read('test_increment', 'redis'));
  307. $this->assertEquals(-1, Cache::read('test_decrement', 'redis'));
  308. }
  309. /**
  310. * Test that increment and decrement set ttls.
  311. *
  312. * @return void
  313. */
  314. public function testIncrementDecrementExpiring()
  315. {
  316. $this->_configCache(['duration' => 1]);
  317. Cache::delete('test_increment', 'redis');
  318. Cache::delete('test_decrement', 'redis');
  319. $this->assertSame(1, Cache::increment('test_increment', 1, 'redis'));
  320. $this->assertSame(-1, Cache::decrement('test_decrement', 1, 'redis'));
  321. sleep(2);
  322. $this->assertFalse(Cache::read('test_increment', 'redis'));
  323. $this->assertFalse(Cache::read('test_decrement', 'redis'));
  324. }
  325. /**
  326. * test clearing redis.
  327. *
  328. * @return void
  329. */
  330. public function testClear()
  331. {
  332. Cache::setConfig('redis2', [
  333. 'engine' => 'Redis',
  334. 'prefix' => 'cake2_',
  335. 'duration' => 3600
  336. ]);
  337. Cache::write('some_value', 'cache1', 'redis');
  338. $result = Cache::clear(true, 'redis');
  339. $this->assertTrue($result);
  340. $this->assertEquals('cache1', Cache::read('some_value', 'redis'));
  341. Cache::write('some_value', 'cache2', 'redis2');
  342. $result = Cache::clear(false, 'redis');
  343. $this->assertTrue($result);
  344. $this->assertFalse(Cache::read('some_value', 'redis'));
  345. $this->assertEquals('cache2', Cache::read('some_value', 'redis2'));
  346. Cache::clear(false, 'redis2');
  347. }
  348. /**
  349. * test that a 0 duration can successfully write.
  350. *
  351. * @return void
  352. */
  353. public function testZeroDuration()
  354. {
  355. $this->_configCache(['duration' => 0]);
  356. $result = Cache::write('test_key', 'written!', 'redis');
  357. $this->assertTrue($result);
  358. $result = Cache::read('test_key', 'redis');
  359. $this->assertEquals('written!', $result);
  360. }
  361. /**
  362. * Tests that configuring groups for stored keys return the correct values when read/written
  363. * Shows that altering the group value is equivalent to deleting all keys under the same
  364. * group
  365. *
  366. * @return void
  367. */
  368. public function testGroupReadWrite()
  369. {
  370. Cache::setConfig('redis_groups', [
  371. 'engine' => 'Redis',
  372. 'duration' => 3600,
  373. 'groups' => ['group_a', 'group_b'],
  374. 'prefix' => 'test_'
  375. ]);
  376. Cache::setConfig('redis_helper', [
  377. 'engine' => 'Redis',
  378. 'duration' => 3600,
  379. 'prefix' => 'test_'
  380. ]);
  381. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  382. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  383. Cache::increment('group_a', 1, 'redis_helper');
  384. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  385. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  386. $this->assertEquals('value2', Cache::read('test_groups', 'redis_groups'));
  387. Cache::increment('group_b', 1, 'redis_helper');
  388. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  389. $this->assertTrue(Cache::write('test_groups', 'value3', 'redis_groups'));
  390. $this->assertEquals('value3', Cache::read('test_groups', 'redis_groups'));
  391. }
  392. /**
  393. * Tests that deleting from a groups-enabled config is possible
  394. *
  395. * @return void
  396. */
  397. public function testGroupDelete()
  398. {
  399. Cache::setConfig('redis_groups', [
  400. 'engine' => 'Redis',
  401. 'duration' => 3600,
  402. 'groups' => ['group_a', 'group_b']
  403. ]);
  404. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  405. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  406. $this->assertTrue(Cache::delete('test_groups', 'redis_groups'));
  407. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  408. }
  409. /**
  410. * Test clearing a cache group
  411. *
  412. * @return void
  413. */
  414. public function testGroupClear()
  415. {
  416. Cache::setConfig('redis_groups', [
  417. 'engine' => 'Redis',
  418. 'duration' => 3600,
  419. 'groups' => ['group_a', 'group_b']
  420. ]);
  421. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  422. $this->assertTrue(Cache::clearGroup('group_a', 'redis_groups'));
  423. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  424. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  425. $this->assertTrue(Cache::clearGroup('group_b', 'redis_groups'));
  426. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  427. }
  428. /**
  429. * Test add
  430. *
  431. * @return void
  432. */
  433. public function testAdd()
  434. {
  435. Cache::delete('test_add_key', 'redis');
  436. $result = Cache::add('test_add_key', 'test data', 'redis');
  437. $this->assertTrue($result);
  438. $expected = 'test data';
  439. $result = Cache::read('test_add_key', 'redis');
  440. $this->assertEquals($expected, $result);
  441. $result = Cache::add('test_add_key', 'test data 2', 'redis');
  442. $this->assertFalse($result);
  443. }
  444. }