MemcachedEngineTest.php 28 KB

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