MemcachedEngineTest.php 29 KB

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