MemcachedEngineTest.php 23 KB

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