RedisBackendTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /*
  3. ==New BSD License==
  4. Copyright (c) 2012, Colin Mollenhour
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. * The name of Colin Mollenhour may not be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  19. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. require_once 'app/Mage.php';
  27. require_once 'Zend/Cache.php';
  28. require_once 'Cm/Cache/Backend/Redis.php';
  29. require_once 'CommonExtendedBackendTest.php';
  30. /**
  31. * @copyright Copyright (c) 2012 Colin Mollenhour (http://colin.mollenhour.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Cache_RedisBackendTest extends Zend_Cache_CommonExtendedBackendTest {
  35. const LUA_MAX_C_STACK = 1000;
  36. protected $forceStandalone = FALSE;
  37. protected $autoExpireLifetime = 0;
  38. protected $autoExpireRefreshOnLoad = 0;
  39. /** @var Cm_Cache_Backend_Redis */
  40. protected $_instance;
  41. public function __construct($name = null, array $data = array(), $dataName = '')
  42. {
  43. parent::__construct('Cm_Cache_Backend_Redis', $data, $dataName);
  44. }
  45. public function setUp($notag = false)
  46. {
  47. $this->_instance = new Cm_Cache_Backend_Redis(array(
  48. 'server' => '127.0.0.1',
  49. 'port' => '6379',
  50. 'database' => '1',
  51. 'notMatchingTags' => TRUE,
  52. 'force_standalone' => $this->forceStandalone,
  53. 'compress_threshold' => 100,
  54. 'compression_lib' => 'gzip',
  55. 'use_lua' => TRUE,
  56. 'lua_max_c_stack' => self::LUA_MAX_C_STACK,
  57. 'auto_expire_lifetime' => $this->autoExpireLifetime,
  58. 'auto_expire_refresh_on_load' => $this->autoExpireRefreshOnLoad,
  59. ));
  60. $this->_instance->clean(Zend_Cache::CLEANING_MODE_ALL);
  61. $this->_instance->___scriptFlush();
  62. parent::setUp($notag);
  63. }
  64. public function tearDown()
  65. {
  66. parent::tearDown();
  67. unset($this->_instance);
  68. }
  69. public function testConstructorCorrectCall()
  70. {
  71. // nah
  72. }
  73. public function testGetWithAnExpiredCacheId()
  74. {
  75. // not supported
  76. }
  77. public function testCompression()
  78. {
  79. $longString = str_repeat(md5('asd')."\r\n", 50);
  80. $this->assertTrue($this->_instance->save($longString, 'long', array('long')));
  81. $this->assertTrue($this->_instance->load('long') == $longString);
  82. }
  83. public function testExpiredCleanup()
  84. {
  85. $this->assertTrue($this->_instance->clean());
  86. $this->assertTrue($this->_instance->save('BLAH','foo', array('TAG1', 'TAG2'), 1));
  87. $this->assertTrue($this->_instance->save('BLAH','bar', array('TAG1', 'TAG3'), 1));
  88. $ids = $this->_instance->getIdsMatchingAnyTags(array('TAG1','TAG2','TAG3'));
  89. sort($ids);
  90. $this->assertEquals(array('bar','foo'), $ids);
  91. // sleep(2);
  92. $this->_instance->___expire('foo');
  93. $this->_instance->___expire('bar');
  94. $this->_instance->clean(Zend_Cache::CLEANING_MODE_OLD);
  95. $this->assertEquals(array(), $this->_instance->getIdsMatchingAnyTags(array('TAG1','TAG2','TAG3')));
  96. $this->assertEquals(array(), $this->_instance->getTags());
  97. }
  98. /**
  99. $this->_instance->save('bar : data to cache', 'bar', array('tag3', 'tag4'));
  100. $this->_instance->save('bar2 : data to cache', 'bar2', array('tag3', 'tag1'));
  101. $this->_instance->save('bar3 : data to cache', 'bar3', array('tag2', 'tag3'));
  102. */
  103. public function testGetIdsMatchingAnyTags()
  104. {
  105. $res = $this->_instance->getIdsMatchingAnyTags(array('tag999'));
  106. $this->assertEquals(0, count($res));
  107. }
  108. public function testGetIdsMatchingAnyTags2()
  109. {
  110. $res = $this->_instance->getIdsMatchingAnyTags(array('tag1', 'tag999'));
  111. $this->assertEquals(1, count($res));
  112. $this->assertTrue(in_array('bar2', $res));
  113. }
  114. public function testGetIdsMatchingAnyTags3()
  115. {
  116. $res = $this->_instance->getIdsMatchingAnyTags(array('tag3', 'tag999'));
  117. $this->assertEquals(3, count($res));
  118. $this->assertTrue(in_array('bar', $res));
  119. $this->assertTrue(in_array('bar2', $res));
  120. $this->assertTrue(in_array('bar3', $res));
  121. }
  122. public function testGetIdsMatchingAnyTags4()
  123. {
  124. $res = $this->_instance->getIdsMatchingAnyTags(array('tag1', 'tag4'));
  125. $this->assertEquals(2, count($res));
  126. $this->assertTrue(in_array('bar', $res));
  127. $this->assertTrue(in_array('bar2', $res));
  128. }
  129. public function testCleanModeMatchingAnyTags()
  130. {
  131. $this->_instance->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array('tag999'));
  132. $this->assertTrue(!!$this->_instance->load('bar'));
  133. $this->assertTrue(!!$this->_instance->load('bar2'));
  134. $this->assertTrue(!!$this->_instance->load('bar3'));
  135. }
  136. public function testCleanModeMatchingAnyTags2()
  137. {
  138. $this->_instance->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array('tag1', 'tag999'));
  139. $this->assertTrue(!!$this->_instance->load('bar'));
  140. $this->assertFalse(!!$this->_instance->load('bar2'));
  141. $this->assertTrue(!!$this->_instance->load('bar3'));
  142. }
  143. public function testCleanModeMatchingAnyTags3()
  144. {
  145. $this->_instance->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array('tag3', 'tag999'));
  146. $this->assertFalse(!!$this->_instance->load('bar'));
  147. $this->assertFalse(!!$this->_instance->load('bar2'));
  148. $this->assertFalse(!!$this->_instance->load('bar3'));
  149. }
  150. public function testCleanModeMatchingAnyTags4()
  151. {
  152. $this->_instance->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array('tag1', 'tag4'));
  153. $this->assertFalse(!!$this->_instance->load('bar'));
  154. $this->assertFalse(!!$this->_instance->load('bar2'));
  155. $this->assertTrue(!!$this->_instance->load('bar3'));
  156. }
  157. public function testCleanModeMatchingAnyTags5()
  158. {
  159. $tags = array('tag1', 'tag4');
  160. for ($i = 0; $i < self::LUA_MAX_C_STACK*5; $i++) {
  161. $this->_instance->save('foo', 'foo'.$i, $tags);
  162. }
  163. $this->assertGreaterThan(self::LUA_MAX_C_STACK, count($this->_instance->getIdsMatchingAnyTags($tags)));
  164. $this->_instance->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, $tags);
  165. $this->assertEquals(0, count($this->_instance->getIdsMatchingAnyTags($tags)));
  166. }
  167. public function testCleanModeMatchingAnyTags6()
  168. {
  169. $tags = array();
  170. for ($i = 0; $i < self::LUA_MAX_C_STACK*5; $i++) {
  171. $tags[] = 'baz'.$i;
  172. }
  173. $this->_instance->save('foo', 'foo', $tags);
  174. $_tags = array(end($tags));
  175. $this->assertEquals(1, count($this->_instance->getIdsMatchingAnyTags($_tags)));
  176. $this->_instance->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, $_tags);
  177. $this->assertEquals(0, count($this->_instance->getIdsMatchingAnyTags($_tags)));
  178. }
  179. }