MemcachedEngineTest.php 23 KB

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