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. $expecting = array(
  62. 'prefix' => 'cake_',
  63. 'duration' => 3600,
  64. 'probability' => 100,
  65. 'groups' => array(),
  66. 'engine' => 'Redis',
  67. 'server' => '127.0.0.1',
  68. 'port' => 6379,
  69. 'timeout' => 0,
  70. 'persistent' => true,
  71. 'password' => false,
  72. );
  73. $this->assertEquals($expecting, $settings);
  74. }
  75. /**
  76. * testConnect method
  77. *
  78. * @return void
  79. */
  80. public function testConnect() {
  81. $Redis = new RedisEngine();
  82. $this->assertTrue($Redis->init(Cache::settings('redis')));
  83. }
  84. /**
  85. * testReadAndWriteCache method
  86. *
  87. * @return void
  88. */
  89. public function testReadAndWriteCache() {
  90. Cache::set(array('duration' => 1), null, 'redis');
  91. $result = Cache::read('test', 'redis');
  92. $expecting = '';
  93. $this->assertEquals($expecting, $result);
  94. $data = 'this is a test of the emergency broadcasting system';
  95. $result = Cache::write('test', $data, 'redis');
  96. $this->assertTrue($result);
  97. $result = Cache::read('test', 'redis');
  98. $expecting = $data;
  99. $this->assertEquals($expecting, $result);
  100. $data = array(1, 2, 3);
  101. $this->assertTrue(Cache::write('array_data', $data, 'redis'));
  102. $this->assertEquals($data, Cache::read('array_data', 'redis'));
  103. Cache::delete('test', 'redis');
  104. }
  105. /**
  106. * testExpiry method
  107. *
  108. * @return void
  109. */
  110. public function testExpiry() {
  111. Cache::set(array('duration' => 1), 'redis');
  112. $result = Cache::read('test', 'redis');
  113. $this->assertFalse($result);
  114. $data = 'this is a test of the emergency broadcasting system';
  115. $result = Cache::write('other_test', $data, 'redis');
  116. $this->assertTrue($result);
  117. sleep(2);
  118. $result = Cache::read('other_test', 'redis');
  119. $this->assertFalse($result);
  120. Cache::set(array('duration' => "+1 second"), 'redis');
  121. $data = 'this is a test of the emergency broadcasting system';
  122. $result = Cache::write('other_test', $data, 'redis');
  123. $this->assertTrue($result);
  124. sleep(2);
  125. $result = Cache::read('other_test', 'redis');
  126. $this->assertFalse($result);
  127. Cache::config('redis', array('duration' => '+1 second'));
  128. sleep(2);
  129. $result = Cache::read('other_test', 'redis');
  130. $this->assertFalse($result);
  131. Cache::config('redis', array('duration' => '+29 days'));
  132. $data = 'this is a test of the emergency broadcasting system';
  133. $result = Cache::write('long_expiry_test', $data, 'redis');
  134. $this->assertTrue($result);
  135. sleep(2);
  136. $result = Cache::read('long_expiry_test', 'redis');
  137. $expecting = $data;
  138. $this->assertEquals($expecting, $result);
  139. Cache::config('redis', array('duration' => 3600));
  140. }
  141. /**
  142. * testDeleteCache method
  143. *
  144. * @return void
  145. */
  146. public function testDeleteCache() {
  147. $data = 'this is a test of the emergency broadcasting system';
  148. $result = Cache::write('delete_test', $data, 'redis');
  149. $this->assertTrue($result);
  150. $result = Cache::delete('delete_test', 'redis');
  151. $this->assertTrue($result);
  152. }
  153. /**
  154. * testDecrement method
  155. *
  156. * @return void
  157. */
  158. public function testDecrement() {
  159. Cache::delete('test_decrement', 'redis');
  160. $result = Cache::write('test_decrement', 5, 'redis');
  161. $this->assertTrue($result);
  162. $result = Cache::decrement('test_decrement', 1, 'redis');
  163. $this->assertEquals(4, $result);
  164. $result = Cache::read('test_decrement', 'redis');
  165. $this->assertEquals(4, $result);
  166. $result = Cache::decrement('test_decrement', 2, 'redis');
  167. $this->assertEquals(2, $result);
  168. $result = Cache::read('test_decrement', 'redis');
  169. $this->assertEquals(2, $result);
  170. }
  171. /**
  172. * testIncrement method
  173. *
  174. * @return void
  175. */
  176. public function testIncrement() {
  177. Cache::delete('test_increment', 'redis');
  178. $result = Cache::increment('test_increment', 1, 'redis');
  179. $this->assertEquals(1, $result);
  180. $result = Cache::read('test_increment', 'redis');
  181. $this->assertEquals(1, $result);
  182. $result = Cache::increment('test_increment', 2, 'redis');
  183. $this->assertEquals(3, $result);
  184. $result = Cache::read('test_increment', 'redis');
  185. $this->assertEquals(3, $result);
  186. }
  187. /**
  188. * test clearing redis.
  189. *
  190. * @return void
  191. */
  192. public function testClear() {
  193. Cache::config('redis2', array(
  194. 'engine' => 'Redis',
  195. 'prefix' => 'cake2_',
  196. 'duration' => 3600
  197. ));
  198. Cache::write('some_value', 'cache1', 'redis');
  199. $result = Cache::clear(true, 'redis');
  200. $this->assertTrue($result);
  201. $this->assertEquals('cache1', Cache::read('some_value', 'redis'));
  202. Cache::write('some_value', 'cache2', 'redis2');
  203. $result = Cache::clear(false, 'redis');
  204. $this->assertTrue($result);
  205. $this->assertFalse(Cache::read('some_value', 'redis'));
  206. $this->assertEquals('cache2', Cache::read('some_value', 'redis2'));
  207. Cache::clear(false, 'redis2');
  208. }
  209. /**
  210. * test that a 0 duration can successfully write.
  211. *
  212. * @return void
  213. */
  214. public function testZeroDuration() {
  215. Cache::config('redis', array('duration' => 0));
  216. $result = Cache::write('test_key', 'written!', 'redis');
  217. $this->assertTrue($result);
  218. $result = Cache::read('test_key', 'redis');
  219. $this->assertEquals('written!', $result);
  220. }
  221. /**
  222. * Tests that configuring groups for stored keys return the correct values when read/written
  223. * Shows that altering the group value is equivalent to deleting all keys under the same
  224. * group
  225. *
  226. * @return void
  227. */
  228. public function testGroupReadWrite() {
  229. Cache::config('redis_groups', array(
  230. 'engine' => 'Redis',
  231. 'duration' => 3600,
  232. 'groups' => array('group_a', 'group_b'),
  233. 'prefix' => 'test_'
  234. ));
  235. Cache::config('redis_helper', array(
  236. 'engine' => 'Redis',
  237. 'duration' => 3600,
  238. 'prefix' => 'test_'
  239. ));
  240. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  241. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  242. Cache::increment('group_a', 1, 'redis_helper');
  243. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  244. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  245. $this->assertEquals('value2', Cache::read('test_groups', 'redis_groups'));
  246. Cache::increment('group_b', 1, 'redis_helper');
  247. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  248. $this->assertTrue(Cache::write('test_groups', 'value3', 'redis_groups'));
  249. $this->assertEquals('value3', Cache::read('test_groups', 'redis_groups'));
  250. }
  251. /**
  252. * Tests that deleteing from a groups-enabled config is possible
  253. *
  254. * @return void
  255. */
  256. public function testGroupDelete() {
  257. Cache::config('redis_groups', array(
  258. 'engine' => 'Redis',
  259. 'duration' => 3600,
  260. 'groups' => array('group_a', 'group_b')
  261. ));
  262. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  263. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  264. $this->assertTrue(Cache::delete('test_groups', 'redis_groups'));
  265. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  266. }
  267. /**
  268. * Test clearing a cache group
  269. *
  270. * @return void
  271. */
  272. public function testGroupClear() {
  273. Cache::config('redis_groups', array(
  274. 'engine' => 'Redis',
  275. 'duration' => 3600,
  276. 'groups' => array('group_a', 'group_b')
  277. ));
  278. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  279. $this->assertTrue(Cache::clearGroup('group_a', 'redis_groups'));
  280. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  281. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  282. $this->assertTrue(Cache::clearGroup('group_b', 'redis_groups'));
  283. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  284. }
  285. }