MemcachedEngineTest.php 30 KB

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