RedisEngineTest.php 8.9 KB

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