MemcachedEngineTest.php 28 KB

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