RedisEngineTest.php 8.8 KB

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