RedisEngineTest.php 8.7 KB

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