MemcachedEngineTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. * testPhpSerializerSetting method
  330. *
  331. * @return void
  332. */
  333. public function testConfigDifferentPorts()
  334. {
  335. $Memcached1 = new MemcachedEngine();
  336. $config1 = [
  337. 'className' => 'Memcached',
  338. 'servers' => ['127.0.0.1:11211'],
  339. 'persistent' => true,
  340. ];
  341. $Memcached1->init($config1);
  342. $expectedServers = [
  343. [
  344. 'host' => '127.0.0.1',
  345. 'port' => 11211,
  346. 'type' => 'TCP',
  347. ],
  348. ];
  349. $this->assertEquals($expectedServers, $Memcached1->getServerList());
  350. $Memcached2 = new MemcachedEngine();
  351. $config2 = [
  352. 'className' => 'Memcached',
  353. 'servers' => ['127.0.0.1:11212'],
  354. 'persistent' => true,
  355. ];
  356. $Memcached2->init($config2);
  357. $expectedServers = [
  358. [
  359. 'host' => '127.0.0.1',
  360. 'port' => 11212,
  361. 'type' => 'TCP',
  362. ],
  363. ];
  364. $this->assertEquals($expectedServers, $Memcached2->getServerList());
  365. }
  366. /**
  367. * testConfig method
  368. *
  369. * @return void
  370. */
  371. public function testMultipleServers()
  372. {
  373. $servers = ['127.0.0.1:11211', '127.0.0.1:11222'];
  374. $available = true;
  375. $Memcached = new \Memcached();
  376. foreach ($servers as $server) {
  377. list($host, $port) = explode(':', $server);
  378. //@codingStandardsIgnoreStart
  379. if (!$Memcached->addServer($host, $port)) {
  380. $available = false;
  381. }
  382. //@codingStandardsIgnoreEnd
  383. }
  384. $this->skipIf(!$available, 'Need memcached servers at ' . implode(', ', $servers) . ' to run this test.');
  385. $Memcached = new MemcachedEngine();
  386. $Memcached->init(['engine' => 'Memcached', 'servers' => $servers]);
  387. $config = $Memcached->getConfig();
  388. $this->assertEquals($config['servers'], $servers);
  389. Cache::drop('dual_server');
  390. }
  391. /**
  392. * test connecting to an ipv6 server.
  393. *
  394. * @return void
  395. */
  396. public function testConnectIpv6()
  397. {
  398. $Memcached = new MemcachedEngine();
  399. $result = $Memcached->init([
  400. 'prefix' => 'cake_',
  401. 'duration' => 200,
  402. 'engine' => 'Memcached',
  403. 'servers' => [
  404. '[::1]:11211',
  405. ],
  406. ]);
  407. $this->assertTrue($result);
  408. }
  409. /**
  410. * test domain starts with u
  411. *
  412. * @return void
  413. */
  414. public function testParseServerStringWithU()
  415. {
  416. $Memcached = new MemcachedEngine();
  417. $result = $Memcached->parseServerString('udomain.net:13211');
  418. $this->assertEquals(['udomain.net', '13211'], $result);
  419. }
  420. /**
  421. * test non latin domains.
  422. *
  423. * @return void
  424. */
  425. public function testParseServerStringNonLatin()
  426. {
  427. $Memcached = new MemcachedEngine();
  428. $result = $Memcached->parseServerString('schülervz.net:13211');
  429. $this->assertEquals(['schülervz.net', '13211'], $result);
  430. $result = $Memcached->parseServerString('sülül:1111');
  431. $this->assertEquals(['sülül', '1111'], $result);
  432. }
  433. /**
  434. * test unix sockets.
  435. *
  436. * @return void
  437. */
  438. public function testParseServerStringUnix()
  439. {
  440. $Memcached = new MemcachedEngine();
  441. $result = $Memcached->parseServerString('unix:///path/to/memcachedd.sock');
  442. $this->assertEquals(['/path/to/memcachedd.sock', 0], $result);
  443. }
  444. /**
  445. * testReadAndWriteCache method
  446. *
  447. * @return void
  448. */
  449. public function testReadAndWriteCache()
  450. {
  451. $this->_configCache(['duration' => 1]);
  452. $result = Cache::read('test', 'memcached');
  453. $expecting = '';
  454. $this->assertEquals($expecting, $result);
  455. $data = 'this is a test of the emergency broadcasting system';
  456. $result = Cache::write('test', $data, 'memcached');
  457. $this->assertTrue($result);
  458. $result = Cache::read('test', 'memcached');
  459. $expecting = $data;
  460. $this->assertEquals($expecting, $result);
  461. Cache::delete('test', 'memcached');
  462. }
  463. /**
  464. * testReadMany method
  465. *
  466. * @return void
  467. */
  468. public function testReadMany()
  469. {
  470. $this->_configCache(['duration' => 2]);
  471. $data = [
  472. 'App.falseTest' => false,
  473. 'App.trueTest' => true,
  474. 'App.nullTest' => null,
  475. 'App.zeroTest' => 0,
  476. 'App.zeroTest2' => '0',
  477. ];
  478. foreach ($data as $key => $value) {
  479. Cache::write($key, $value, 'memcached');
  480. }
  481. $read = Cache::readMany(array_merge(array_keys($data), ['App.doesNotExist']), 'memcached');
  482. $this->assertFalse($read['App.falseTest']);
  483. $this->assertTrue($read['App.trueTest']);
  484. $this->assertNull($read['App.nullTest']);
  485. $this->assertSame($read['App.zeroTest'], 0);
  486. $this->assertSame($read['App.zeroTest2'], '0');
  487. $this->assertFalse($read['App.doesNotExist']);
  488. }
  489. /**
  490. * testWriteMany method
  491. *
  492. * @return void
  493. */
  494. public function testWriteMany()
  495. {
  496. $this->_configCache(['duration' => 2]);
  497. $data = [
  498. 'App.falseTest' => false,
  499. 'App.trueTest' => true,
  500. 'App.nullTest' => null,
  501. 'App.zeroTest' => 0,
  502. 'App.zeroTest2' => '0',
  503. ];
  504. Cache::writeMany($data, 'memcached');
  505. $this->assertFalse(Cache::read('App.falseTest', 'memcached'));
  506. $this->assertTrue(Cache::read('App.trueTest', 'memcached'));
  507. $this->assertNull(Cache::read('App.nullTest', 'memcached'));
  508. $this->assertSame(Cache::read('App.zeroTest', 'memcached'), 0);
  509. $this->assertSame(Cache::read('App.zeroTest2', 'memcached'), '0');
  510. }
  511. /**
  512. * testExpiry method
  513. *
  514. * @return void
  515. */
  516. public function testExpiry()
  517. {
  518. $this->_configCache(['duration' => 1]);
  519. $result = Cache::read('test', 'memcached');
  520. $this->assertFalse($result);
  521. $data = 'this is a test of the emergency broadcasting system';
  522. $result = Cache::write('other_test', $data, 'memcached');
  523. $this->assertTrue($result);
  524. sleep(2);
  525. $result = Cache::read('other_test', 'memcached');
  526. $this->assertFalse($result);
  527. $this->_configCache(['duration' => '+1 second']);
  528. $data = 'this is a test of the emergency broadcasting system';
  529. $result = Cache::write('other_test', $data, 'memcached');
  530. $this->assertTrue($result);
  531. sleep(3);
  532. $result = Cache::read('other_test', 'memcached');
  533. $this->assertFalse($result);
  534. $result = Cache::read('other_test', 'memcached');
  535. $this->assertFalse($result);
  536. $this->_configCache(['duration' => '+29 days']);
  537. $data = 'this is a test of the emergency broadcasting system';
  538. $result = Cache::write('long_expiry_test', $data, 'memcached');
  539. $this->assertTrue($result);
  540. sleep(2);
  541. $result = Cache::read('long_expiry_test', 'memcached');
  542. $expecting = $data;
  543. $this->assertEquals($expecting, $result);
  544. }
  545. /**
  546. * testDeleteCache method
  547. *
  548. * @return void
  549. */
  550. public function testDeleteCache()
  551. {
  552. $data = 'this is a test of the emergency broadcasting system';
  553. $result = Cache::write('delete_test', $data, 'memcached');
  554. $this->assertTrue($result);
  555. $result = Cache::delete('delete_test', 'memcached');
  556. $this->assertTrue($result);
  557. }
  558. /**
  559. * testDeleteMany method
  560. *
  561. * @return void
  562. */
  563. public function testDeleteMany()
  564. {
  565. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not implement deleteMulti');
  566. $this->_configCache();
  567. $data = [
  568. 'App.falseTest' => false,
  569. 'App.trueTest' => true,
  570. 'App.nullTest' => null,
  571. 'App.zeroTest' => 0,
  572. 'App.zeroTest2' => '0',
  573. ];
  574. foreach ($data as $key => $value) {
  575. Cache::write($key, $value, 'memcached');
  576. }
  577. Cache::write('App.keepTest', 'keepMe', 'memcached');
  578. Cache::deleteMany(array_merge(array_keys($data), ['App.doesNotExist']), 'memcached');
  579. $this->assertFalse(Cache::read('App.falseTest', 'memcached'));
  580. $this->assertFalse(Cache::read('App.trueTest', 'memcached'));
  581. $this->assertFalse(Cache::read('App.nullTest', 'memcached'));
  582. $this->assertFalse(Cache::read('App.zeroTest', 'memcached'));
  583. $this->assertFalse(Cache::read('App.zeroTest2', 'memcached'));
  584. $this->assertSame(Cache::read('App.keepTest', 'memcached'), 'keepMe');
  585. }
  586. /**
  587. * testDecrement method
  588. *
  589. * @return void
  590. */
  591. public function testDecrement()
  592. {
  593. $result = Cache::write('test_decrement', 5, 'memcached');
  594. $this->assertTrue($result);
  595. $result = Cache::decrement('test_decrement', 1, 'memcached');
  596. $this->assertEquals(4, $result);
  597. $result = Cache::read('test_decrement', 'memcached');
  598. $this->assertEquals(4, $result);
  599. $result = Cache::decrement('test_decrement', 2, 'memcached');
  600. $this->assertEquals(2, $result);
  601. $result = Cache::read('test_decrement', 'memcached');
  602. $this->assertEquals(2, $result);
  603. Cache::delete('test_decrement', 'memcached');
  604. }
  605. /**
  606. * test decrementing compressed keys
  607. *
  608. * @return void
  609. */
  610. public function testDecrementCompressedKeys()
  611. {
  612. Cache::setConfig('compressed_memcached', [
  613. 'engine' => 'Memcached',
  614. 'duration' => '+2 seconds',
  615. 'servers' => ['127.0.0.1:11211'],
  616. 'compress' => true,
  617. ]);
  618. $result = Cache::write('test_decrement', 5, 'compressed_memcached');
  619. $this->assertTrue($result);
  620. $result = Cache::decrement('test_decrement', 1, 'compressed_memcached');
  621. $this->assertEquals(4, $result);
  622. $result = Cache::read('test_decrement', 'compressed_memcached');
  623. $this->assertEquals(4, $result);
  624. $result = Cache::decrement('test_decrement', 2, 'compressed_memcached');
  625. $this->assertEquals(2, $result);
  626. $result = Cache::read('test_decrement', 'compressed_memcached');
  627. $this->assertEquals(2, $result);
  628. Cache::delete('test_decrement', 'compressed_memcached');
  629. }
  630. /**
  631. * testIncrement method
  632. *
  633. * @return void
  634. */
  635. public function testIncrement()
  636. {
  637. $result = Cache::write('test_increment', 5, 'memcached');
  638. $this->assertTrue($result);
  639. $result = Cache::increment('test_increment', 1, 'memcached');
  640. $this->assertEquals(6, $result);
  641. $result = Cache::read('test_increment', 'memcached');
  642. $this->assertEquals(6, $result);
  643. $result = Cache::increment('test_increment', 2, 'memcached');
  644. $this->assertEquals(8, $result);
  645. $result = Cache::read('test_increment', 'memcached');
  646. $this->assertEquals(8, $result);
  647. Cache::delete('test_increment', 'memcached');
  648. }
  649. /**
  650. * Test that increment and decrement set ttls.
  651. *
  652. * @return void
  653. */
  654. public function testIncrementDecrementExpiring()
  655. {
  656. $this->_configCache(['duration' => 1]);
  657. Cache::write('test_increment', 1, 'memcached');
  658. Cache::write('test_decrement', 1, 'memcached');
  659. $this->assertSame(2, Cache::increment('test_increment', 1, 'memcached'));
  660. $this->assertSame(0, Cache::decrement('test_decrement', 1, 'memcached'));
  661. sleep(1);
  662. $this->assertFalse(Cache::read('test_increment', 'memcached'));
  663. $this->assertFalse(Cache::read('test_decrement', 'memcached'));
  664. }
  665. /**
  666. * test incrementing compressed keys
  667. *
  668. * @return void
  669. */
  670. public function testIncrementCompressedKeys()
  671. {
  672. Cache::setConfig('compressed_memcached', [
  673. 'engine' => 'Memcached',
  674. 'duration' => '+2 seconds',
  675. 'servers' => ['127.0.0.1:11211'],
  676. 'compress' => true,
  677. ]);
  678. $result = Cache::write('test_increment', 5, 'compressed_memcached');
  679. $this->assertTrue($result);
  680. $result = Cache::increment('test_increment', 1, 'compressed_memcached');
  681. $this->assertEquals(6, $result);
  682. $result = Cache::read('test_increment', 'compressed_memcached');
  683. $this->assertEquals(6, $result);
  684. $result = Cache::increment('test_increment', 2, 'compressed_memcached');
  685. $this->assertEquals(8, $result);
  686. $result = Cache::read('test_increment', 'compressed_memcached');
  687. $this->assertEquals(8, $result);
  688. Cache::delete('test_increment', 'compressed_memcached');
  689. }
  690. /**
  691. * test that configurations don't conflict, when a file engine is declared after a memcached one.
  692. *
  693. * @return void
  694. */
  695. public function testConfigurationConflict()
  696. {
  697. Cache::setConfig('long_memcached', [
  698. 'engine' => 'Memcached',
  699. 'duration' => '+3 seconds',
  700. 'servers' => ['127.0.0.1:11211'],
  701. ]);
  702. Cache::setConfig('short_memcached', [
  703. 'engine' => 'Memcached',
  704. 'duration' => '+2 seconds',
  705. 'servers' => ['127.0.0.1:11211'],
  706. ]);
  707. $this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcached'));
  708. $this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcached'));
  709. $this->assertEquals('yay', Cache::read('duration_test', 'long_memcached'), 'Value was not read %s');
  710. $this->assertEquals('boo', Cache::read('short_duration_test', 'short_memcached'), 'Value was not read %s');
  711. usleep(500000);
  712. $this->assertEquals('yay', Cache::read('duration_test', 'long_memcached'), 'Value was not read %s');
  713. usleep(3000000);
  714. $this->assertFalse(Cache::read('short_duration_test', 'short_memcached'), 'Cache was not invalidated %s');
  715. $this->assertFalse(Cache::read('duration_test', 'long_memcached'), 'Value did not expire %s');
  716. Cache::delete('duration_test', 'long_memcached');
  717. Cache::delete('short_duration_test', 'short_memcached');
  718. }
  719. /**
  720. * test clearing memcached.
  721. *
  722. * @return void
  723. */
  724. public function testClear()
  725. {
  726. Cache::setConfig('memcached2', [
  727. 'engine' => 'Memcached',
  728. 'prefix' => 'cake2_',
  729. 'duration' => 3600,
  730. ]);
  731. Cache::write('some_value', 'cache1', 'memcached');
  732. $result = Cache::clear(true, 'memcached');
  733. $this->assertTrue($result);
  734. $this->assertEquals('cache1', Cache::read('some_value', 'memcached'));
  735. Cache::write('some_value', 'cache2', 'memcached2');
  736. $result = Cache::clear(false, 'memcached');
  737. $this->assertTrue($result);
  738. $this->assertFalse(Cache::read('some_value', 'memcached'));
  739. $this->assertEquals('cache2', Cache::read('some_value', 'memcached2'));
  740. Cache::clear(false, 'memcached2');
  741. }
  742. /**
  743. * test that a 0 duration can successfully write.
  744. *
  745. * @return void
  746. */
  747. public function testZeroDuration()
  748. {
  749. $this->_configCache(['duration' => 0]);
  750. $result = Cache::write('test_key', 'written!', 'memcached');
  751. $this->assertTrue($result);
  752. $result = Cache::read('test_key', 'memcached');
  753. $this->assertEquals('written!', $result);
  754. }
  755. /**
  756. * Tests that configuring groups for stored keys return the correct values when read/written
  757. * Shows that altering the group value is equivalent to deleting all keys under the same
  758. * group
  759. *
  760. * @return void
  761. */
  762. public function testGroupReadWrite()
  763. {
  764. Cache::setConfig('memcached_groups', [
  765. 'engine' => 'Memcached',
  766. 'duration' => 3600,
  767. 'groups' => ['group_a', 'group_b'],
  768. 'prefix' => 'test_',
  769. ]);
  770. Cache::setConfig('memcached_helper', [
  771. 'engine' => 'Memcached',
  772. 'duration' => 3600,
  773. 'prefix' => 'test_',
  774. ]);
  775. $this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
  776. $this->assertEquals('value', Cache::read('test_groups', 'memcached_groups'));
  777. Cache::increment('group_a', 1, 'memcached_helper');
  778. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  779. $this->assertTrue(Cache::write('test_groups', 'value2', 'memcached_groups'));
  780. $this->assertEquals('value2', Cache::read('test_groups', 'memcached_groups'));
  781. Cache::increment('group_b', 1, 'memcached_helper');
  782. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  783. $this->assertTrue(Cache::write('test_groups', 'value3', 'memcached_groups'));
  784. $this->assertEquals('value3', Cache::read('test_groups', 'memcached_groups'));
  785. }
  786. /**
  787. * Tests that deleting from a groups-enabled config is possible
  788. *
  789. * @return void
  790. */
  791. public function testGroupDelete()
  792. {
  793. Cache::setConfig('memcached_groups', [
  794. 'engine' => 'Memcached',
  795. 'duration' => 3600,
  796. 'groups' => ['group_a', 'group_b'],
  797. ]);
  798. $this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
  799. $this->assertEquals('value', Cache::read('test_groups', 'memcached_groups'));
  800. $this->assertTrue(Cache::delete('test_groups', 'memcached_groups'));
  801. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  802. }
  803. /**
  804. * Test clearing a cache group
  805. *
  806. * @return void
  807. */
  808. public function testGroupClear()
  809. {
  810. Cache::setConfig('memcached_groups', [
  811. 'engine' => 'Memcached',
  812. 'duration' => 3600,
  813. 'groups' => ['group_a', 'group_b'],
  814. ]);
  815. $this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
  816. $this->assertTrue(Cache::clearGroup('group_a', 'memcached_groups'));
  817. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  818. $this->assertTrue(Cache::write('test_groups', 'value2', 'memcached_groups'));
  819. $this->assertTrue(Cache::clearGroup('group_b', 'memcached_groups'));
  820. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  821. }
  822. /**
  823. * Test add
  824. *
  825. * @return void
  826. */
  827. public function testAdd()
  828. {
  829. Cache::delete('test_add_key', 'memcached');
  830. $result = Cache::add('test_add_key', 'test data', 'memcached');
  831. $this->assertTrue($result);
  832. $expected = 'test data';
  833. $result = Cache::read('test_add_key', 'memcached');
  834. $this->assertEquals($expected, $result);
  835. $result = Cache::add('test_add_key', 'test data 2', 'memcached');
  836. $this->assertFalse($result);
  837. }
  838. }