MemcacheEngineTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. 'groups' => array()
  86. );
  87. $this->assertEquals($expecting, $settings);
  88. }
  89. /**
  90. * testSettings method
  91. *
  92. * @return void
  93. */
  94. public function testMultipleServers() {
  95. $servers = array('127.0.0.1:11211', '127.0.0.1:11222');
  96. $available = true;
  97. $Memcache = new Memcache();
  98. foreach ($servers as $server) {
  99. list($host, $port) = explode(':', $server);
  100. if (!@$Memcache->connect($host, $port)) {
  101. $available = false;
  102. }
  103. }
  104. $this->skipIf(!$available, 'Need memcache servers at ' . implode(', ', $servers) . ' to run this test.');
  105. $Memcache = new MemcacheEngine();
  106. $Memcache->init(array('engine' => 'Memcache', 'servers' => $servers));
  107. $servers = array_keys($Memcache->__Memcache->getExtendedStats());
  108. $settings = $Memcache->settings();
  109. $this->assertEquals($settings['servers'], $servers);
  110. Cache::drop('dual_server');
  111. }
  112. /**
  113. * testConnect method
  114. *
  115. * @return void
  116. */
  117. public function testConnect() {
  118. $Memcache = new MemcacheEngine();
  119. $Memcache->init(Cache::settings('memcache'));
  120. $result = $Memcache->connect('127.0.0.1');
  121. $this->assertTrue($result);
  122. }
  123. /**
  124. * test connecting to an ipv6 server.
  125. *
  126. * @return void
  127. */
  128. public function testConnectIpv6() {
  129. $Memcache = new MemcacheEngine();
  130. $result = $Memcache->init(array(
  131. 'prefix' => 'cake_',
  132. 'duration' => 200,
  133. 'engine' => 'Memcache',
  134. 'servers' => array(
  135. '[::1]:11211'
  136. )
  137. ));
  138. $this->assertTrue($result);
  139. }
  140. /**
  141. * test non latin domains.
  142. *
  143. * @return void
  144. */
  145. public function testParseServerStringNonLatin() {
  146. $Memcache = new TestMemcacheEngine();
  147. $result = $Memcache->parseServerString('schülervz.net:13211');
  148. $this->assertEquals(array('schülervz.net', '13211'), $result);
  149. $result = $Memcache->parseServerString('sülül:1111');
  150. $this->assertEquals(array('sülül', '1111'), $result);
  151. }
  152. /**
  153. * test unix sockets.
  154. *
  155. * @return void
  156. */
  157. public function testParseServerStringUnix() {
  158. $Memcache = new TestMemcacheEngine();
  159. $result = $Memcache->parseServerString('unix:///path/to/memcached.sock');
  160. $this->assertEquals(array('unix:///path/to/memcached.sock', 0), $result);
  161. }
  162. /**
  163. * testReadAndWriteCache method
  164. *
  165. * @return void
  166. */
  167. public function testReadAndWriteCache() {
  168. Cache::set(array('duration' => 1), null, 'memcache');
  169. $result = Cache::read('test', 'memcache');
  170. $expecting = '';
  171. $this->assertEquals($expecting, $result);
  172. $data = 'this is a test of the emergency broadcasting system';
  173. $result = Cache::write('test', $data, 'memcache');
  174. $this->assertTrue($result);
  175. $result = Cache::read('test', 'memcache');
  176. $expecting = $data;
  177. $this->assertEquals($expecting, $result);
  178. Cache::delete('test', 'memcache');
  179. }
  180. /**
  181. * testExpiry method
  182. *
  183. * @return void
  184. */
  185. public function testExpiry() {
  186. Cache::set(array('duration' => 1), 'memcache');
  187. $result = Cache::read('test', 'memcache');
  188. $this->assertFalse($result);
  189. $data = 'this is a test of the emergency broadcasting system';
  190. $result = Cache::write('other_test', $data, 'memcache');
  191. $this->assertTrue($result);
  192. sleep(2);
  193. $result = Cache::read('other_test', 'memcache');
  194. $this->assertFalse($result);
  195. Cache::set(array('duration' => "+1 second"), 'memcache');
  196. $data = 'this is a test of the emergency broadcasting system';
  197. $result = Cache::write('other_test', $data, 'memcache');
  198. $this->assertTrue($result);
  199. sleep(2);
  200. $result = Cache::read('other_test', 'memcache');
  201. $this->assertFalse($result);
  202. Cache::config('memcache', array('duration' => '+1 second'));
  203. sleep(2);
  204. $result = Cache::read('other_test', 'memcache');
  205. $this->assertFalse($result);
  206. Cache::config('memcache', array('duration' => '+29 days'));
  207. $data = 'this is a test of the emergency broadcasting system';
  208. $result = Cache::write('long_expiry_test', $data, 'memcache');
  209. $this->assertTrue($result);
  210. sleep(2);
  211. $result = Cache::read('long_expiry_test', 'memcache');
  212. $expecting = $data;
  213. $this->assertEquals($expecting, $result);
  214. Cache::config('memcache', array('duration' => 3600));
  215. }
  216. /**
  217. * testDeleteCache method
  218. *
  219. * @return void
  220. */
  221. public function testDeleteCache() {
  222. $data = 'this is a test of the emergency broadcasting system';
  223. $result = Cache::write('delete_test', $data, 'memcache');
  224. $this->assertTrue($result);
  225. $result = Cache::delete('delete_test', 'memcache');
  226. $this->assertTrue($result);
  227. }
  228. /**
  229. * testDecrement method
  230. *
  231. * @return void
  232. */
  233. public function testDecrement() {
  234. $result = Cache::write('test_decrement', 5, 'memcache');
  235. $this->assertTrue($result);
  236. $result = Cache::decrement('test_decrement', 1, 'memcache');
  237. $this->assertEquals(4, $result);
  238. $result = Cache::read('test_decrement', 'memcache');
  239. $this->assertEquals(4, $result);
  240. $result = Cache::decrement('test_decrement', 2, 'memcache');
  241. $this->assertEquals(2, $result);
  242. $result = Cache::read('test_decrement', 'memcache');
  243. $this->assertEquals(2, $result);
  244. }
  245. /**
  246. * testIncrement method
  247. *
  248. * @return void
  249. */
  250. public function testIncrement() {
  251. $result = Cache::write('test_increment', 5, 'memcache');
  252. $this->assertTrue($result);
  253. $result = Cache::increment('test_increment', 1, 'memcache');
  254. $this->assertEquals(6, $result);
  255. $result = Cache::read('test_increment', 'memcache');
  256. $this->assertEquals(6, $result);
  257. $result = Cache::increment('test_increment', 2, 'memcache');
  258. $this->assertEquals(8, $result);
  259. $result = Cache::read('test_increment', 'memcache');
  260. $this->assertEquals(8, $result);
  261. }
  262. /**
  263. * test that configurations don't conflict, when a file engine is declared after a memcache one.
  264. *
  265. * @return void
  266. */
  267. public function testConfigurationConflict() {
  268. Cache::config('long_memcache', array(
  269. 'engine' => 'Memcache',
  270. 'duration' => '+2 seconds',
  271. 'servers' => array('127.0.0.1:11211'),
  272. ));
  273. Cache::config('short_memcache', array(
  274. 'engine' => 'Memcache',
  275. 'duration' => '+1 seconds',
  276. 'servers' => array('127.0.0.1:11211'),
  277. ));
  278. Cache::config('some_file', array('engine' => 'File'));
  279. $this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcache'));
  280. $this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcache'));
  281. $this->assertEquals('yay', Cache::read('duration_test', 'long_memcache'), 'Value was not read %s');
  282. $this->assertEquals('boo', Cache::read('short_duration_test', 'short_memcache'), 'Value was not read %s');
  283. sleep(1);
  284. $this->assertEquals('yay', Cache::read('duration_test', 'long_memcache'), 'Value was not read %s');
  285. sleep(2);
  286. $this->assertFalse(Cache::read('short_duration_test', 'short_memcache'), 'Cache was not invalidated %s');
  287. $this->assertFalse(Cache::read('duration_test', 'long_memcache'), 'Value did not expire %s');
  288. Cache::delete('duration_test', 'long_memcache');
  289. Cache::delete('short_duration_test', 'short_memcache');
  290. }
  291. /**
  292. * test clearing memcache.
  293. *
  294. * @return void
  295. */
  296. public function testClear() {
  297. Cache::config('memcache2', array(
  298. 'engine' => 'Memcache',
  299. 'prefix' => 'cake2_',
  300. 'duration' => 3600
  301. ));
  302. Cache::write('some_value', 'cache1', 'memcache');
  303. $result = Cache::clear(true, 'memcache');
  304. $this->assertTrue($result);
  305. $this->assertEquals('cache1', Cache::read('some_value', 'memcache'));
  306. Cache::write('some_value', 'cache2', 'memcache2');
  307. $result = Cache::clear(false, 'memcache');
  308. $this->assertTrue($result);
  309. $this->assertFalse(Cache::read('some_value', 'memcache'));
  310. $this->assertEquals('cache2', Cache::read('some_value', 'memcache2'));
  311. Cache::clear(false, 'memcache2');
  312. }
  313. /**
  314. * test that a 0 duration can successfully write.
  315. *
  316. * @return void
  317. */
  318. public function testZeroDuration() {
  319. Cache::config('memcache', array('duration' => 0));
  320. $result = Cache::write('test_key', 'written!', 'memcache');
  321. $this->assertTrue($result);
  322. $result = Cache::read('test_key', 'memcache');
  323. $this->assertEquals('written!', $result);
  324. }
  325. /**
  326. * test that durations greater than 30 days never expire
  327. *
  328. * @return void
  329. */
  330. public function testLongDurationEqualToZero() {
  331. $memcache = new TestMemcacheEngine();
  332. $memcache->settings['compress'] = false;
  333. $mock = $this->getMock('Memcache');
  334. $memcache->setMemcache($mock);
  335. $mock->expects($this->once())
  336. ->method('set')
  337. ->with('key', 'value', false, 0);
  338. $value = 'value';
  339. $memcache->write('key', $value, 50 * DAY);
  340. }
  341. /**
  342. * Tests that configuring groups for stored keys return the correct values when read/written
  343. * Shows that altering the group value is equivalent to deleting all keys under the same
  344. * group
  345. *
  346. * @return void
  347. */
  348. public function testGroupReadWrite() {
  349. Cache::config('memcache_groups', array(
  350. 'engine' => 'Memcache',
  351. 'duration' => 3600,
  352. 'groups' => array('group_a', 'group_b'),
  353. 'prefix' => 'test_'
  354. ));
  355. Cache::config('memcache_helper', array(
  356. 'engine' => 'Memcache',
  357. 'duration' => 3600,
  358. 'prefix' => 'test_'
  359. ));
  360. $this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
  361. $this->assertEquals('value', Cache::read('test_groups', 'memcache_groups'));
  362. Cache::increment('group_a', 1, 'memcache_helper');
  363. $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
  364. $this->assertTrue(Cache::write('test_groups', 'value2', 'memcache_groups'));
  365. $this->assertEquals('value2', Cache::read('test_groups', 'memcache_groups'));
  366. Cache::increment('group_b', 1, 'memcache_helper');
  367. $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
  368. $this->assertTrue(Cache::write('test_groups', 'value3', 'memcache_groups'));
  369. $this->assertEquals('value3', Cache::read('test_groups', 'memcache_groups'));
  370. }
  371. /**
  372. * Tests that deleteing from a groups-enabled config is possible
  373. *
  374. * @return void
  375. */
  376. public function testGroupDelete() {
  377. Cache::config('memcache_groups', array(
  378. 'engine' => 'Memcache',
  379. 'duration' => 3600,
  380. 'groups' => array('group_a', 'group_b')
  381. ));
  382. $this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
  383. $this->assertEquals('value', Cache::read('test_groups', 'memcache_groups'));
  384. $this->assertTrue(Cache::delete('test_groups', 'memcache_groups'));
  385. $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
  386. }
  387. /**
  388. * Test clearing a cache group
  389. *
  390. * @return void
  391. **/
  392. public function testGroupClear() {
  393. Cache::config('memcache_groups', array(
  394. 'engine' => 'Memcache',
  395. 'duration' => 3600,
  396. 'groups' => array('group_a', 'group_b')
  397. ));
  398. $this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
  399. $this->assertTrue(Cache::clearGroup('group_a', 'memcache_groups'));
  400. $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
  401. $this->assertTrue(Cache::write('test_groups', 'value2', 'memcache_groups'));
  402. $this->assertTrue(Cache::clearGroup('group_b', 'memcache_groups'));
  403. $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
  404. }
  405. }