MemcachedEngineTest.php 29 KB

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