MemcachedEngineTest.php 29 KB

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