RedisEngineTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. use Redis;
  22. use function Cake\Core\env;
  23. /**
  24. * RedisEngineTest class
  25. */
  26. class RedisEngineTest extends TestCase
  27. {
  28. /**
  29. * @var string
  30. */
  31. protected $port = '6379';
  32. /**
  33. * setUp method
  34. */
  35. public function setUp(): void
  36. {
  37. parent::setUp();
  38. $this->skipIf(!class_exists('Redis'), 'Redis extension is not installed or configured properly.');
  39. $this->skipIf(PHP_VERSION_ID >= 80400, 'redis.io currrently generates an error on PHP 8.4');
  40. $this->port = env('REDIS_PORT', $this->port);
  41. // phpcs:disable
  42. $socket = @fsockopen('127.0.0.1', (int)$this->port, $errno, $errstr, 1);
  43. // phpcs:enable
  44. $this->skipIf(!$socket, 'Redis is not running.');
  45. fclose($socket);
  46. Cache::enable();
  47. $this->_configCache();
  48. }
  49. /**
  50. * tearDown method
  51. */
  52. public function tearDown(): void
  53. {
  54. parent::tearDown();
  55. Cache::drop('redis');
  56. Cache::drop('redis_groups');
  57. Cache::drop('redis_helper');
  58. }
  59. /**
  60. * Helper method for testing.
  61. *
  62. * @param array $config
  63. */
  64. protected function _configCache($config = []): void
  65. {
  66. $defaults = [
  67. 'className' => 'Redis',
  68. 'prefix' => 'cake_',
  69. 'duration' => 3600,
  70. 'port' => $this->port,
  71. ];
  72. Cache::drop('redis');
  73. Cache::setConfig('redis', array_merge($defaults, $config));
  74. }
  75. /**
  76. * testConfig method
  77. */
  78. public function testConfig(): void
  79. {
  80. $config = Cache::pool('redis')->getConfig();
  81. $expecting = [
  82. 'prefix' => 'cake_',
  83. 'duration' => 3600,
  84. 'groups' => [],
  85. 'server' => '127.0.0.1',
  86. 'port' => $this->port,
  87. 'tls' => false,
  88. 'timeout' => 0,
  89. 'persistent' => true,
  90. 'password' => false,
  91. 'database' => 0,
  92. 'unix_socket' => false,
  93. 'host' => null,
  94. 'scanCount' => 10,
  95. ];
  96. $this->assertEquals($expecting, $config);
  97. }
  98. /**
  99. * testConfigDsn method
  100. */
  101. public function testConfigDsn(): void
  102. {
  103. Cache::setConfig('redis_dsn', [
  104. 'url' => 'redis://localhost:' . $this->port . '?database=1&prefix=redis_',
  105. ]);
  106. $config = Cache::pool('redis_dsn')->getConfig();
  107. $expecting = [
  108. 'prefix' => 'redis_',
  109. 'duration' => 3600,
  110. 'groups' => [],
  111. 'server' => 'localhost',
  112. 'port' => $this->port,
  113. 'tls' => false,
  114. 'timeout' => 0,
  115. 'persistent' => true,
  116. 'password' => false,
  117. 'database' => '1',
  118. 'unix_socket' => false,
  119. 'host' => 'localhost',
  120. 'scheme' => 'redis',
  121. 'scanCount' => 10,
  122. ];
  123. $this->assertEquals($expecting, $config);
  124. Cache::drop('redis_dsn');
  125. }
  126. /**
  127. * testConfigDsnSSLContext method
  128. */
  129. public function testConfigDsnSSLContext(): void
  130. {
  131. $url = 'redis://localhost:' . $this->port;
  132. $url .= '?ssl_ca=/tmp/cert.crt';
  133. $url .= '&ssl_key=/tmp/local.key';
  134. $url .= '&ssl_cert=/tmp/local.crt';
  135. Cache::setConfig('redis_dsn', compact('url'));
  136. $config = Cache::pool('redis_dsn')->getConfig();
  137. $expecting = [
  138. 'prefix' => 'cake_',
  139. 'duration' => 3600,
  140. 'groups' => [],
  141. 'server' => 'localhost',
  142. 'port' => $this->port,
  143. 'tls' => false,
  144. 'timeout' => 0,
  145. 'persistent' => true,
  146. 'password' => false,
  147. 'database' => 0,
  148. 'unix_socket' => false,
  149. 'host' => 'localhost',
  150. 'scheme' => 'redis',
  151. 'scanCount' => 10,
  152. 'ssl_ca' => '/tmp/cert.crt',
  153. 'ssl_key' => '/tmp/local.key',
  154. 'ssl_cert' => '/tmp/local.crt',
  155. ];
  156. $this->assertEquals($expecting, $config);
  157. Cache::drop('redis_dsn');
  158. }
  159. /**
  160. * testConnect method
  161. */
  162. public function testConnect(): void
  163. {
  164. $Redis = new RedisEngine();
  165. $this->assertTrue($Redis->init(Cache::pool('redis')->getConfig()));
  166. }
  167. /**
  168. * testConnectTransient method
  169. */
  170. public function testConnectTransient(): void
  171. {
  172. $Redis = $this->createPartialMock(RedisEngine::class, ['_createRedisInstance']);
  173. $phpredis = $this->createMock(Redis::class);
  174. $phpredis->expects($this->once())
  175. ->method('select')
  176. ->with((int)$Redis->getConfig('database'))
  177. ->willReturn(true);
  178. $phpredis->expects($this->once())
  179. ->method('connect')
  180. ->with(
  181. $Redis->getConfig('server'),
  182. (int)$this->port,
  183. (int)$Redis->getConfig('timeout'),
  184. )
  185. ->willReturn(true);
  186. $Redis->expects($this->once())
  187. ->method('_createRedisInstance')
  188. ->willReturn($phpredis);
  189. $config = [
  190. 'port' => $this->port,
  191. 'persistent' => false,
  192. ];
  193. $this->assertTrue($Redis->init($config + Cache::pool('redis')->getConfig()));
  194. $Redis = $this->createPartialMock(RedisEngine::class, ['_createRedisInstance']);
  195. $phpredis = $this->createMock(Redis::class);
  196. $phpredis->expects($this->once())
  197. ->method('select')
  198. ->with((int)$Redis->getConfig('database'))
  199. ->willReturn(true);
  200. $phpredis->expects($this->once())
  201. ->method('connect')
  202. ->with(
  203. 'tls://' . $Redis->getConfig('server'),
  204. (int)$this->port,
  205. (int)$Redis->getConfig('timeout'),
  206. )
  207. ->willReturn(true);
  208. $Redis->expects($this->once())
  209. ->method('_createRedisInstance')
  210. ->willReturn($phpredis);
  211. $config = [
  212. 'port' => $this->port,
  213. 'persistent' => false,
  214. 'tls' => true,
  215. ];
  216. $this->assertTrue($Redis->init($config + Cache::pool('redis')->getConfig()));
  217. }
  218. /**
  219. * testConnectTransientContext method
  220. */
  221. public function testConnectTransientContext(): void
  222. {
  223. $Redis = $this->createPartialMock(RedisEngine::class, ['_createRedisInstance']);
  224. $phpredis = $this->createMock(Redis::class);
  225. $cafile = ROOT . DS . 'vendor' . DS . 'composer' . DS . 'ca-bundle' . DS . 'res' . DS . 'cacert.pem';
  226. $context = [
  227. 'ssl' => [
  228. 'cafile' => $cafile,
  229. ],
  230. ];
  231. $phpredis->expects($this->once())
  232. ->method('select')
  233. ->with((int)$Redis->getConfig('database'))
  234. ->willReturn(true);
  235. $phpredis->expects($this->once())
  236. ->method('connect')
  237. ->with(
  238. $Redis->getConfig('server'),
  239. (int)$this->port,
  240. (int)$Redis->getConfig('timeout'),
  241. null,
  242. 0,
  243. 0.0,
  244. $context
  245. )
  246. ->willReturn(true);
  247. $Redis->expects($this->once())
  248. ->method('_createRedisInstance')
  249. ->willReturn($phpredis);
  250. $config = [
  251. 'port' => $this->port,
  252. 'persistent' => false,
  253. 'ssl_ca' => $cafile,
  254. ];
  255. $this->assertTrue($Redis->init($config + Cache::pool('redis')->getConfig()));
  256. }
  257. /**
  258. * testConnectPersistent method
  259. */
  260. public function testConnectPersistent(): void
  261. {
  262. $Redis = $this->createPartialMock(RedisEngine::class, ['_createRedisInstance']);
  263. $phpredis = $this->createMock(Redis::class);
  264. $expectedPersistentId = $this->port . $Redis->getConfig('timeout') . $Redis->getConfig('database');
  265. $phpredis->expects($this->once())
  266. ->method('select')
  267. ->with((int)$Redis->getConfig('database'))
  268. ->willReturn(true);
  269. $phpredis->expects($this->once())
  270. ->method('pconnect')
  271. ->with(
  272. $Redis->getConfig('server'),
  273. (int)$this->port,
  274. (int)$Redis->getConfig('timeout'),
  275. $expectedPersistentId
  276. )
  277. ->willReturn(true);
  278. $Redis->expects($this->once())
  279. ->method('_createRedisInstance')
  280. ->willReturn($phpredis);
  281. $config = [
  282. 'port' => $this->port,
  283. ];
  284. $this->assertTrue($Redis->init($config + Cache::pool('redis')->getConfig()));
  285. $Redis = $this->createPartialMock(RedisEngine::class, ['_createRedisInstance']);
  286. $phpredis = $this->createMock(Redis::class);
  287. $phpredis->expects($this->once())
  288. ->method('select')
  289. ->with((int)$Redis->getConfig('database'))
  290. ->willReturn(true);
  291. $phpredis->expects($this->once())
  292. ->method('pconnect')
  293. ->with(
  294. 'tls://' . $Redis->getConfig('server'),
  295. (int)$this->port,
  296. (int)$Redis->getConfig('timeout'),
  297. $expectedPersistentId
  298. )
  299. ->willReturn(true);
  300. $Redis->expects($this->once())
  301. ->method('_createRedisInstance')
  302. ->willReturn($phpredis);
  303. $config = [
  304. 'port' => $this->port,
  305. 'tls' => true,
  306. ];
  307. $this->assertTrue($Redis->init($config + Cache::pool('redis')->getConfig()));
  308. }
  309. /**
  310. * testConnectPersistentContext method
  311. */
  312. public function testConnectPersistentContext(): void
  313. {
  314. $Redis = $this->createPartialMock(RedisEngine::class, ['_createRedisInstance']);
  315. $phpredis = $this->createMock(Redis::class);
  316. $expectedPersistentId = $this->port . $Redis->getConfig('timeout') . $Redis->getConfig('database');
  317. $cafile = ROOT . DS . 'vendor' . DS . 'composer' . DS . 'ca-bundle' . DS . 'res' . DS . 'cacert.pem';
  318. $context = [
  319. 'ssl' => [
  320. 'cafile' => $cafile,
  321. ],
  322. ];
  323. $phpredis->expects($this->once())
  324. ->method('select')
  325. ->with((int)$Redis->getConfig('database'))
  326. ->willReturn(true);
  327. $phpredis->expects($this->once())
  328. ->method('pconnect')
  329. ->with(
  330. $Redis->getConfig('server'),
  331. (int)$this->port,
  332. (int)$Redis->getConfig('timeout'),
  333. $expectedPersistentId,
  334. 0,
  335. 0.0,
  336. $context
  337. )
  338. ->willReturn(true);
  339. $Redis->expects($this->once())
  340. ->method('_createRedisInstance')
  341. ->willReturn($phpredis);
  342. $config = [
  343. 'port' => $this->port,
  344. 'persistent' => true,
  345. 'ssl_ca' => $cafile,
  346. ];
  347. $this->assertTrue($Redis->init($config + Cache::pool('redis')->getConfig()));
  348. }
  349. /**
  350. * testMultiDatabaseOperations method
  351. */
  352. public function testMultiDatabaseOperations(): void
  353. {
  354. Cache::setConfig('redisdb0', [
  355. 'engine' => 'Redis',
  356. 'prefix' => 'cake2_',
  357. 'duration' => 3600,
  358. 'persistent' => false,
  359. 'port' => $this->port,
  360. ]);
  361. Cache::setConfig('redisdb1', [
  362. 'engine' => 'Redis',
  363. 'database' => 1,
  364. 'prefix' => 'cake2_',
  365. 'duration' => 3600,
  366. 'persistent' => false,
  367. 'port' => $this->port,
  368. ]);
  369. $result = Cache::write('save_in_0', true, 'redisdb0');
  370. $exist = Cache::read('save_in_0', 'redisdb0');
  371. $this->assertTrue($result);
  372. $this->assertTrue($exist);
  373. $result = Cache::write('save_in_1', true, 'redisdb1');
  374. $this->assertTrue($result);
  375. $exist = Cache::read('save_in_0', 'redisdb1');
  376. $this->assertNull($exist);
  377. $exist = Cache::read('save_in_1', 'redisdb1');
  378. $this->assertTrue($exist);
  379. Cache::delete('save_in_0', 'redisdb0');
  380. $exist = Cache::read('save_in_0', 'redisdb0');
  381. $this->assertNull($exist);
  382. Cache::delete('save_in_1', 'redisdb1');
  383. $exist = Cache::read('save_in_1', 'redisdb1');
  384. $this->assertNull($exist);
  385. Cache::drop('redisdb0');
  386. Cache::drop('redisdb1');
  387. }
  388. /**
  389. * test write numbers method
  390. */
  391. public function testWriteNumbers(): void
  392. {
  393. Cache::write('test-counter', 1, 'redis');
  394. $this->assertSame(1, Cache::read('test-counter', 'redis'));
  395. Cache::write('test-counter', 0, 'redis');
  396. $this->assertSame(0, Cache::read('test-counter', 'redis'));
  397. Cache::write('test-counter', -1, 'redis');
  398. $this->assertSame(-1, Cache::read('test-counter', 'redis'));
  399. }
  400. /**
  401. * testReadAndWriteCache method
  402. */
  403. public function testReadAndWriteCache(): void
  404. {
  405. $this->_configCache(['duration' => 1]);
  406. $result = Cache::read('test', 'redis');
  407. $this->assertNull($result);
  408. $data = 'this is a test of the emergency broadcasting system';
  409. $result = Cache::write('test', $data, 'redis');
  410. $this->assertTrue($result);
  411. $result = Cache::read('test', 'redis');
  412. $this->assertSame($data, $result);
  413. $data = [1, 2, 3];
  414. $this->assertTrue(Cache::write('array_data', $data, 'redis'));
  415. $this->assertEquals($data, Cache::read('array_data', 'redis'));
  416. $result = Cache::write('test', false, 'redis');
  417. $this->assertTrue($result);
  418. $result = Cache::read('test', 'redis');
  419. $this->assertFalse($result);
  420. $result = Cache::write('test', null, 'redis');
  421. $this->assertTrue($result);
  422. $result = Cache::read('test', 'redis');
  423. $this->assertNull($result);
  424. Cache::delete('test', 'redis');
  425. }
  426. /**
  427. * Test get with default value
  428. */
  429. public function testGetDefaultValue(): void
  430. {
  431. $redis = Cache::pool('redis');
  432. $this->assertFalse($redis->get('nope', false));
  433. $this->assertNull($redis->get('nope', null));
  434. $this->assertTrue($redis->get('nope', true));
  435. $this->assertSame(0, $redis->get('nope', 0));
  436. $redis->set('yep', 0);
  437. $this->assertSame(0, $redis->get('yep', false));
  438. }
  439. /**
  440. * testExpiry method
  441. */
  442. public function testExpiry(): void
  443. {
  444. $this->_configCache(['duration' => 1]);
  445. $result = Cache::read('test', 'redis');
  446. $this->assertNull($result);
  447. $data = 'this is a test of the emergency broadcasting system';
  448. $result = Cache::write('other_test', $data, 'redis');
  449. $this->assertTrue($result);
  450. sleep(2);
  451. $result = Cache::read('other_test', 'redis');
  452. $this->assertNull($result);
  453. $this->_configCache(['duration' => '+1 second']);
  454. $data = 'this is a test of the emergency broadcasting system';
  455. $result = Cache::write('other_test', $data, 'redis');
  456. $this->assertTrue($result);
  457. sleep(2);
  458. $result = Cache::read('other_test', 'redis');
  459. $this->assertNull($result);
  460. sleep(2);
  461. $result = Cache::read('other_test', 'redis');
  462. $this->assertNull($result);
  463. $this->_configCache(['duration' => '+29 days']);
  464. $data = 'this is a test of the emergency broadcasting system';
  465. $result = Cache::write('long_expiry_test', $data, 'redis');
  466. $this->assertTrue($result);
  467. sleep(2);
  468. $result = Cache::read('long_expiry_test', 'redis');
  469. $expecting = $data;
  470. $this->assertSame($expecting, $result);
  471. }
  472. /**
  473. * test set ttl parameter
  474. */
  475. public function testSetWithTtl(): void
  476. {
  477. $this->_configCache(['duration' => 99]);
  478. $engine = Cache::pool('redis');
  479. $this->assertNull($engine->get('test'));
  480. $data = 'this is a test of the emergency broadcasting system';
  481. $this->assertTrue($engine->set('default_ttl', $data));
  482. $this->assertTrue($engine->set('int_ttl', $data, 1));
  483. $this->assertTrue($engine->set('interval_ttl', $data, new DateInterval('PT1S')));
  484. sleep(2);
  485. $this->assertNull($engine->get('int_ttl'));
  486. $this->assertNull($engine->get('interval_ttl'));
  487. $this->assertSame($data, $engine->get('default_ttl'));
  488. }
  489. /**
  490. * testDeleteCache method
  491. */
  492. public function testDeleteCache(): void
  493. {
  494. $data = 'this is a test of the emergency broadcasting system';
  495. $result = Cache::write('delete_test', $data, 'redis');
  496. $this->assertTrue($result);
  497. $result = Cache::delete('delete_test', 'redis');
  498. $this->assertTrue($result);
  499. }
  500. /**
  501. * testDeleteCacheAsync method
  502. */
  503. public function testDeleteCacheAsync(): void
  504. {
  505. $data = 'this is a test of the emergency broadcasting system';
  506. $result = Cache::write('delete_async_test', $data, 'redis');
  507. $this->assertTrue($result);
  508. $result = Cache::pool('redis')->deleteAsync('delete_async_test');
  509. $this->assertTrue($result);
  510. }
  511. /**
  512. * testDecrement method
  513. */
  514. public function testDecrement(): void
  515. {
  516. Cache::delete('test_decrement', 'redis');
  517. $result = Cache::write('test_decrement', 5, 'redis');
  518. $this->assertTrue($result);
  519. $result = Cache::decrement('test_decrement', 1, 'redis');
  520. $this->assertSame(4, $result);
  521. $result = Cache::read('test_decrement', 'redis');
  522. $this->assertSame(4, $result);
  523. $result = Cache::decrement('test_decrement', 2, 'redis');
  524. $this->assertSame(2, $result);
  525. $result = Cache::read('test_decrement', 'redis');
  526. $this->assertSame(2, $result);
  527. }
  528. /**
  529. * testIncrement method
  530. */
  531. public function testIncrement(): void
  532. {
  533. Cache::delete('test_increment', 'redis');
  534. $result = Cache::increment('test_increment', 1, 'redis');
  535. $this->assertSame(1, $result);
  536. $result = Cache::read('test_increment', 'redis');
  537. $this->assertSame(1, $result);
  538. $result = Cache::increment('test_increment', 2, 'redis');
  539. $this->assertSame(3, $result);
  540. $result = Cache::read('test_increment', 'redis');
  541. $this->assertSame(3, $result);
  542. }
  543. /**
  544. * testIncrementAfterWrite method
  545. */
  546. public function testIncrementAfterWrite(): void
  547. {
  548. Cache::delete('test_increment', 'redis');
  549. $result = Cache::write('test_increment', 1, 'redis');
  550. $this->assertTrue($result);
  551. $result = Cache::read('test_increment', 'redis');
  552. $this->assertSame(1, $result);
  553. $result = Cache::increment('test_increment', 2, 'redis');
  554. $this->assertSame(3, $result);
  555. $result = Cache::read('test_increment', 'redis');
  556. $this->assertSame(3, $result);
  557. }
  558. /**
  559. * Test that increment() and decrement() can live forever.
  560. */
  561. public function testIncrementDecrementForvever(): void
  562. {
  563. $this->_configCache(['duration' => 0]);
  564. Cache::delete('test_increment', 'redis');
  565. Cache::delete('test_decrement', 'redis');
  566. $result = Cache::increment('test_increment', 1, 'redis');
  567. $this->assertSame(1, $result);
  568. $result = Cache::decrement('test_decrement', 1, 'redis');
  569. $this->assertSame(-1, $result);
  570. $this->assertSame(1, Cache::read('test_increment', 'redis'));
  571. $this->assertSame(-1, Cache::read('test_decrement', 'redis'));
  572. }
  573. /**
  574. * Test that increment and decrement set ttls.
  575. */
  576. public function testIncrementDecrementExpiring(): void
  577. {
  578. $this->_configCache(['duration' => 1]);
  579. Cache::delete('test_increment', 'redis');
  580. Cache::delete('test_decrement', 'redis');
  581. $this->assertSame(1, Cache::increment('test_increment', 1, 'redis'));
  582. $this->assertSame(-1, Cache::decrement('test_decrement', 1, 'redis'));
  583. sleep(2);
  584. $this->assertNull(Cache::read('test_increment', 'redis'));
  585. $this->assertNull(Cache::read('test_decrement', 'redis'));
  586. }
  587. /**
  588. * test clearing redis.
  589. */
  590. public function testClear(): void
  591. {
  592. Cache::setConfig('redis2', [
  593. 'engine' => 'Redis',
  594. 'prefix' => 'cake2_',
  595. 'duration' => 3600,
  596. 'port' => $this->port,
  597. ]);
  598. Cache::write('some_value', 'cache1', 'redis');
  599. $result = Cache::clear('redis');
  600. $this->assertTrue($result);
  601. $this->assertNull(Cache::read('some_value', 'redis'));
  602. Cache::write('some_value', 'cache2', 'redis2');
  603. $result = Cache::clear('redis');
  604. $this->assertTrue($result);
  605. $this->assertNull(Cache::read('some_value', 'redis'));
  606. $this->assertSame('cache2', Cache::read('some_value', 'redis2'));
  607. Cache::clear('redis2');
  608. }
  609. /**
  610. * testClearBlocking method
  611. */
  612. public function testClearBlocking(): void
  613. {
  614. Cache::setConfig('redis_clear_blocking', [
  615. 'engine' => 'Redis',
  616. 'prefix' => 'cake2_',
  617. 'duration' => 3600,
  618. 'port' => $this->port,
  619. ]);
  620. Cache::write('some_value', 'cache1', 'redis');
  621. $result = Cache::pool('redis')->clearBlocking();
  622. $this->assertTrue($result);
  623. $this->assertNull(Cache::read('some_value', 'redis'));
  624. Cache::write('some_value', 'cache2', 'redis_clear_blocking');
  625. $result = Cache::pool('redis')->clearBlocking();
  626. $this->assertTrue($result);
  627. $this->assertNull(Cache::read('some_value', 'redis'));
  628. $this->assertSame('cache2', Cache::read('some_value', 'redis_clear_blocking'));
  629. Cache::pool('redis_clear_blocking')->clearBlocking();
  630. }
  631. /**
  632. * test that a 0 duration can successfully write.
  633. */
  634. public function testZeroDuration(): void
  635. {
  636. $this->_configCache(['duration' => 0]);
  637. $result = Cache::write('test_key', 'written!', 'redis');
  638. $this->assertTrue($result);
  639. $result = Cache::read('test_key', 'redis');
  640. $this->assertSame('written!', $result);
  641. }
  642. /**
  643. * Tests that configuring groups for stored keys return the correct values when read/written
  644. * Shows that altering the group value is equivalent to deleting all keys under the same
  645. * group
  646. */
  647. public function testGroupReadWrite(): void
  648. {
  649. Cache::setConfig('redis_groups', [
  650. 'engine' => 'Redis',
  651. 'duration' => 3600,
  652. 'groups' => ['group_a', 'group_b'],
  653. 'prefix' => 'test_',
  654. 'port' => $this->port,
  655. ]);
  656. Cache::setConfig('redis_helper', [
  657. 'engine' => 'Redis',
  658. 'duration' => 3600,
  659. 'prefix' => 'test_',
  660. 'port' => $this->port,
  661. ]);
  662. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  663. $this->assertSame('value', Cache::read('test_groups', 'redis_groups'));
  664. Cache::increment('group_a', 1, 'redis_helper');
  665. $this->assertNull(Cache::read('test_groups', 'redis_groups'));
  666. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  667. $this->assertSame('value2', Cache::read('test_groups', 'redis_groups'));
  668. Cache::increment('group_b', 1, 'redis_helper');
  669. $this->assertNull(Cache::read('test_groups', 'redis_groups'));
  670. $this->assertTrue(Cache::write('test_groups', 'value3', 'redis_groups'));
  671. $this->assertSame('value3', Cache::read('test_groups', 'redis_groups'));
  672. }
  673. /**
  674. * Tests that deleting from a groups-enabled config is possible
  675. */
  676. public function testGroupDelete(): void
  677. {
  678. Cache::setConfig('redis_groups', [
  679. 'engine' => 'Redis',
  680. 'duration' => 3600,
  681. 'groups' => ['group_a', 'group_b'],
  682. 'port' => $this->port,
  683. ]);
  684. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  685. $this->assertSame('value', Cache::read('test_groups', 'redis_groups'));
  686. $this->assertTrue(Cache::delete('test_groups', 'redis_groups'));
  687. $this->assertNull(Cache::read('test_groups', 'redis_groups'));
  688. }
  689. /**
  690. * Test clearing a cache group
  691. */
  692. public function testGroupClear(): void
  693. {
  694. Cache::setConfig('redis_groups', [
  695. 'engine' => 'Redis',
  696. 'duration' => 3600,
  697. 'groups' => ['group_a', 'group_b'],
  698. 'port' => $this->port,
  699. ]);
  700. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  701. $this->assertTrue(Cache::clearGroup('group_a', 'redis_groups'));
  702. $this->assertNull(Cache::read('test_groups', 'redis_groups'));
  703. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  704. $this->assertTrue(Cache::clearGroup('group_b', 'redis_groups'));
  705. $this->assertNull(Cache::read('test_groups', 'redis_groups'));
  706. }
  707. /**
  708. * Test add
  709. */
  710. public function testAdd(): void
  711. {
  712. Cache::delete('test_add_key', 'redis');
  713. $result = Cache::add('test_add_key', 'test data', 'redis');
  714. $this->assertTrue($result);
  715. $expected = 'test data';
  716. $result = Cache::read('test_add_key', 'redis');
  717. $this->assertSame($expected, $result);
  718. $result = Cache::add('test_add_key', 'test data 2', 'redis');
  719. $this->assertFalse($result);
  720. }
  721. }