MemcacheEngineTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. /**
  3. * MemcacheEngineTest 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 1.2.0.5434
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Cache', 'Cache');
  20. App::uses('MemcacheEngine', 'Cache/Engine');
  21. class TestMemcacheEngine extends MemcacheEngine {
  22. /**
  23. * public accessor to _parseServerString
  24. *
  25. * @param string $server
  26. * @return array
  27. */
  28. public function parseServerString($server) {
  29. return $this->_parseServerString($server);
  30. }
  31. public function setMemcache($memcache) {
  32. $this->_Memcache = $memcache;
  33. }
  34. }
  35. /**
  36. * MemcacheEngineTest class
  37. *
  38. * @package Cake.Test.Case.Cache.Engine
  39. */
  40. class MemcacheEngineTest extends CakeTestCase {
  41. /**
  42. * setUp method
  43. *
  44. * @return void
  45. */
  46. public function setUp() {
  47. $this->skipIf(!class_exists('Memcache'), 'Memcache is not installed or configured properly.');
  48. $this->_cacheDisable = Configure::read('Cache.disable');
  49. Configure::write('Cache.disable', false);
  50. Cache::config('memcache', array(
  51. 'engine' => 'Memcache',
  52. 'prefix' => 'cake_',
  53. 'duration' => 3600
  54. ));
  55. }
  56. /**
  57. * tearDown method
  58. *
  59. * @return void
  60. */
  61. public function tearDown() {
  62. Configure::write('Cache.disable', $this->_cacheDisable);
  63. Cache::drop('memcache');
  64. Cache::drop('memcache_groups');
  65. Cache::drop('memcache_helper');
  66. Cache::config('default');
  67. }
  68. /**
  69. * testSettings method
  70. *
  71. * @return void
  72. */
  73. public function testSettings() {
  74. $settings = Cache::settings('memcache');
  75. unset($settings['serialize'], $settings['path']);
  76. $expecting = array(
  77. 'prefix' => 'cake_',
  78. 'duration' => 3600,
  79. 'probability' => 100,
  80. 'servers' => array('127.0.0.1'),
  81. 'persistent' => true,
  82. 'compress' => false,
  83. 'engine' => 'Memcache',
  84. 'persistent' => true,
  85. );
  86. $this->assertEquals($expecting, $settings);
  87. }
  88. /**
  89. * testSettings method
  90. *
  91. * @return void
  92. */
  93. public function testMultipleServers() {
  94. $servers = array('127.0.0.1:11211', '127.0.0.1:11222');
  95. $available = true;
  96. $Memcache = new Memcache();
  97. foreach ($servers as $server) {
  98. list($host, $port) = explode(':', $server);
  99. if (!@$Memcache->connect($host, $port)) {
  100. $available = false;
  101. }
  102. }
  103. $this->skipIf(!$available, 'Need memcache servers at ' . implode(', ', $servers) . ' to run this test.');
  104. $Memcache = new MemcacheEngine();
  105. $Memcache->init(array('engine' => 'Memcache', 'servers' => $servers));
  106. $servers = array_keys($Memcache->__Memcache->getExtendedStats());
  107. $settings = $Memcache->settings();
  108. $this->assertEquals($settings['servers'], $servers);
  109. Cache::drop('dual_server');
  110. }
  111. /**
  112. * testConnect method
  113. *
  114. * @return void
  115. */
  116. public function testConnect() {
  117. $Memcache = new MemcacheEngine();
  118. $Memcache->init(Cache::settings('memcache'));
  119. $result = $Memcache->connect('127.0.0.1');
  120. $this->assertTrue($result);
  121. }
  122. /**
  123. * test connecting to an ipv6 server.
  124. *
  125. * @return void
  126. */
  127. public function testConnectIpv6() {
  128. $Memcache = new MemcacheEngine();
  129. $result = $Memcache->init(array(
  130. 'prefix' => 'cake_',
  131. 'duration' => 200,
  132. 'engine' => 'Memcache',
  133. 'servers' => array(
  134. '[::1]:11211'
  135. )
  136. ));
  137. $this->assertTrue($result);
  138. }
  139. /**
  140. * test non latin domains.
  141. *
  142. * @return void
  143. */
  144. public function testParseServerStringNonLatin() {
  145. $Memcache = new TestMemcacheEngine();
  146. $result = $Memcache->parseServerString('schülervz.net:13211');
  147. $this->assertEquals(array('schülervz.net', '13211'), $result);
  148. $result = $Memcache->parseServerString('sülül:1111');
  149. $this->assertEquals(array('sülül', '1111'), $result);
  150. }
  151. /**
  152. * test unix sockets.
  153. *
  154. * @return void
  155. */
  156. public function testParseServerStringUnix() {
  157. $Memcache = new TestMemcacheEngine();
  158. $result = $Memcache->parseServerString('unix:///path/to/memcached.sock');
  159. $this->assertEquals(array('unix:///path/to/memcached.sock', 0), $result);
  160. }
  161. /**
  162. * testReadAndWriteCache method
  163. *
  164. * @return void
  165. */
  166. public function testReadAndWriteCache() {
  167. Cache::set(array('duration' => 1), null, 'memcache');
  168. $result = Cache::read('test', 'memcache');
  169. $expecting = '';
  170. $this->assertEquals($expecting, $result);
  171. $data = 'this is a test of the emergency broadcasting system';
  172. $result = Cache::write('test', $data, 'memcache');
  173. $this->assertTrue($result);
  174. $result = Cache::read('test', 'memcache');
  175. $expecting = $data;
  176. $this->assertEquals($expecting, $result);
  177. Cache::delete('test', 'memcache');
  178. }
  179. /**
  180. * testExpiry method
  181. *
  182. * @return void
  183. */
  184. public function testExpiry() {
  185. Cache::set(array('duration' => 1), 'memcache');
  186. $result = Cache::read('test', 'memcache');
  187. $this->assertFalse($result);
  188. $data = 'this is a test of the emergency broadcasting system';
  189. $result = Cache::write('other_test', $data, 'memcache');
  190. $this->assertTrue($result);
  191. sleep(2);
  192. $result = Cache::read('other_test', 'memcache');
  193. $this->assertFalse($result);
  194. Cache::set(array('duration' => "+1 second"), 'memcache');
  195. $data = 'this is a test of the emergency broadcasting system';
  196. $result = Cache::write('other_test', $data, 'memcache');
  197. $this->assertTrue($result);
  198. sleep(2);
  199. $result = Cache::read('other_test', 'memcache');
  200. $this->assertFalse($result);
  201. Cache::config('memcache', array('duration' => '+1 second'));
  202. sleep(2);
  203. $result = Cache::read('other_test', 'memcache');
  204. $this->assertFalse($result);
  205. Cache::config('memcache', array('duration' => '+29 days'));
  206. $data = 'this is a test of the emergency broadcasting system';
  207. $result = Cache::write('long_expiry_test', $data, 'memcache');
  208. $this->assertTrue($result);
  209. sleep(2);
  210. $result = Cache::read('long_expiry_test', 'memcache');
  211. $expecting = $data;
  212. $this->assertEquals($expecting, $result);
  213. Cache::config('memcache', array('duration' => 3600));
  214. }
  215. /**
  216. * testDeleteCache method
  217. *
  218. * @return void
  219. */
  220. public function testDeleteCache() {
  221. $data = 'this is a test of the emergency broadcasting system';
  222. $result = Cache::write('delete_test', $data, 'memcache');
  223. $this->assertTrue($result);
  224. $result = Cache::delete('delete_test', 'memcache');
  225. $this->assertTrue($result);
  226. }
  227. /**
  228. * testDecrement method
  229. *
  230. * @return void
  231. */
  232. public function testDecrement() {
  233. $result = Cache::write('test_decrement', 5, 'memcache');
  234. $this->assertTrue($result);
  235. $result = Cache::decrement('test_decrement', 1, 'memcache');
  236. $this->assertEquals(4, $result);
  237. $result = Cache::read('test_decrement', 'memcache');
  238. $this->assertEquals(4, $result);
  239. $result = Cache::decrement('test_decrement', 2, 'memcache');
  240. $this->assertEquals(2, $result);
  241. $result = Cache::read('test_decrement', 'memcache');
  242. $this->assertEquals(2, $result);
  243. }
  244. /**
  245. * testIncrement method
  246. *
  247. * @return void
  248. */
  249. public function testIncrement() {
  250. $result = Cache::write('test_increment', 5, 'memcache');
  251. $this->assertTrue($result);
  252. $result = Cache::increment('test_increment', 1, 'memcache');
  253. $this->assertEquals(6, $result);
  254. $result = Cache::read('test_increment', 'memcache');
  255. $this->assertEquals(6, $result);
  256. $result = Cache::increment('test_increment', 2, 'memcache');
  257. $this->assertEquals(8, $result);
  258. $result = Cache::read('test_increment', 'memcache');
  259. $this->assertEquals(8, $result);
  260. }
  261. /**
  262. * test that configurations don't conflict, when a file engine is declared after a memcache one.
  263. *
  264. * @return void
  265. */
  266. public function testConfigurationConflict() {
  267. Cache::config('long_memcache', array(
  268. 'engine' => 'Memcache',
  269. 'duration' => '+2 seconds',
  270. 'servers' => array('127.0.0.1:11211'),
  271. ));
  272. Cache::config('short_memcache', array(
  273. 'engine' => 'Memcache',
  274. 'duration' => '+1 seconds',
  275. 'servers' => array('127.0.0.1:11211'),
  276. ));
  277. Cache::config('some_file', array('engine' => 'File'));
  278. $this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcache'));
  279. $this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcache'));
  280. $this->assertEquals('yay', Cache::read('duration_test', 'long_memcache'), 'Value was not read %s');
  281. $this->assertEquals('boo', Cache::read('short_duration_test', 'short_memcache'), 'Value was not read %s');
  282. sleep(1);
  283. $this->assertEquals('yay', Cache::read('duration_test', 'long_memcache'), 'Value was not read %s');
  284. sleep(2);
  285. $this->assertFalse(Cache::read('short_duration_test', 'short_memcache'), 'Cache was not invalidated %s');
  286. $this->assertFalse(Cache::read('duration_test', 'long_memcache'), 'Value did not expire %s');
  287. Cache::delete('duration_test', 'long_memcache');
  288. Cache::delete('short_duration_test', 'short_memcache');
  289. }
  290. /**
  291. * test clearing memcache.
  292. *
  293. * @return void
  294. */
  295. public function testClear() {
  296. Cache::config('memcache2', array(
  297. 'engine' => 'Memcache',
  298. 'prefix' => 'cake2_',
  299. 'duration' => 3600
  300. ));
  301. Cache::write('some_value', 'cache1', 'memcache');
  302. $result = Cache::clear(true, 'memcache');
  303. $this->assertTrue($result);
  304. $this->assertEquals('cache1', Cache::read('some_value', 'memcache'));
  305. Cache::write('some_value', 'cache2', 'memcache2');
  306. $result = Cache::clear(false, 'memcache');
  307. $this->assertTrue($result);
  308. $this->assertFalse(Cache::read('some_value', 'memcache'));
  309. $this->assertEquals('cache2', Cache::read('some_value', 'memcache2'));
  310. Cache::clear(false, 'memcache2');
  311. }
  312. /**
  313. * test that a 0 duration can successfully write.
  314. *
  315. * @return void
  316. */
  317. public function testZeroDuration() {
  318. Cache::config('memcache', array('duration' => 0));
  319. $result = Cache::write('test_key', 'written!', 'memcache');
  320. $this->assertTrue($result);
  321. $result = Cache::read('test_key', 'memcache');
  322. $this->assertEquals('written!', $result);
  323. }
  324. /**
  325. * test that durations greater than 30 days never expire
  326. *
  327. * @return void
  328. */
  329. public function testLongDurationEqualToZero() {
  330. $memcache = new TestMemcacheEngine();
  331. $memcache->settings['compress'] = false;
  332. $mock = $this->getMock('Memcache');
  333. $memcache->setMemcache($mock);
  334. $mock->expects($this->once())
  335. ->method('set')
  336. ->with('key', 'value', false, 0);
  337. $value = 'value';
  338. $memcache->write('key', $value, 50 * DAY);
  339. }
  340. /**
  341. * Tests that configuring groups for stored keys return the correct values when read/written
  342. * Shows that altering the group value is equivalent to deleting all keys under the same
  343. * group
  344. *
  345. * @return void
  346. */
  347. public function testGroupReadWrite() {
  348. Cache::config('memcache_groups', array(
  349. 'engine' => 'Memcache',
  350. 'duration' => 3600,
  351. 'groups' => array('group_a', 'group_b')
  352. ));
  353. Cache::config('memcache_helper', array(
  354. 'engine' => 'Memcache',
  355. 'duration' => 3600,
  356. 'prefix' => ''
  357. ));
  358. $this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
  359. $this->assertEquals('value', Cache::read('test_groups', 'memcache_groups'));
  360. Cache::increment('group_a', 1, 'memcache_helper');
  361. $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
  362. $this->assertTrue(Cache::write('test_groups', 'value2', 'memcache_groups'));
  363. $this->assertEquals('value2', Cache::read('test_groups', 'memcache_groups'));
  364. Cache::increment('group_b', 1, 'memcache_helper');
  365. $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
  366. $this->assertTrue(Cache::write('test_groups', 'value3', 'memcache_groups'));
  367. $this->assertEquals('value3', Cache::read('test_groups', 'memcache_groups'));
  368. }
  369. }