MemcachedEngineTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 2.5.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Cache\Engine;
  16. use Cake\Cache\Cache;
  17. use Cake\Cache\Engine\MemcachedEngine;
  18. use Cake\Core\Configure;
  19. use Cake\TestSuite\TestCase;
  20. use \Memcached;
  21. /**
  22. * Class TestMemcachedEngine
  23. *
  24. */
  25. class TestMemcachedEngine extends MemcachedEngine
  26. {
  27. /**
  28. * public accessor to _parseServerString
  29. *
  30. * @param string $server
  31. * @return array
  32. */
  33. public function parseServerString($server)
  34. {
  35. return $this->_parseServerString($server);
  36. }
  37. public function setMemcached($memcached)
  38. {
  39. $this->_Memcached = $memcached;
  40. }
  41. public function getMemcached()
  42. {
  43. return $this->_Memcached;
  44. }
  45. }
  46. /**
  47. * MemcachedEngineTest class
  48. *
  49. */
  50. class MemcachedEngineTest extends TestCase
  51. {
  52. /**
  53. * setUp method
  54. *
  55. * @return void
  56. */
  57. public function setUp()
  58. {
  59. parent::setUp();
  60. $this->skipIf(!class_exists('Memcached'), 'Memcached is not installed or configured properly.');
  61. // @codingStandardsIgnoreStart
  62. $socket = @fsockopen('127.0.0.1', 11211, $errno, $errstr, 1);
  63. // @codingStandardsIgnoreEnd
  64. $this->skipIf(!$socket, 'Memcached is not running.');
  65. fclose($socket);
  66. $this->_configCache();
  67. }
  68. /**
  69. * Helper method for testing.
  70. *
  71. * @param array $config
  72. * @return void
  73. */
  74. protected function _configCache($config = [])
  75. {
  76. $defaults = [
  77. 'className' => 'Memcached',
  78. 'prefix' => 'cake_',
  79. 'duration' => 3600
  80. ];
  81. Cache::drop('memcached');
  82. Cache::config('memcached', array_merge($defaults, $config));
  83. }
  84. /**
  85. * tearDown method
  86. *
  87. * @return void
  88. */
  89. public function tearDown()
  90. {
  91. parent::tearDown();
  92. Cache::drop('memcached');
  93. Cache::drop('memcached2');
  94. Cache::drop('memcached_groups');
  95. Cache::drop('memcached_helper');
  96. Cache::drop('compressed_memcached');
  97. Cache::drop('long_memcached');
  98. Cache::drop('short_memcached');
  99. }
  100. /**
  101. * testConfig method
  102. *
  103. * @return void
  104. */
  105. public function testConfig()
  106. {
  107. $config = Cache::engine('memcached')->config();
  108. unset($config['path']);
  109. $expecting = [
  110. 'prefix' => 'cake_',
  111. 'duration' => 3600,
  112. 'probability' => 100,
  113. 'servers' => ['127.0.0.1'],
  114. 'persistent' => false,
  115. 'compress' => false,
  116. 'username' => null,
  117. 'password' => null,
  118. 'groups' => [],
  119. 'serialize' => 'php',
  120. 'options' => [],
  121. 'host' => null,
  122. 'port' => null,
  123. ];
  124. $this->assertEquals($expecting, $config);
  125. }
  126. /**
  127. * testCompressionSetting method
  128. *
  129. * @return void
  130. */
  131. public function testCompressionSetting()
  132. {
  133. $Memcached = new TestMemcachedEngine();
  134. $Memcached->init([
  135. 'engine' => 'Memcached',
  136. 'servers' => ['127.0.0.1:11211'],
  137. 'compress' => false
  138. ]);
  139. $this->assertFalse($Memcached->getMemcached()->getOption(\Memcached::OPT_COMPRESSION));
  140. $MemcachedCompressed = new TestMemcachedEngine();
  141. $MemcachedCompressed->init([
  142. 'engine' => 'Memcached',
  143. 'servers' => ['127.0.0.1:11211'],
  144. 'compress' => true
  145. ]);
  146. $this->assertTrue($MemcachedCompressed->getMemcached()->getOption(\Memcached::OPT_COMPRESSION));
  147. }
  148. /**
  149. * test setting options
  150. *
  151. * @return void
  152. */
  153. public function testOptionsSetting()
  154. {
  155. $memcached = new TestMemcachedEngine();
  156. $memcached->init([
  157. 'engine' => 'Memcached',
  158. 'servers' => ['127.0.0.1:11211'],
  159. 'options' => [
  160. Memcached::OPT_BINARY_PROTOCOL => true
  161. ]
  162. ]);
  163. $this->assertEquals(1, $memcached->getMemcached()->getOption(Memcached::OPT_BINARY_PROTOCOL));
  164. }
  165. /**
  166. * test accepts only valid serializer engine
  167. *
  168. * @return void
  169. */
  170. public function testInvalidSerializerSetting()
  171. {
  172. $Memcached = new TestMemcachedEngine();
  173. $config = [
  174. 'className' => 'Memcached',
  175. 'servers' => ['127.0.0.1:11211'],
  176. 'persistent' => false,
  177. 'serialize' => 'invalid_serializer'
  178. ];
  179. $this->setExpectedException(
  180. 'InvalidArgumentException',
  181. 'invalid_serializer is not a valid serializer engine for Memcached'
  182. );
  183. $Memcached->init($config);
  184. }
  185. /**
  186. * testPhpSerializerSetting method
  187. *
  188. * @return void
  189. */
  190. public function testPhpSerializerSetting()
  191. {
  192. $Memcached = new TestMemcachedEngine();
  193. $config = [
  194. 'className' => 'Memcached',
  195. 'servers' => ['127.0.0.1:11211'],
  196. 'persistent' => false,
  197. 'serialize' => 'php'
  198. ];
  199. $Memcached->init($config);
  200. $this->assertEquals(Memcached::SERIALIZER_PHP, $Memcached->getMemcached()->getOption(Memcached::OPT_SERIALIZER));
  201. }
  202. /**
  203. * testJsonSerializerSetting method
  204. *
  205. * @return void
  206. */
  207. public function testJsonSerializerSetting()
  208. {
  209. $this->skipIf(
  210. !Memcached::HAVE_JSON,
  211. 'Memcached extension is not compiled with json support'
  212. );
  213. $Memcached = new TestMemcachedEngine();
  214. $config = [
  215. 'engine' => 'Memcached',
  216. 'servers' => ['127.0.0.1:11211'],
  217. 'persistent' => false,
  218. 'serialize' => 'json'
  219. ];
  220. $Memcached->init($config);
  221. $this->assertEquals(Memcached::SERIALIZER_JSON, $Memcached->getMemcached()->getOption(Memcached::OPT_SERIALIZER));
  222. }
  223. /**
  224. * testIgbinarySerializerSetting method
  225. *
  226. * @return void
  227. */
  228. public function testIgbinarySerializerSetting()
  229. {
  230. $this->skipIf(
  231. !Memcached::HAVE_IGBINARY,
  232. 'Memcached extension is not compiled with igbinary support'
  233. );
  234. $Memcached = new TestMemcachedEngine();
  235. $config = [
  236. 'engine' => 'Memcached',
  237. 'servers' => ['127.0.0.1:11211'],
  238. 'persistent' => false,
  239. 'serialize' => 'igbinary'
  240. ];
  241. $Memcached->init($config);
  242. $this->assertEquals(Memcached::SERIALIZER_IGBINARY, $Memcached->getMemcached()->getOption(Memcached::OPT_SERIALIZER));
  243. }
  244. /**
  245. * testMsgpackSerializerSetting method
  246. *
  247. * @return void
  248. */
  249. public function testMsgpackSerializerSetting()
  250. {
  251. $this->skipIf(
  252. !defined('Memcached::HAVE_MSGPACK') || !Memcached::HAVE_MSGPACK,
  253. 'Memcached extension is not compiled with msgpack support'
  254. );
  255. $Memcached = new TestMemcachedEngine();
  256. $config = [
  257. 'engine' => 'Memcached',
  258. 'servers' => ['127.0.0.1:11211'],
  259. 'persistent' => false,
  260. 'serialize' => 'msgpack'
  261. ];
  262. $Memcached->init($config);
  263. $this->assertEquals(Memcached::SERIALIZER_MSGPACK, $Memcached->getMemcached()->getOption(Memcached::OPT_SERIALIZER));
  264. }
  265. /**
  266. * testJsonSerializerThrowException method
  267. *
  268. * @return void
  269. */
  270. public function testJsonSerializerThrowException()
  271. {
  272. $this->skipIf(
  273. Memcached::HAVE_JSON,
  274. 'Memcached extension is compiled with json support'
  275. );
  276. $Memcached = new TestMemcachedEngine();
  277. $config = [
  278. 'className' => 'Memcached',
  279. 'servers' => ['127.0.0.1:11211'],
  280. 'persistent' => false,
  281. 'serialize' => 'json'
  282. ];
  283. $this->setExpectedException(
  284. 'InvalidArgumentException',
  285. 'Memcached extension is not compiled with json support'
  286. );
  287. $Memcached->init($config);
  288. }
  289. /**
  290. * testMsgpackSerializerThrowException method
  291. *
  292. * @return void
  293. */
  294. public function testMsgpackSerializerThrowException()
  295. {
  296. $this->skipIf(
  297. defined('Memcached::HAVE_MSGPACK') && Memcached::HAVE_MSGPACK,
  298. 'Memcached extension is compiled with msgpack support'
  299. );
  300. $Memcached = new TestMemcachedEngine();
  301. $config = [
  302. 'engine' => 'Memcached',
  303. 'servers' => ['127.0.0.1:11211'],
  304. 'persistent' => false,
  305. 'serialize' => 'msgpack'
  306. ];
  307. $this->setExpectedException(
  308. 'InvalidArgumentException',
  309. 'msgpack is not a valid serializer engine for Memcached'
  310. );
  311. $Memcached->init($config);
  312. }
  313. /**
  314. * testIgbinarySerializerThrowException method
  315. *
  316. * @return void
  317. */
  318. public function testIgbinarySerializerThrowException()
  319. {
  320. $this->skipIf(
  321. Memcached::HAVE_IGBINARY,
  322. 'Memcached extension is compiled with igbinary support'
  323. );
  324. $Memcached = new TestMemcachedEngine();
  325. $config = [
  326. 'engine' => 'Memcached',
  327. 'servers' => ['127.0.0.1:11211'],
  328. 'persistent' => false,
  329. 'serialize' => 'igbinary'
  330. ];
  331. $this->setExpectedException(
  332. 'InvalidArgumentException',
  333. 'Memcached extension is not compiled with igbinary support'
  334. );
  335. $Memcached->init($config);
  336. }
  337. /**
  338. * test using authentication without memcached installed with SASL support
  339. * throw an exception
  340. *
  341. * @expectedException \InvalidArgumentException
  342. * @expectedExceptionMessage Memcached extension is not build with SASL support
  343. * @return void
  344. */
  345. public function testSaslAuthException()
  346. {
  347. $MemcachedEngine = new TestMemcachedEngine();
  348. $config = [
  349. 'engine' => 'Memcached',
  350. 'servers' => ['127.0.0.1:11211'],
  351. 'persistent' => false,
  352. 'username' => 'test',
  353. 'password' => 'password'
  354. ];
  355. $MemcachedEngine->init($config);
  356. }
  357. /**
  358. * testConfig method
  359. *
  360. * @return void
  361. */
  362. public function testMultipleServers()
  363. {
  364. $servers = ['127.0.0.1:11211', '127.0.0.1:11222'];
  365. $available = true;
  366. $Memcached = new \Memcached();
  367. foreach ($servers as $server) {
  368. list($host, $port) = explode(':', $server);
  369. //@codingStandardsIgnoreStart
  370. if (!$Memcached->addServer($host, $port)) {
  371. $available = false;
  372. }
  373. //@codingStandardsIgnoreEnd
  374. }
  375. $this->skipIf(!$available, 'Need memcached servers at ' . implode(', ', $servers) . ' to run this test.');
  376. $Memcached = new MemcachedEngine();
  377. $Memcached->init(['engine' => 'Memcached', 'servers' => $servers]);
  378. $config = $Memcached->config();
  379. $this->assertEquals($config['servers'], $servers);
  380. Cache::drop('dual_server');
  381. }
  382. /**
  383. * test connecting to an ipv6 server.
  384. *
  385. * @return void
  386. */
  387. public function testConnectIpv6()
  388. {
  389. $Memcached = new MemcachedEngine();
  390. $result = $Memcached->init([
  391. 'prefix' => 'cake_',
  392. 'duration' => 200,
  393. 'engine' => 'Memcached',
  394. 'servers' => [
  395. '[::1]:11211'
  396. ]
  397. ]);
  398. $this->assertTrue($result);
  399. }
  400. /**
  401. * test domain starts with u
  402. *
  403. * @return void
  404. */
  405. public function testParseServerStringWithU()
  406. {
  407. $Memcached = new TestMemcachedEngine();
  408. $result = $Memcached->parseServerString('udomain.net:13211');
  409. $this->assertEquals(['udomain.net', '13211'], $result);
  410. }
  411. /**
  412. * test non latin domains.
  413. *
  414. * @return void
  415. */
  416. public function testParseServerStringNonLatin()
  417. {
  418. $Memcached = new TestMemcachedEngine();
  419. $result = $Memcached->parseServerString('schülervz.net:13211');
  420. $this->assertEquals(['schülervz.net', '13211'], $result);
  421. $result = $Memcached->parseServerString('sülül:1111');
  422. $this->assertEquals(['sülül', '1111'], $result);
  423. }
  424. /**
  425. * test unix sockets.
  426. *
  427. * @return void
  428. */
  429. public function testParseServerStringUnix()
  430. {
  431. $Memcached = new TestMemcachedEngine();
  432. $result = $Memcached->parseServerString('unix:///path/to/memcachedd.sock');
  433. $this->assertEquals(['unix:///path/to/memcachedd.sock', 0], $result);
  434. }
  435. /**
  436. * testReadAndWriteCache method
  437. *
  438. * @return void
  439. */
  440. public function testReadAndWriteCache()
  441. {
  442. $this->_configCache(['duration' => 1]);
  443. $result = Cache::read('test', 'memcached');
  444. $expecting = '';
  445. $this->assertEquals($expecting, $result);
  446. $data = 'this is a test of the emergency broadcasting system';
  447. $result = Cache::write('test', $data, 'memcached');
  448. $this->assertTrue($result);
  449. $result = Cache::read('test', 'memcached');
  450. $expecting = $data;
  451. $this->assertEquals($expecting, $result);
  452. Cache::delete('test', 'memcached');
  453. }
  454. /**
  455. * testReadMany method
  456. *
  457. * @return void
  458. */
  459. public function testReadMany()
  460. {
  461. $this->_configCache(['duration' => 2]);
  462. $data = [
  463. 'App.falseTest' => false,
  464. 'App.trueTest' => true,
  465. 'App.nullTest' => null,
  466. 'App.zeroTest' => 0,
  467. 'App.zeroTest2' => '0'
  468. ];
  469. foreach ($data as $key => $value) {
  470. Cache::write($key, $value, 'memcached');
  471. }
  472. $read = Cache::readMany(array_merge(array_keys($data), ['App.doesNotExist']), 'memcached');
  473. $this->assertSame($read['App.falseTest'], false);
  474. $this->assertSame($read['App.trueTest'], true);
  475. $this->assertSame($read['App.nullTest'], null);
  476. $this->assertSame($read['App.zeroTest'], 0);
  477. $this->assertSame($read['App.zeroTest2'], '0');
  478. $this->assertSame($read['App.doesNotExist'], false);
  479. }
  480. /**
  481. * testWriteMany method
  482. *
  483. * @return void
  484. */
  485. public function testWriteMany()
  486. {
  487. $this->_configCache(['duration' => 2]);
  488. $data = [
  489. 'App.falseTest' => false,
  490. 'App.trueTest' => true,
  491. 'App.nullTest' => null,
  492. 'App.zeroTest' => 0,
  493. 'App.zeroTest2' => '0'
  494. ];
  495. Cache::writeMany($data, 'memcached');
  496. $this->assertSame(Cache::read('App.falseTest', 'memcached'), false);
  497. $this->assertSame(Cache::read('App.trueTest', 'memcached'), true);
  498. $this->assertSame(Cache::read('App.nullTest', 'memcached'), null);
  499. $this->assertSame(Cache::read('App.zeroTest', 'memcached'), 0);
  500. $this->assertSame(Cache::read('App.zeroTest2', 'memcached'), '0');
  501. }
  502. /**
  503. * testExpiry method
  504. *
  505. * @return void
  506. */
  507. public function testExpiry()
  508. {
  509. $this->_configCache(['duration' => 1]);
  510. $result = Cache::read('test', 'memcached');
  511. $this->assertFalse($result);
  512. $data = 'this is a test of the emergency broadcasting system';
  513. $result = Cache::write('other_test', $data, 'memcached');
  514. $this->assertTrue($result);
  515. sleep(2);
  516. $result = Cache::read('other_test', 'memcached');
  517. $this->assertFalse($result);
  518. $this->_configCache(['duration' => '+1 second']);
  519. $data = 'this is a test of the emergency broadcasting system';
  520. $result = Cache::write('other_test', $data, 'memcached');
  521. $this->assertTrue($result);
  522. sleep(3);
  523. $result = Cache::read('other_test', 'memcached');
  524. $this->assertFalse($result);
  525. $result = Cache::read('other_test', 'memcached');
  526. $this->assertFalse($result);
  527. $this->_configCache(['duration' => '+29 days']);
  528. $data = 'this is a test of the emergency broadcasting system';
  529. $result = Cache::write('long_expiry_test', $data, 'memcached');
  530. $this->assertTrue($result);
  531. sleep(2);
  532. $result = Cache::read('long_expiry_test', 'memcached');
  533. $expecting = $data;
  534. $this->assertEquals($expecting, $result);
  535. }
  536. /**
  537. * testDeleteCache method
  538. *
  539. * @return void
  540. */
  541. public function testDeleteCache()
  542. {
  543. $data = 'this is a test of the emergency broadcasting system';
  544. $result = Cache::write('delete_test', $data, 'memcached');
  545. $this->assertTrue($result);
  546. $result = Cache::delete('delete_test', 'memcached');
  547. $this->assertTrue($result);
  548. }
  549. /**
  550. * testDeleteMany method
  551. *
  552. * @return void
  553. */
  554. public function testDeleteMany()
  555. {
  556. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not implement deleteMulti');
  557. $this->_configCache();
  558. $data = [
  559. 'App.falseTest' => false,
  560. 'App.trueTest' => true,
  561. 'App.nullTest' => null,
  562. 'App.zeroTest' => 0,
  563. 'App.zeroTest2' => '0',
  564. ];
  565. foreach ($data as $key => $value) {
  566. Cache::write($key, $value, 'memcached');
  567. }
  568. Cache::write('App.keepTest', 'keepMe', 'memcached');
  569. Cache::deleteMany(array_merge(array_keys($data), ['App.doesNotExist']), 'memcached');
  570. $this->assertSame(Cache::read('App.falseTest', 'memcached'), false);
  571. $this->assertSame(Cache::read('App.trueTest', 'memcached'), false);
  572. $this->assertSame(Cache::read('App.nullTest', 'memcached'), false);
  573. $this->assertSame(Cache::read('App.zeroTest', 'memcached'), false);
  574. $this->assertSame(Cache::read('App.zeroTest2', 'memcached'), false);
  575. $this->assertSame(Cache::read('App.keepTest', 'memcached'), 'keepMe');
  576. }
  577. /**
  578. * testDecrement method
  579. *
  580. * @return void
  581. */
  582. public function testDecrement()
  583. {
  584. $result = Cache::write('test_decrement', 5, 'memcached');
  585. $this->assertTrue($result);
  586. $result = Cache::decrement('test_decrement', 1, 'memcached');
  587. $this->assertEquals(4, $result);
  588. $result = Cache::read('test_decrement', 'memcached');
  589. $this->assertEquals(4, $result);
  590. $result = Cache::decrement('test_decrement', 2, 'memcached');
  591. $this->assertEquals(2, $result);
  592. $result = Cache::read('test_decrement', 'memcached');
  593. $this->assertEquals(2, $result);
  594. Cache::delete('test_decrement', 'memcached');
  595. }
  596. /**
  597. * test decrementing compressed keys
  598. *
  599. * @return void
  600. */
  601. public function testDecrementCompressedKeys()
  602. {
  603. Cache::config('compressed_memcached', [
  604. 'engine' => 'Memcached',
  605. 'duration' => '+2 seconds',
  606. 'servers' => ['127.0.0.1:11211'],
  607. 'compress' => true
  608. ]);
  609. $result = Cache::write('test_decrement', 5, 'compressed_memcached');
  610. $this->assertTrue($result);
  611. $result = Cache::decrement('test_decrement', 1, 'compressed_memcached');
  612. $this->assertEquals(4, $result);
  613. $result = Cache::read('test_decrement', 'compressed_memcached');
  614. $this->assertEquals(4, $result);
  615. $result = Cache::decrement('test_decrement', 2, 'compressed_memcached');
  616. $this->assertEquals(2, $result);
  617. $result = Cache::read('test_decrement', 'compressed_memcached');
  618. $this->assertEquals(2, $result);
  619. Cache::delete('test_decrement', 'compressed_memcached');
  620. }
  621. /**
  622. * testIncrement method
  623. *
  624. * @return void
  625. */
  626. public function testIncrement()
  627. {
  628. $result = Cache::write('test_increment', 5, 'memcached');
  629. $this->assertTrue($result);
  630. $result = Cache::increment('test_increment', 1, 'memcached');
  631. $this->assertEquals(6, $result);
  632. $result = Cache::read('test_increment', 'memcached');
  633. $this->assertEquals(6, $result);
  634. $result = Cache::increment('test_increment', 2, 'memcached');
  635. $this->assertEquals(8, $result);
  636. $result = Cache::read('test_increment', 'memcached');
  637. $this->assertEquals(8, $result);
  638. Cache::delete('test_increment', 'memcached');
  639. }
  640. /**
  641. * test incrementing compressed keys
  642. *
  643. * @return void
  644. */
  645. public function testIncrementCompressedKeys()
  646. {
  647. Cache::config('compressed_memcached', [
  648. 'engine' => 'Memcached',
  649. 'duration' => '+2 seconds',
  650. 'servers' => ['127.0.0.1:11211'],
  651. 'compress' => true
  652. ]);
  653. $result = Cache::write('test_increment', 5, 'compressed_memcached');
  654. $this->assertTrue($result);
  655. $result = Cache::increment('test_increment', 1, 'compressed_memcached');
  656. $this->assertEquals(6, $result);
  657. $result = Cache::read('test_increment', 'compressed_memcached');
  658. $this->assertEquals(6, $result);
  659. $result = Cache::increment('test_increment', 2, 'compressed_memcached');
  660. $this->assertEquals(8, $result);
  661. $result = Cache::read('test_increment', 'compressed_memcached');
  662. $this->assertEquals(8, $result);
  663. Cache::delete('test_increment', 'compressed_memcached');
  664. }
  665. /**
  666. * test that configurations don't conflict, when a file engine is declared after a memcached one.
  667. *
  668. * @return void
  669. */
  670. public function testConfigurationConflict()
  671. {
  672. Cache::config('long_memcached', [
  673. 'engine' => 'Memcached',
  674. 'duration' => '+2 seconds',
  675. 'servers' => ['127.0.0.1:11211'],
  676. ]);
  677. Cache::config('short_memcached', [
  678. 'engine' => 'Memcached',
  679. 'duration' => '+1 seconds',
  680. 'servers' => ['127.0.0.1:11211'],
  681. ]);
  682. $this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcached'));
  683. $this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcached'));
  684. $this->assertEquals('yay', Cache::read('duration_test', 'long_memcached'), 'Value was not read %s');
  685. $this->assertEquals('boo', Cache::read('short_duration_test', 'short_memcached'), 'Value was not read %s');
  686. sleep(1);
  687. $this->assertEquals('yay', Cache::read('duration_test', 'long_memcached'), 'Value was not read %s');
  688. sleep(2);
  689. $this->assertFalse(Cache::read('short_duration_test', 'short_memcached'), 'Cache was not invalidated %s');
  690. $this->assertFalse(Cache::read('duration_test', 'long_memcached'), 'Value did not expire %s');
  691. Cache::delete('duration_test', 'long_memcached');
  692. Cache::delete('short_duration_test', 'short_memcached');
  693. }
  694. /**
  695. * test clearing memcached.
  696. *
  697. * @return void
  698. */
  699. public function testClear()
  700. {
  701. Cache::config('memcached2', [
  702. 'engine' => 'Memcached',
  703. 'prefix' => 'cake2_',
  704. 'duration' => 3600
  705. ]);
  706. Cache::write('some_value', 'cache1', 'memcached');
  707. $result = Cache::clear(true, 'memcached');
  708. $this->assertTrue($result);
  709. $this->assertEquals('cache1', Cache::read('some_value', 'memcached'));
  710. Cache::write('some_value', 'cache2', 'memcached2');
  711. $result = Cache::clear(false, 'memcached');
  712. $this->assertTrue($result);
  713. $this->assertFalse(Cache::read('some_value', 'memcached'));
  714. $this->assertEquals('cache2', Cache::read('some_value', 'memcached2'));
  715. Cache::clear(false, 'memcached2');
  716. }
  717. /**
  718. * test that a 0 duration can successfully write.
  719. *
  720. * @return void
  721. */
  722. public function testZeroDuration()
  723. {
  724. $this->_configCache(['duration' => 0]);
  725. $result = Cache::write('test_key', 'written!', 'memcached');
  726. $this->assertTrue($result);
  727. $result = Cache::read('test_key', 'memcached');
  728. $this->assertEquals('written!', $result);
  729. }
  730. /**
  731. * Tests that configuring groups for stored keys return the correct values when read/written
  732. * Shows that altering the group value is equivalent to deleting all keys under the same
  733. * group
  734. *
  735. * @return void
  736. */
  737. public function testGroupReadWrite()
  738. {
  739. Cache::config('memcached_groups', [
  740. 'engine' => 'Memcached',
  741. 'duration' => 3600,
  742. 'groups' => ['group_a', 'group_b'],
  743. 'prefix' => 'test_'
  744. ]);
  745. Cache::config('memcached_helper', [
  746. 'engine' => 'Memcached',
  747. 'duration' => 3600,
  748. 'prefix' => 'test_'
  749. ]);
  750. $this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
  751. $this->assertEquals('value', Cache::read('test_groups', 'memcached_groups'));
  752. Cache::increment('group_a', 1, 'memcached_helper');
  753. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  754. $this->assertTrue(Cache::write('test_groups', 'value2', 'memcached_groups'));
  755. $this->assertEquals('value2', Cache::read('test_groups', 'memcached_groups'));
  756. Cache::increment('group_b', 1, 'memcached_helper');
  757. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  758. $this->assertTrue(Cache::write('test_groups', 'value3', 'memcached_groups'));
  759. $this->assertEquals('value3', Cache::read('test_groups', 'memcached_groups'));
  760. }
  761. /**
  762. * Tests that deleteing from a groups-enabled config is possible
  763. *
  764. * @return void
  765. */
  766. public function testGroupDelete()
  767. {
  768. Cache::config('memcached_groups', [
  769. 'engine' => 'Memcached',
  770. 'duration' => 3600,
  771. 'groups' => ['group_a', 'group_b']
  772. ]);
  773. $this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
  774. $this->assertEquals('value', Cache::read('test_groups', 'memcached_groups'));
  775. $this->assertTrue(Cache::delete('test_groups', 'memcached_groups'));
  776. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  777. }
  778. /**
  779. * Test clearing a cache group
  780. *
  781. * @return void
  782. */
  783. public function testGroupClear()
  784. {
  785. Cache::config('memcached_groups', [
  786. 'engine' => 'Memcached',
  787. 'duration' => 3600,
  788. 'groups' => ['group_a', 'group_b']
  789. ]);
  790. $this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
  791. $this->assertTrue(Cache::clearGroup('group_a', 'memcached_groups'));
  792. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  793. $this->assertTrue(Cache::write('test_groups', 'value2', 'memcached_groups'));
  794. $this->assertTrue(Cache::clearGroup('group_b', 'memcached_groups'));
  795. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  796. }
  797. }