RedisEngineTest.php 18 KB

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