MemcachedEngineTest.php 27 KB

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