RedisEngineTest.php 14 KB

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