RedisEngineTest.php 8.8 KB

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