MemcachedEngineTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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. 'Cake\Error\Exception', '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. 'Cake\Error\Exception', '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. 'Cake\Error\Exception', '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. 'Cake\Error\Exception', '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. $Memcached = 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. $this->skipIf(
  323. method_exists($Memcached->getMemcached(), 'setSaslAuthData'),
  324. 'Memcached extension is installed with SASL support'
  325. );
  326. $this->setExpectedException(
  327. 'Cake\Error\Exception', 'Memcached extension is not build with SASL support'
  328. );
  329. $Memcached->init($config);
  330. }
  331. /**
  332. * testConfig method
  333. *
  334. * @return void
  335. */
  336. public function testMultipleServers() {
  337. $servers = array('127.0.0.1:11211', '127.0.0.1:11222');
  338. $available = true;
  339. $Memcached = new \Memcached();
  340. foreach ($servers as $server) {
  341. list($host, $port) = explode(':', $server);
  342. //@codingStandardsIgnoreStart
  343. if (!$Memcached->addServer($host, $port)) {
  344. $available = false;
  345. }
  346. //@codingStandardsIgnoreEnd
  347. }
  348. $this->skipIf(!$available, 'Need memcached servers at ' . implode(', ', $servers) . ' to run this test.');
  349. $Memcached = new MemcachedEngine();
  350. $Memcached->init(array('engine' => 'Memcached', 'servers' => $servers));
  351. $config = $Memcached->config();
  352. $this->assertEquals($config['servers'], $servers);
  353. Cache::drop('dual_server');
  354. }
  355. /**
  356. * test connecting to an ipv6 server.
  357. *
  358. * @return void
  359. */
  360. public function testConnectIpv6() {
  361. $Memcached = new MemcachedEngine();
  362. $result = $Memcached->init(array(
  363. 'prefix' => 'cake_',
  364. 'duration' => 200,
  365. 'engine' => 'Memcached',
  366. 'servers' => array(
  367. '[::1]:11211'
  368. )
  369. ));
  370. $this->assertTrue($result);
  371. }
  372. /**
  373. * test non latin domains.
  374. *
  375. * @return void
  376. */
  377. public function testParseServerStringNonLatin() {
  378. $Memcached = new TestMemcachedEngine();
  379. $result = $Memcached->parseServerString('schülervz.net:13211');
  380. $this->assertEquals(array('schülervz.net', '13211'), $result);
  381. $result = $Memcached->parseServerString('sülül:1111');
  382. $this->assertEquals(array('sülül', '1111'), $result);
  383. }
  384. /**
  385. * test unix sockets.
  386. *
  387. * @return void
  388. */
  389. public function testParseServerStringUnix() {
  390. $Memcached = new TestMemcachedEngine();
  391. $result = $Memcached->parseServerString('unix:///path/to/memcachedd.sock');
  392. $this->assertEquals(array('unix:///path/to/memcachedd.sock', 0), $result);
  393. }
  394. /**
  395. * testReadAndWriteCache method
  396. *
  397. * @return void
  398. */
  399. public function testReadAndWriteCache() {
  400. $this->_configCache(['duration' => 1]);
  401. $result = Cache::read('test', 'memcached');
  402. $expecting = '';
  403. $this->assertEquals($expecting, $result);
  404. $data = 'this is a test of the emergency broadcasting system';
  405. $result = Cache::write('test', $data, 'memcached');
  406. $this->assertTrue($result);
  407. $result = Cache::read('test', 'memcached');
  408. $expecting = $data;
  409. $this->assertEquals($expecting, $result);
  410. Cache::delete('test', 'memcached');
  411. }
  412. /**
  413. * testReadMany method
  414. *
  415. * @return void
  416. */
  417. public function testReadMany() {
  418. $this->_configCache(['duration' => 2]);
  419. $data = array(
  420. 'App.falseTest' => false,
  421. 'App.trueTest' => true,
  422. 'App.nullTest' => null,
  423. 'App.zeroTest' => 0,
  424. 'App.zeroTest2' => '0'
  425. );
  426. foreach ($data as $key => $value) {
  427. Cache::write($key, $value, 'memcached');
  428. }
  429. $read = Cache::readMany(array_merge(array_keys($data), ['App.doesNotExist']), 'memcached');
  430. $this->assertSame($read['App.falseTest'], false);
  431. $this->assertSame($read['App.trueTest'], true);
  432. $this->assertSame($read['App.nullTest'], null);
  433. $this->assertSame($read['App.zeroTest'], 0);
  434. $this->assertSame($read['App.zeroTest2'], '0');
  435. $this->assertSame($read['App.doesNotExist'], false);
  436. }
  437. /**
  438. * testWriteMany method
  439. *
  440. * @return void
  441. */
  442. public function testWriteMany() {
  443. $this->_configCache(['duration' => 2]);
  444. $data = array(
  445. 'App.falseTest' => false,
  446. 'App.trueTest' => true,
  447. 'App.nullTest' => null,
  448. 'App.zeroTest' => 0,
  449. 'App.zeroTest2' => '0'
  450. );
  451. Cache::writeMany($data, 'memcached');
  452. $this->assertSame(Cache::read('App.falseTest', 'memcached'), false);
  453. $this->assertSame(Cache::read('App.trueTest', 'memcached'), true);
  454. $this->assertSame(Cache::read('App.nullTest', 'memcached'), null);
  455. $this->assertSame(Cache::read('App.zeroTest', 'memcached'), 0);
  456. $this->assertSame(Cache::read('App.zeroTest2', 'memcached'), '0');
  457. }
  458. /**
  459. * testExpiry method
  460. *
  461. * @return void
  462. */
  463. public function testExpiry() {
  464. $this->_configCache(['duration' => 1]);
  465. $result = Cache::read('test', 'memcached');
  466. $this->assertFalse($result);
  467. $data = 'this is a test of the emergency broadcasting system';
  468. $result = Cache::write('other_test', $data, 'memcached');
  469. $this->assertTrue($result);
  470. sleep(2);
  471. $result = Cache::read('other_test', 'memcached');
  472. $this->assertFalse($result);
  473. $this->_configCache(['duration' => '+1 second']);
  474. $data = 'this is a test of the emergency broadcasting system';
  475. $result = Cache::write('other_test', $data, 'memcached');
  476. $this->assertTrue($result);
  477. sleep(3);
  478. $result = Cache::read('other_test', 'memcached');
  479. $this->assertFalse($result);
  480. $result = Cache::read('other_test', 'memcached');
  481. $this->assertFalse($result);
  482. $this->_configCache(['duration' => '+29 days']);
  483. $data = 'this is a test of the emergency broadcasting system';
  484. $result = Cache::write('long_expiry_test', $data, 'memcached');
  485. $this->assertTrue($result);
  486. sleep(2);
  487. $result = Cache::read('long_expiry_test', 'memcached');
  488. $expecting = $data;
  489. $this->assertEquals($expecting, $result);
  490. }
  491. /**
  492. * testDeleteCache method
  493. *
  494. * @return void
  495. */
  496. public function testDeleteCache() {
  497. $data = 'this is a test of the emergency broadcasting system';
  498. $result = Cache::write('delete_test', $data, 'memcached');
  499. $this->assertTrue($result);
  500. $result = Cache::delete('delete_test', 'memcached');
  501. $this->assertTrue($result);
  502. }
  503. /**
  504. * testDeleteMany method
  505. *
  506. * @return void
  507. */
  508. public function testDeleteMany() {
  509. $this->assertFalse(defined('HHVM_VERSION'), 'Crashes HHVM');
  510. $this->_configCache();
  511. $data = array(
  512. 'App.falseTest' => false,
  513. 'App.trueTest' => true,
  514. 'App.nullTest' => null,
  515. 'App.zeroTest' => 0,
  516. 'App.zeroTest2' => '0',
  517. );
  518. foreach ($data as $key => $value) {
  519. Cache::write($key, $value, 'memcached');
  520. }
  521. Cache::write('App.keepTest', 'keepMe', 'memcached');
  522. Cache::deleteMany(array_merge(array_keys($data), ['App.doesNotExist']), 'memcached');
  523. $this->assertSame(Cache::read('App.falseTest', 'memcached'), false);
  524. $this->assertSame(Cache::read('App.trueTest', 'memcached'), false);
  525. $this->assertSame(Cache::read('App.nullTest', 'memcached'), false);
  526. $this->assertSame(Cache::read('App.zeroTest', 'memcached'), false);
  527. $this->assertSame(Cache::read('App.zeroTest2', 'memcached'), false);
  528. $this->assertSame(Cache::read('App.keepTest', 'memcached'), 'keepMe');
  529. }
  530. /**
  531. * testDecrement method
  532. *
  533. * @return void
  534. */
  535. public function testDecrement() {
  536. $result = Cache::write('test_decrement', 5, 'memcached');
  537. $this->assertTrue($result);
  538. $result = Cache::decrement('test_decrement', 1, 'memcached');
  539. $this->assertEquals(4, $result);
  540. $result = Cache::read('test_decrement', 'memcached');
  541. $this->assertEquals(4, $result);
  542. $result = Cache::decrement('test_decrement', 2, 'memcached');
  543. $this->assertEquals(2, $result);
  544. $result = Cache::read('test_decrement', 'memcached');
  545. $this->assertEquals(2, $result);
  546. Cache::delete('test_decrement', 'memcached');
  547. }
  548. /**
  549. * test decrementing compressed keys
  550. *
  551. * @return void
  552. */
  553. public function testDecrementCompressedKeys() {
  554. Cache::config('compressed_memcached', array(
  555. 'engine' => 'Memcached',
  556. 'duration' => '+2 seconds',
  557. 'servers' => array('127.0.0.1:11211'),
  558. 'compress' => true
  559. ));
  560. $result = Cache::write('test_decrement', 5, 'compressed_memcached');
  561. $this->assertTrue($result);
  562. $result = Cache::decrement('test_decrement', 1, 'compressed_memcached');
  563. $this->assertEquals(4, $result);
  564. $result = Cache::read('test_decrement', 'compressed_memcached');
  565. $this->assertEquals(4, $result);
  566. $result = Cache::decrement('test_decrement', 2, 'compressed_memcached');
  567. $this->assertEquals(2, $result);
  568. $result = Cache::read('test_decrement', 'compressed_memcached');
  569. $this->assertEquals(2, $result);
  570. Cache::delete('test_decrement', 'compressed_memcached');
  571. }
  572. /**
  573. * testIncrement method
  574. *
  575. * @return void
  576. */
  577. public function testIncrement() {
  578. $result = Cache::write('test_increment', 5, 'memcached');
  579. $this->assertTrue($result);
  580. $result = Cache::increment('test_increment', 1, 'memcached');
  581. $this->assertEquals(6, $result);
  582. $result = Cache::read('test_increment', 'memcached');
  583. $this->assertEquals(6, $result);
  584. $result = Cache::increment('test_increment', 2, 'memcached');
  585. $this->assertEquals(8, $result);
  586. $result = Cache::read('test_increment', 'memcached');
  587. $this->assertEquals(8, $result);
  588. Cache::delete('test_increment', 'memcached');
  589. }
  590. /**
  591. * test incrementing compressed keys
  592. *
  593. * @return void
  594. */
  595. public function testIncrementCompressedKeys() {
  596. Cache::config('compressed_memcached', array(
  597. 'engine' => 'Memcached',
  598. 'duration' => '+2 seconds',
  599. 'servers' => array('127.0.0.1:11211'),
  600. 'compress' => true
  601. ));
  602. $result = Cache::write('test_increment', 5, 'compressed_memcached');
  603. $this->assertTrue($result);
  604. $result = Cache::increment('test_increment', 1, 'compressed_memcached');
  605. $this->assertEquals(6, $result);
  606. $result = Cache::read('test_increment', 'compressed_memcached');
  607. $this->assertEquals(6, $result);
  608. $result = Cache::increment('test_increment', 2, 'compressed_memcached');
  609. $this->assertEquals(8, $result);
  610. $result = Cache::read('test_increment', 'compressed_memcached');
  611. $this->assertEquals(8, $result);
  612. Cache::delete('test_increment', 'compressed_memcached');
  613. }
  614. /**
  615. * test that configurations don't conflict, when a file engine is declared after a memcached one.
  616. *
  617. * @return void
  618. */
  619. public function testConfigurationConflict() {
  620. Cache::config('long_memcached', array(
  621. 'engine' => 'Memcached',
  622. 'duration' => '+2 seconds',
  623. 'servers' => array('127.0.0.1:11211'),
  624. ));
  625. Cache::config('short_memcached', array(
  626. 'engine' => 'Memcached',
  627. 'duration' => '+1 seconds',
  628. 'servers' => array('127.0.0.1:11211'),
  629. ));
  630. $this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcached'));
  631. $this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcached'));
  632. $this->assertEquals('yay', Cache::read('duration_test', 'long_memcached'), 'Value was not read %s');
  633. $this->assertEquals('boo', Cache::read('short_duration_test', 'short_memcached'), 'Value was not read %s');
  634. sleep(1);
  635. $this->assertEquals('yay', Cache::read('duration_test', 'long_memcached'), 'Value was not read %s');
  636. sleep(2);
  637. $this->assertFalse(Cache::read('short_duration_test', 'short_memcached'), 'Cache was not invalidated %s');
  638. $this->assertFalse(Cache::read('duration_test', 'long_memcached'), 'Value did not expire %s');
  639. Cache::delete('duration_test', 'long_memcached');
  640. Cache::delete('short_duration_test', 'short_memcached');
  641. }
  642. /**
  643. * test clearing memcached.
  644. *
  645. * @return void
  646. */
  647. public function testClear() {
  648. $this->assertFalse(defined('HHVM_VERSION'), 'Crashes HHVM');
  649. Cache::config('memcached2', array(
  650. 'engine' => 'Memcached',
  651. 'prefix' => 'cake2_',
  652. 'duration' => 3600
  653. ));
  654. Cache::write('some_value', 'cache1', 'memcached');
  655. $result = Cache::clear(true, 'memcached');
  656. $this->assertTrue($result);
  657. $this->assertEquals('cache1', Cache::read('some_value', 'memcached'));
  658. Cache::write('some_value', 'cache2', 'memcached2');
  659. $result = Cache::clear(false, 'memcached');
  660. $this->assertTrue($result);
  661. $this->assertFalse(Cache::read('some_value', 'memcached'));
  662. $this->assertEquals('cache2', Cache::read('some_value', 'memcached2'));
  663. Cache::clear(false, 'memcached2');
  664. }
  665. /**
  666. * test that a 0 duration can successfully write.
  667. *
  668. * @return void
  669. */
  670. public function testZeroDuration() {
  671. $this->_configCache(['duration' => 0]);
  672. $result = Cache::write('test_key', 'written!', 'memcached');
  673. $this->assertTrue($result);
  674. $result = Cache::read('test_key', 'memcached');
  675. $this->assertEquals('written!', $result);
  676. }
  677. /**
  678. * test that durations greater than 30 days never expire
  679. *
  680. * @return void
  681. */
  682. public function testLongDurationEqualToZero() {
  683. $memcached = new TestMemcachedEngine();
  684. $memcached->init(['prefix' => 'Foo_', 'compress' => false, 'duration' => 50 * DAY]);
  685. $mock = $this->getMock('Memcached');
  686. $memcached->setMemcached($mock);
  687. $mock->expects($this->once())
  688. ->method('set')
  689. ->with('Foo_key', 'value', 0);
  690. $value = 'value';
  691. $memcached->write('key', $value);
  692. }
  693. /**
  694. * Tests that configuring groups for stored keys return the correct values when read/written
  695. * Shows that altering the group value is equivalent to deleting all keys under the same
  696. * group
  697. *
  698. * @return void
  699. */
  700. public function testGroupReadWrite() {
  701. Cache::config('memcached_groups', array(
  702. 'engine' => 'Memcached',
  703. 'duration' => 3600,
  704. 'groups' => array('group_a', 'group_b'),
  705. 'prefix' => 'test_'
  706. ));
  707. Cache::config('memcached_helper', array(
  708. 'engine' => 'Memcached',
  709. 'duration' => 3600,
  710. 'prefix' => 'test_'
  711. ));
  712. $this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
  713. $this->assertEquals('value', Cache::read('test_groups', 'memcached_groups'));
  714. Cache::increment('group_a', 1, 'memcached_helper');
  715. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  716. $this->assertTrue(Cache::write('test_groups', 'value2', 'memcached_groups'));
  717. $this->assertEquals('value2', Cache::read('test_groups', 'memcached_groups'));
  718. Cache::increment('group_b', 1, 'memcached_helper');
  719. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  720. $this->assertTrue(Cache::write('test_groups', 'value3', 'memcached_groups'));
  721. $this->assertEquals('value3', Cache::read('test_groups', 'memcached_groups'));
  722. }
  723. /**
  724. * Tests that deleteing from a groups-enabled config is possible
  725. *
  726. * @return void
  727. */
  728. public function testGroupDelete() {
  729. Cache::config('memcached_groups', array(
  730. 'engine' => 'Memcached',
  731. 'duration' => 3600,
  732. 'groups' => array('group_a', 'group_b')
  733. ));
  734. $this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
  735. $this->assertEquals('value', Cache::read('test_groups', 'memcached_groups'));
  736. $this->assertTrue(Cache::delete('test_groups', 'memcached_groups'));
  737. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  738. }
  739. /**
  740. * Test clearing a cache group
  741. *
  742. * @return void
  743. */
  744. public function testGroupClear() {
  745. Cache::config('memcached_groups', array(
  746. 'engine' => 'Memcached',
  747. 'duration' => 3600,
  748. 'groups' => array('group_a', 'group_b')
  749. ));
  750. $this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
  751. $this->assertTrue(Cache::clearGroup('group_a', 'memcached_groups'));
  752. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  753. $this->assertTrue(Cache::write('test_groups', 'value2', 'memcached_groups'));
  754. $this->assertTrue(Cache::clearGroup('group_b', 'memcached_groups'));
  755. $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
  756. }
  757. }