MemcachedEngineTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <?php
  2. /**
  3. * MemcachedEngineTest 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 2.5.0
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('Cache', 'Cache');
  21. App::uses('MemcachedEngine', 'Cache/Engine');
  22. /**
  23. * Class TestMemcachedEngine
  24. *
  25. * @package Cake.Test.Case.Cache.Engine
  26. */
  27. class TestMemcachedEngine extends MemcachedEngine {
  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 setMemcached($memcached) {
  38. $this->_Memcached = $memcached;
  39. }
  40. public function getMemcached() {
  41. return $this->_Memcached;
  42. }
  43. }
  44. /**
  45. * MemcachedEngineTest class
  46. *
  47. * @package Cake.Test.Case.Cache.Engine
  48. */
  49. class MemcachedEngineTest extends CakeTestCase {
  50. /**
  51. * setUp method
  52. *
  53. * @return void
  54. */
  55. public function setUp() {
  56. parent::setUp();
  57. $this->skipIf(!class_exists('Memcached'), 'Memcached is not installed or configured properly.');
  58. Cache::config('memcached', array(
  59. 'engine' => 'Memcached',
  60. 'prefix' => 'cake_',
  61. 'duration' => 3600
  62. ));
  63. }
  64. /**
  65. * tearDown method
  66. *
  67. * @return void
  68. */
  69. public function tearDown() {
  70. parent::tearDown();
  71. Cache::drop('memcached');
  72. Cache::drop('memcached_groups');
  73. Cache::drop('memcached_helper');
  74. Cache::config('default');
  75. }
  76. /**
  77. * testSettings method
  78. *
  79. * @return void
  80. */
  81. public function testSettings() {
  82. $settings = Cache::settings('memcached');
  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' => false,
  90. 'compress' => false,
  91. 'engine' => 'Memcached',
  92. 'login' => null,
  93. 'password' => null,
  94. 'groups' => array()
  95. );
  96. $this->assertEquals($expecting, $settings);
  97. }
  98. /**
  99. * testCompressionSetting method
  100. *
  101. * @return void
  102. */
  103. public function testCompressionSetting() {
  104. $Memcached = new TestMemcachedEngine();
  105. $Memcached->init(array(
  106. 'engine' => 'Memcached',
  107. 'servers' => array('127.0.0.1:11211'),
  108. 'compress' => false
  109. ));
  110. $this->assertFalse($Memcached->getMemcached()->getOption(Memcached::OPT_COMPRESSION));
  111. $MemcachedCompressed = new TestMemcachedEngine();
  112. $MemcachedCompressed->init(array(
  113. 'engine' => 'Memcached',
  114. 'servers' => array('127.0.0.1:11211'),
  115. 'compress' => true
  116. ));
  117. $this->assertTrue($MemcachedCompressed->getMemcached()->getOption(Memcached::OPT_COMPRESSION));
  118. }
  119. /**
  120. * test using authentication without memcached installed with SASL support
  121. * throw an exception
  122. *
  123. * @return void
  124. */
  125. public function testSaslAuthException() {
  126. $Memcached = new TestMemcachedEngine();
  127. $settings = array(
  128. 'engine' => 'Memcached',
  129. 'servers' => array('127.0.0.1:11211'),
  130. 'persistent' => false,
  131. 'login' => 'test',
  132. 'password' => 'password'
  133. );
  134. $this->skipIf(
  135. method_exists($Memcached->getMemcached(), 'setSaslAuthData'),
  136. 'Memcached extension is installed with SASL support'
  137. );
  138. $this->setExpectedException(
  139. 'CacheException', 'Memcached extension is not build with SASL support'
  140. );
  141. $Memcached->init($settings);
  142. }
  143. /**
  144. * testSettings method
  145. *
  146. * @return void
  147. */
  148. public function testMultipleServers() {
  149. $servers = array('127.0.0.1:11211', '127.0.0.1:11222');
  150. $available = true;
  151. $Memcached = new Memcached();
  152. foreach ($servers as $server) {
  153. list($host, $port) = explode(':', $server);
  154. //@codingStandardsIgnoreStart
  155. if (!$Memcached->addServer($host, $port)) {
  156. $available = false;
  157. }
  158. //@codingStandardsIgnoreEnd
  159. }
  160. $this->skipIf(!$available, 'Need memcached servers at ' . implode(', ', $servers) . ' to run this test.');
  161. $Memcached = new MemcachedEngine();
  162. $Memcached->init(array('engine' => 'Memcached', 'servers' => $servers));
  163. $settings = $Memcached->settings();
  164. $this->assertEquals($settings['servers'], $servers);
  165. Cache::drop('dual_server');
  166. }
  167. /**
  168. * test connecting to an ipv6 server.
  169. *
  170. * @return void
  171. */
  172. public function testConnectIpv6() {
  173. $Memcached = new MemcachedEngine();
  174. $result = $Memcached->init(array(
  175. 'prefix' => 'cake_',
  176. 'duration' => 200,
  177. 'engine' => 'Memcached',
  178. 'servers' => array(
  179. '[::1]:11211'
  180. )
  181. ));
  182. $this->assertTrue($result);
  183. }
  184. /**
  185. * test non latin domains.
  186. *
  187. * @return void
  188. */
  189. public function testParseServerStringNonLatin() {
  190. $Memcached = new TestMemcachedEngine();
  191. $result = $Memcached->parseServerString('schülervz.net:13211');
  192. $this->assertEquals(array('schülervz.net', '13211'), $result);
  193. $result = $Memcached->parseServerString('sülül:1111');
  194. $this->assertEquals(array('sülül', '1111'), $result);
  195. }
  196. /**
  197. * test unix sockets.
  198. *
  199. * @return void
  200. */
  201. public function testParseServerStringUnix() {
  202. $Memcached = new TestMemcachedEngine();
  203. $result = $Memcached->parseServerString('unix:///path/to/memcachedd.sock');
  204. $this->assertEquals(array('unix:///path/to/memcachedd.sock', 0), $result);
  205. }
  206. /**
  207. * testReadAndWriteCache method
  208. *
  209. * @return void
  210. */
  211. public function testReadAndWriteCache() {
  212. Cache::set(array('duration' => 1), null, 'memcached');
  213. $result = Cache::read('test', 'memcached');
  214. $expecting = '';
  215. $this->assertEquals($expecting, $result);
  216. $data = 'this is a test of the emergency broadcasting system';
  217. $result = Cache::write('test', $data, 'memcached');
  218. $this->assertTrue($result);
  219. $result = Cache::read('test', 'memcached');
  220. $expecting = $data;
  221. $this->assertEquals($expecting, $result);
  222. Cache::delete('test', 'memcached');
  223. }
  224. /**
  225. * testExpiry method
  226. *
  227. * @return void
  228. */
  229. public function testExpiry() {
  230. Cache::set(array('duration' => 1), 'memcached');
  231. $result = Cache::read('test', 'memcached');
  232. $this->assertFalse($result);
  233. $data = 'this is a test of the emergency broadcasting system';
  234. $result = Cache::write('other_test', $data, 'memcached');
  235. $this->assertTrue($result);
  236. sleep(2);
  237. $result = Cache::read('other_test', 'memcached');
  238. $this->assertFalse($result);
  239. Cache::set(array('duration' => "+1 second"), 'memcached');
  240. $data = 'this is a test of the emergency broadcasting system';
  241. $result = Cache::write('other_test', $data, 'memcached');
  242. $this->assertTrue($result);
  243. sleep(3);
  244. $result = Cache::read('other_test', 'memcached');
  245. $this->assertFalse($result);
  246. Cache::config('memcached', array('duration' => '+1 second'));
  247. $result = Cache::read('other_test', 'memcached');
  248. $this->assertFalse($result);
  249. Cache::config('memcached', array('duration' => '+29 days'));
  250. $data = 'this is a test of the emergency broadcasting system';
  251. $result = Cache::write('long_expiry_test', $data, 'memcached');
  252. $this->assertTrue($result);
  253. sleep(2);
  254. $result = Cache::read('long_expiry_test', 'memcached');
  255. $expecting = $data;
  256. $this->assertEquals($expecting, $result);
  257. Cache::config('memcached', array('duration' => 3600));
  258. }
  259. /**
  260. * testDeleteCache method
  261. *
  262. * @return void
  263. */
  264. public function testDeleteCache() {
  265. $data = 'this is a test of the emergency broadcasting system';
  266. $result = Cache::write('delete_test', $data, 'memcached');
  267. $this->assertTrue($result);
  268. $result = Cache::delete('delete_test', 'memcached');
  269. $this->assertTrue($result);
  270. }
  271. /**
  272. * testDecrement method
  273. *
  274. * @return void
  275. */
  276. public function testDecrement() {
  277. $result = Cache::write('test_decrement', 5, 'memcached');
  278. $this->assertTrue($result);
  279. $result = Cache::decrement('test_decrement', 1, 'memcached');
  280. $this->assertEquals(4, $result);
  281. $result = Cache::read('test_decrement', 'memcached');
  282. $this->assertEquals(4, $result);
  283. $result = Cache::decrement('test_decrement', 2, 'memcached');
  284. $this->assertEquals(2, $result);
  285. $result = Cache::read('test_decrement', 'memcached');
  286. $this->assertEquals(2, $result);
  287. Cache::delete('test_decrement', 'memcached');
  288. }
  289. /**
  290. * test decrementing compressed keys
  291. *
  292. * @return void
  293. */
  294. public function testDecrementCompressedKeys() {
  295. Cache::config('compressed_memcached', array(
  296. 'engine' => 'Memcached',
  297. 'duration' => '+2 seconds',
  298. 'servers' => array('127.0.0.1:11211'),
  299. 'compress' => true
  300. ));
  301. $result = Cache::write('test_decrement', 5, 'compressed_memcached');
  302. $this->assertTrue($result);
  303. $result = Cache::decrement('test_decrement', 1, 'compressed_memcached');
  304. $this->assertEquals(4, $result);
  305. $result = Cache::read('test_decrement', 'compressed_memcached');
  306. $this->assertEquals(4, $result);
  307. $result = Cache::decrement('test_decrement', 2, 'compressed_memcached');
  308. $this->assertEquals(2, $result);
  309. $result = Cache::read('test_decrement', 'compressed_memcached');
  310. $this->assertEquals(2, $result);
  311. Cache::delete('test_decrement', 'compressed_memcached');
  312. }
  313. /**
  314. * testIncrement method
  315. *
  316. * @return void
  317. */
  318. public function testIncrement() {
  319. $result = Cache::write('test_increment', 5, 'memcached');
  320. $this->assertTrue($result);
  321. $result = Cache::increment('test_increment', 1, 'memcached');
  322. $this->assertEquals(6, $result);
  323. $result = Cache::read('test_increment', 'memcached');
  324. $this->assertEquals(6, $result);
  325. $result = Cache::increment('test_increment', 2, 'memcached');
  326. $this->assertEquals(8, $result);
  327. $result = Cache::read('test_increment', 'memcached');
  328. $this->assertEquals(8, $result);
  329. Cache::delete('test_increment', 'memcached');
  330. }
  331. /**
  332. * test incrementing compressed keys
  333. *
  334. * @return void
  335. */
  336. public function testIncrementCompressedKeys() {
  337. Cache::config('compressed_memcached', array(
  338. 'engine' => 'Memcached',
  339. 'duration' => '+2 seconds',
  340. 'servers' => array('127.0.0.1:11211'),
  341. 'compress' => true
  342. ));
  343. $result = Cache::write('test_increment', 5, 'compressed_memcached');
  344. $this->assertTrue($result);
  345. $result = Cache::increment('test_increment', 1, 'compressed_memcached');
  346. $this->assertEquals(6, $result);
  347. $result = Cache::read('test_increment', 'compressed_memcached');
  348. $this->assertEquals(6, $result);
  349. $result = Cache::increment('test_increment', 2, 'compressed_memcached');
  350. $this->assertEquals(8, $result);
  351. $result = Cache::read('test_increment', 'compressed_memcached');
  352. $this->assertEquals(8, $result);
  353. Cache::delete('test_increment', 'compressed_memcached');
  354. }
  355. /**
  356. * test that configurations don't conflict, when a file engine is declared after a memcached one.
  357. *
  358. * @return void
  359. */
  360. public function testConfigurationConflict() {
  361. Cache::config('long_memcached', array(
  362. 'engine' => 'Memcached',
  363. 'duration' => '+2 seconds',
  364. 'servers' => array('127.0.0.1:11211'),
  365. ));
  366. Cache::config('short_memcached', array(
  367. 'engine' => 'Memcached',
  368. 'duration' => '+1 seconds',
  369. 'servers' => array('127.0.0.1:11211'),
  370. ));
  371. Cache::config('some_file', array('engine' => 'File'));
  372. $this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcached'));
  373. $this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcached'));
  374. $this->assertEquals('yay', Cache::read('duration_test', 'long_memcached'), 'Value was not read %s');
  375. $this->assertEquals('boo', Cache::read('short_duration_test', 'short_memcached'), 'Value was not read %s');
  376. sleep(1);
  377. $this->assertEquals('yay', Cache::read('duration_test', 'long_memcached'), 'Value was not read %s');
  378. sleep(2);
  379. $this->assertFalse(Cache::read('short_duration_test', 'short_memcached'), 'Cache was not invalidated %s');
  380. $this->assertFalse(Cache::read('duration_test', 'long_memcached'), 'Value did not expire %s');
  381. Cache::delete('duration_test', 'long_memcached');
  382. Cache::delete('short_duration_test', 'short_memcached');
  383. }
  384. /**
  385. * test clearing memcached.
  386. *
  387. * @return void
  388. */
  389. public function testClear() {
  390. Cache::config('memcached2', array(
  391. 'engine' => 'Memcached',
  392. 'prefix' => 'cake2_',
  393. 'duration' => 3600
  394. ));
  395. Cache::write('some_value', 'cache1', 'memcached');
  396. $result = Cache::clear(true, 'memcached');
  397. $this->assertTrue($result);
  398. $this->assertEquals('cache1', Cache::read('some_value', 'memcached'));
  399. Cache::write('some_value', 'cache2', 'memcached2');
  400. $result = Cache::clear(false, 'memcached');
  401. $this->assertTrue($result);
  402. $this->assertFalse(Cache::read('some_value', 'memcached'));
  403. $this->assertEquals('cache2', Cache::read('some_value', 'memcached2'));
  404. Cache::clear(false, 'memcached2');
  405. }
  406. /**
  407. * test that a 0 duration can successfully write.
  408. *
  409. * @return void
  410. */
  411. public function testZeroDuration() {
  412. Cache::config('memcached', array('duration' => 0));
  413. $result = Cache::write('test_key', 'written!', 'memcached');
  414. $this->assertTrue($result);
  415. $result = Cache::read('test_key', 'memcached');
  416. $this->assertEquals('written!', $result);
  417. }
  418. /**
  419. * test that durations greater than 30 days never expire
  420. *
  421. * @return void
  422. */
  423. public function testLongDurationEqualToZero() {
  424. $memcached = new TestMemcachedEngine();
  425. $memcached->settings['compress'] = false;
  426. $mock = $this->getMock('Memcached');
  427. $memcached->setMemcached($mock);
  428. $mock->expects($this->once())
  429. ->method('set')
  430. ->with('key', 'value', 0);
  431. $value = 'value';
  432. $memcached->write('key', $value, 50 * DAY);
  433. }
  434. /**
  435. * Tests that configuring groups for stored keys return the correct values when read/written
  436. * Shows that altering the group value is equivalent to deleting all keys under the same
  437. * group
  438. *
  439. * @return void
  440. */
  441. public function testGroupReadWrite() {
  442. Cache::config('memcached_groups', array(
  443. 'engine' => 'Memcached',
  444. 'duration' => 3600,
  445. 'groups' => array('group_a', 'group_b'),
  446. 'prefix' => 'test_'
  447. ));
  448. Cache::config('memcached_helper', array(
  449. 'engine' => 'Memcached',
  450. 'duration' => 3600,
  451. 'prefix' => 'test_'
  452. ));
  453. $this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
  454. $this->assertEquals('value', Cache::read('test_groups', 'memcached_groups'));
  455. Cache::increment('group_a', 1, 'memcached_helper');
  456. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  457. $this->assertTrue(Cache::write('test_groups', 'value2', 'memcached_groups'));
  458. $this->assertEquals('value2', Cache::read('test_groups', 'memcached_groups'));
  459. Cache::increment('group_b', 1, 'memcached_helper');
  460. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  461. $this->assertTrue(Cache::write('test_groups', 'value3', 'memcached_groups'));
  462. $this->assertEquals('value3', Cache::read('test_groups', 'memcached_groups'));
  463. }
  464. /**
  465. * Tests that deleteing from a groups-enabled config is possible
  466. *
  467. * @return void
  468. */
  469. public function testGroupDelete() {
  470. Cache::config('memcached_groups', array(
  471. 'engine' => 'Memcached',
  472. 'duration' => 3600,
  473. 'groups' => array('group_a', 'group_b')
  474. ));
  475. $this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
  476. $this->assertEquals('value', Cache::read('test_groups', 'memcached_groups'));
  477. $this->assertTrue(Cache::delete('test_groups', 'memcached_groups'));
  478. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  479. }
  480. /**
  481. * Test clearing a cache group
  482. *
  483. * @return void
  484. */
  485. public function testGroupClear() {
  486. Cache::config('memcached_groups', array(
  487. 'engine' => 'Memcached',
  488. 'duration' => 3600,
  489. 'groups' => array('group_a', 'group_b')
  490. ));
  491. $this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
  492. $this->assertTrue(Cache::clearGroup('group_a', 'memcached_groups'));
  493. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  494. $this->assertTrue(Cache::write('test_groups', 'value2', 'memcached_groups'));
  495. $this->assertTrue(Cache::clearGroup('group_b', 'memcached_groups'));
  496. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  497. }
  498. }