MemcachedEngineTest.php 23 KB

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