RedisEngineTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 CakePHP(tm) v 2.2
  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. */
  26. class RedisEngineTest extends TestCase {
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. $this->skipIf(!class_exists('Redis'), 'Redis is not installed or configured properly.');
  35. Cache::enable();
  36. $this->_configCache();
  37. }
  38. /**
  39. * tearDown method
  40. *
  41. * @return void
  42. */
  43. public function tearDown() {
  44. parent::tearDown();
  45. Cache::drop('redis');
  46. Cache::drop('redis_groups');
  47. Cache::drop('redis_helper');
  48. }
  49. /**
  50. * Helper method for testing.
  51. *
  52. * @return void
  53. */
  54. protected function _configCache($config = []) {
  55. $defaults = [
  56. 'className' => 'Redis',
  57. 'prefix' => 'cake_',
  58. 'duration' => 3600
  59. ];
  60. Cache::drop('redis');
  61. Cache::config('redis', array_merge($defaults, $config));
  62. }
  63. /**
  64. * testConfig method
  65. *
  66. * @return void
  67. */
  68. public function testConfig() {
  69. $config = Cache::engine('redis')->config();
  70. $expecting = array(
  71. 'prefix' => 'cake_',
  72. 'duration' => 3600,
  73. 'probability' => 100,
  74. 'groups' => array(),
  75. 'server' => '127.0.0.1',
  76. 'port' => 6379,
  77. 'timeout' => 0,
  78. 'persistent' => true,
  79. 'password' => false,
  80. 'database' => 0,
  81. );
  82. $this->assertEquals($expecting, $config);
  83. }
  84. /**
  85. * testConnect method
  86. *
  87. * @return void
  88. */
  89. public function testConnect() {
  90. $Redis = new RedisEngine();
  91. $this->assertTrue($Redis->init(Cache::engine('redis')->config()));
  92. }
  93. /**
  94. * testMultiDatabaseOperations method
  95. *
  96. * @return void
  97. */
  98. public function testMultiDatabaseOperations() {
  99. Cache::config('redisdb0', array(
  100. 'engine' => 'Redis',
  101. 'prefix' => 'cake2_',
  102. 'duration' => 3600,
  103. 'persistent' => false,
  104. ));
  105. Cache::config('redisdb1', array(
  106. 'engine' => 'Redis',
  107. 'database' => 1,
  108. 'prefix' => 'cake2_',
  109. 'duration' => 3600,
  110. 'persistent' => false,
  111. ));
  112. $result = Cache::write('save_in_0', true, 'redisdb0');
  113. $exist = Cache::read('save_in_0', 'redisdb0');
  114. $this->assertTrue($result);
  115. $this->assertTrue($exist);
  116. $result = Cache::write('save_in_1', true, 'redisdb1');
  117. $this->assertTrue($result);
  118. $exist = Cache::read('save_in_0', 'redisdb1');
  119. $this->assertFalse($exist);
  120. $exist = Cache::read('save_in_1', 'redisdb1');
  121. $this->assertTrue($exist);
  122. Cache::delete('save_in_0', 'redisdb0');
  123. $exist = Cache::read('save_in_0', 'redisdb0');
  124. $this->assertFalse($exist);
  125. Cache::delete('save_in_1', 'redisdb1');
  126. $exist = Cache::read('save_in_1', 'redisdb1');
  127. $this->assertFalse($exist);
  128. Cache::drop('redisdb0');
  129. Cache::drop('redisdb1');
  130. }
  131. /**
  132. * testReadAndWriteCache method
  133. *
  134. * @return void
  135. */
  136. public function testReadAndWriteCache() {
  137. $this->_configCache(['duration' => 1]);
  138. $result = Cache::read('test', 'redis');
  139. $expecting = '';
  140. $this->assertEquals($expecting, $result);
  141. $data = 'this is a test of the emergency broadcasting system';
  142. $result = Cache::write('test', $data, 'redis');
  143. $this->assertTrue($result);
  144. $result = Cache::read('test', 'redis');
  145. $expecting = $data;
  146. $this->assertEquals($expecting, $result);
  147. $data = array(1, 2, 3);
  148. $this->assertTrue(Cache::write('array_data', $data, 'redis'));
  149. $this->assertEquals($data, Cache::read('array_data', 'redis'));
  150. Cache::delete('test', 'redis');
  151. }
  152. /**
  153. * testExpiry method
  154. *
  155. * @return void
  156. */
  157. public function testExpiry() {
  158. $this->_configCache(['duration' => 1]);
  159. $result = Cache::read('test', 'redis');
  160. $this->assertFalse($result);
  161. $data = 'this is a test of the emergency broadcasting system';
  162. $result = Cache::write('other_test', $data, 'redis');
  163. $this->assertTrue($result);
  164. sleep(2);
  165. $result = Cache::read('other_test', 'redis');
  166. $this->assertFalse($result);
  167. $this->_configCache(['duration' => '+1 second']);
  168. $data = 'this is a test of the emergency broadcasting system';
  169. $result = Cache::write('other_test', $data, 'redis');
  170. $this->assertTrue($result);
  171. sleep(2);
  172. $result = Cache::read('other_test', 'redis');
  173. $this->assertFalse($result);
  174. sleep(2);
  175. $result = Cache::read('other_test', 'redis');
  176. $this->assertFalse($result);
  177. $this->_configCache(['duration' => '+29 days']);
  178. $data = 'this is a test of the emergency broadcasting system';
  179. $result = Cache::write('long_expiry_test', $data, 'redis');
  180. $this->assertTrue($result);
  181. sleep(2);
  182. $result = Cache::read('long_expiry_test', 'redis');
  183. $expecting = $data;
  184. $this->assertEquals($expecting, $result);
  185. }
  186. /**
  187. * testDeleteCache method
  188. *
  189. * @return void
  190. */
  191. public function testDeleteCache() {
  192. $data = 'this is a test of the emergency broadcasting system';
  193. $result = Cache::write('delete_test', $data, 'redis');
  194. $this->assertTrue($result);
  195. $result = Cache::delete('delete_test', 'redis');
  196. $this->assertTrue($result);
  197. }
  198. /**
  199. * testDecrement method
  200. *
  201. * @return void
  202. */
  203. public function testDecrement() {
  204. Cache::delete('test_decrement', 'redis');
  205. $result = Cache::write('test_decrement', 5, 'redis');
  206. $this->assertTrue($result);
  207. $result = Cache::decrement('test_decrement', 1, 'redis');
  208. $this->assertEquals(4, $result);
  209. $result = Cache::read('test_decrement', 'redis');
  210. $this->assertEquals(4, $result);
  211. $result = Cache::decrement('test_decrement', 2, 'redis');
  212. $this->assertEquals(2, $result);
  213. $result = Cache::read('test_decrement', 'redis');
  214. $this->assertEquals(2, $result);
  215. }
  216. /**
  217. * testIncrement method
  218. *
  219. * @return void
  220. */
  221. public function testIncrement() {
  222. Cache::delete('test_increment', 'redis');
  223. $result = Cache::increment('test_increment', 1, 'redis');
  224. $this->assertEquals(1, $result);
  225. $result = Cache::read('test_increment', 'redis');
  226. $this->assertEquals(1, $result);
  227. $result = Cache::increment('test_increment', 2, 'redis');
  228. $this->assertEquals(3, $result);
  229. $result = Cache::read('test_increment', 'redis');
  230. $this->assertEquals(3, $result);
  231. }
  232. /**
  233. * test clearing redis.
  234. *
  235. * @return void
  236. */
  237. public function testClear() {
  238. Cache::config('redis2', array(
  239. 'engine' => 'Redis',
  240. 'prefix' => 'cake2_',
  241. 'duration' => 3600
  242. ));
  243. Cache::write('some_value', 'cache1', 'redis');
  244. $result = Cache::clear(true, 'redis');
  245. $this->assertTrue($result);
  246. $this->assertEquals('cache1', Cache::read('some_value', 'redis'));
  247. Cache::write('some_value', 'cache2', 'redis2');
  248. $result = Cache::clear(false, 'redis');
  249. $this->assertTrue($result);
  250. $this->assertFalse(Cache::read('some_value', 'redis'));
  251. $this->assertEquals('cache2', Cache::read('some_value', 'redis2'));
  252. Cache::clear(false, 'redis2');
  253. }
  254. /**
  255. * test that a 0 duration can successfully write.
  256. *
  257. * @return void
  258. */
  259. public function testZeroDuration() {
  260. $this->_configCache(['duration' => 0]);
  261. $result = Cache::write('test_key', 'written!', 'redis');
  262. $this->assertTrue($result);
  263. $result = Cache::read('test_key', 'redis');
  264. $this->assertEquals('written!', $result);
  265. }
  266. /**
  267. * Tests that configuring groups for stored keys return the correct values when read/written
  268. * Shows that altering the group value is equivalent to deleting all keys under the same
  269. * group
  270. *
  271. * @return void
  272. */
  273. public function testGroupReadWrite() {
  274. Cache::config('redis_groups', [
  275. 'engine' => 'Redis',
  276. 'duration' => 3600,
  277. 'groups' => ['group_a', 'group_b'],
  278. 'prefix' => 'test_'
  279. ]);
  280. Cache::config('redis_helper', [
  281. 'engine' => 'Redis',
  282. 'duration' => 3600,
  283. 'prefix' => 'test_'
  284. ]);
  285. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  286. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  287. Cache::increment('group_a', 1, 'redis_helper');
  288. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  289. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  290. $this->assertEquals('value2', Cache::read('test_groups', 'redis_groups'));
  291. Cache::increment('group_b', 1, 'redis_helper');
  292. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  293. $this->assertTrue(Cache::write('test_groups', 'value3', 'redis_groups'));
  294. $this->assertEquals('value3', Cache::read('test_groups', 'redis_groups'));
  295. }
  296. /**
  297. * Tests that deleteing from a groups-enabled config is possible
  298. *
  299. * @return void
  300. */
  301. public function testGroupDelete() {
  302. Cache::config('redis_groups', [
  303. 'engine' => 'Redis',
  304. 'duration' => 3600,
  305. 'groups' => ['group_a', 'group_b']
  306. ]);
  307. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  308. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  309. $this->assertTrue(Cache::delete('test_groups', 'redis_groups'));
  310. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  311. }
  312. /**
  313. * Test clearing a cache group
  314. *
  315. * @return void
  316. */
  317. public function testGroupClear() {
  318. Cache::config('redis_groups', [
  319. 'engine' => 'Redis',
  320. 'duration' => 3600,
  321. 'groups' => ['group_a', 'group_b']
  322. ]);
  323. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  324. $this->assertTrue(Cache::clearGroup('group_a', 'redis_groups'));
  325. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  326. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  327. $this->assertTrue(Cache::clearGroup('group_b', 'redis_groups'));
  328. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  329. }
  330. }