MemcachedEngineTest.php 27 KB

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