MemcacheEngineTest.php 13 KB

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