MemcachedEngineTest.php 23 KB

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