| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 2.5.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Cache\Engine;
- use Cake\Cache\Cache;
- use Cake\Cache\Engine\MemcachedEngine;
- use Cake\TestSuite\TestCase;
- use Memcached;
- /**
- * MemcachedEngineTest class
- */
- class MemcachedEngineTest extends TestCase
- {
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $this->skipIf(!class_exists('Memcached'), 'Memcached is not installed or configured properly.');
- // @codingStandardsIgnoreStart
- $socket = @fsockopen('127.0.0.1', 11211, $errno, $errstr, 1);
- // @codingStandardsIgnoreEnd
- $this->skipIf(!$socket, 'Memcached is not running.');
- fclose($socket);
- $this->_configCache();
- }
- /**
- * Helper method for testing.
- *
- * @param array $config
- * @return void
- */
- protected function _configCache($config = [])
- {
- $defaults = [
- 'className' => 'Memcached',
- 'prefix' => 'cake_',
- 'duration' => 3600,
- ];
- Cache::drop('memcached');
- Cache::setConfig('memcached', array_merge($defaults, $config));
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown()
- {
- parent::tearDown();
- Cache::drop('memcached');
- Cache::drop('memcached2');
- Cache::drop('memcached_groups');
- Cache::drop('memcached_helper');
- Cache::drop('compressed_memcached');
- Cache::drop('long_memcached');
- Cache::drop('short_memcached');
- }
- /**
- * testConfig method
- *
- * @return void
- */
- public function testConfig()
- {
- $config = Cache::engine('memcached')->getConfig();
- unset($config['path']);
- $expecting = [
- 'prefix' => 'cake_',
- 'duration' => 3600,
- 'probability' => 100,
- 'servers' => ['127.0.0.1'],
- 'persistent' => false,
- 'compress' => false,
- 'username' => null,
- 'password' => null,
- 'groups' => [],
- 'serialize' => 'php',
- 'options' => [],
- 'host' => null,
- 'port' => null,
- ];
- $this->assertEquals($expecting, $config);
- }
- /**
- * testCompressionSetting method
- *
- * @return void
- */
- public function testCompressionSetting()
- {
- $Memcached = new MemcachedEngine();
- $Memcached->init([
- 'engine' => 'Memcached',
- 'servers' => ['127.0.0.1:11211'],
- 'compress' => false,
- ]);
- $this->assertFalse($Memcached->getOption(\Memcached::OPT_COMPRESSION));
- $MemcachedCompressed = new MemcachedEngine();
- $MemcachedCompressed->init([
- 'engine' => 'Memcached',
- 'servers' => ['127.0.0.1:11211'],
- 'compress' => true,
- ]);
- $this->assertTrue($MemcachedCompressed->getOption(\Memcached::OPT_COMPRESSION));
- }
- /**
- * test setting options
- *
- * @return void
- */
- public function testOptionsSetting()
- {
- $memcached = new MemcachedEngine();
- $memcached->init([
- 'engine' => 'Memcached',
- 'servers' => ['127.0.0.1:11211'],
- 'options' => [
- Memcached::OPT_BINARY_PROTOCOL => true,
- ],
- ]);
- $this->assertEquals(1, $memcached->getOption(Memcached::OPT_BINARY_PROTOCOL));
- }
- /**
- * test accepts only valid serializer engine
- *
- * @return void
- */
- public function testInvalidSerializerSetting()
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('invalid_serializer is not a valid serializer engine for Memcached');
- $Memcached = new MemcachedEngine();
- $config = [
- 'className' => 'Memcached',
- 'servers' => ['127.0.0.1:11211'],
- 'persistent' => false,
- 'serialize' => 'invalid_serializer',
- ];
- $Memcached->init($config);
- }
- /**
- * testPhpSerializerSetting method
- *
- * @return void
- */
- public function testPhpSerializerSetting()
- {
- $Memcached = new MemcachedEngine();
- $config = [
- 'className' => 'Memcached',
- 'servers' => ['127.0.0.1:11211'],
- 'persistent' => false,
- 'serialize' => 'php',
- ];
- $Memcached->init($config);
- $this->assertEquals(Memcached::SERIALIZER_PHP, $Memcached->getOption(Memcached::OPT_SERIALIZER));
- }
- /**
- * testJsonSerializerSetting method
- *
- * @return void
- */
- public function testJsonSerializerSetting()
- {
- $this->skipIf(
- !Memcached::HAVE_JSON,
- 'Memcached extension is not compiled with json support'
- );
- $Memcached = new MemcachedEngine();
- $config = [
- 'engine' => 'Memcached',
- 'servers' => ['127.0.0.1:11211'],
- 'persistent' => false,
- 'serialize' => 'json',
- ];
- $Memcached->init($config);
- $this->assertEquals(Memcached::SERIALIZER_JSON, $Memcached->getOption(Memcached::OPT_SERIALIZER));
- }
- /**
- * testIgbinarySerializerSetting method
- *
- * @return void
- */
- public function testIgbinarySerializerSetting()
- {
- $this->skipIf(
- !Memcached::HAVE_IGBINARY,
- 'Memcached extension is not compiled with igbinary support'
- );
- $Memcached = new MemcachedEngine();
- $config = [
- 'engine' => 'Memcached',
- 'servers' => ['127.0.0.1:11211'],
- 'persistent' => false,
- 'serialize' => 'igbinary',
- ];
- $Memcached->init($config);
- $this->assertEquals(Memcached::SERIALIZER_IGBINARY, $Memcached->getOption(Memcached::OPT_SERIALIZER));
- }
- /**
- * testMsgpackSerializerSetting method
- *
- * @return void
- */
- public function testMsgpackSerializerSetting()
- {
- $this->skipIf(
- !defined('Memcached::HAVE_MSGPACK') || !Memcached::HAVE_MSGPACK,
- 'Memcached extension is not compiled with msgpack support'
- );
- $Memcached = new MemcachedEngine();
- $config = [
- 'engine' => 'Memcached',
- 'servers' => ['127.0.0.1:11211'],
- 'persistent' => false,
- 'serialize' => 'msgpack',
- ];
- $Memcached->init($config);
- $this->assertEquals(Memcached::SERIALIZER_MSGPACK, $Memcached->getOption(Memcached::OPT_SERIALIZER));
- }
- /**
- * testJsonSerializerThrowException method
- *
- * @return void
- */
- public function testJsonSerializerThrowException()
- {
- $this->skipIf(
- Memcached::HAVE_JSON,
- 'Memcached extension is compiled with json support'
- );
- $Memcached = new MemcachedEngine();
- $config = [
- 'className' => 'Memcached',
- 'servers' => ['127.0.0.1:11211'],
- 'persistent' => false,
- 'serialize' => 'json',
- ];
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Memcached extension is not compiled with json support');
- $Memcached->init($config);
- }
- /**
- * testMsgpackSerializerThrowException method
- *
- * @return void
- */
- public function testMsgpackSerializerThrowException()
- {
- $this->skipIf(
- defined('Memcached::HAVE_MSGPACK') && Memcached::HAVE_MSGPACK,
- 'Memcached extension is compiled with msgpack support'
- );
- $Memcached = new MemcachedEngine();
- $config = [
- 'engine' => 'Memcached',
- 'servers' => ['127.0.0.1:11211'],
- 'persistent' => false,
- 'serialize' => 'msgpack',
- ];
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('msgpack is not a valid serializer engine for Memcached');
- $Memcached->init($config);
- }
- /**
- * testIgbinarySerializerThrowException method
- *
- * @return void
- */
- public function testIgbinarySerializerThrowException()
- {
- $this->skipIf(
- Memcached::HAVE_IGBINARY,
- 'Memcached extension is compiled with igbinary support'
- );
- $Memcached = new MemcachedEngine();
- $config = [
- 'engine' => 'Memcached',
- 'servers' => ['127.0.0.1:11211'],
- 'persistent' => false,
- 'serialize' => 'igbinary',
- ];
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Memcached extension is not compiled with igbinary support');
- $Memcached->init($config);
- }
- /**
- * test using authentication without memcached installed with SASL support
- * throw an exception
- *
- * @return void
- */
- public function testSaslAuthException()
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Memcached extension is not build with SASL support');
- $this->skipIf(
- method_exists(Memcached::class, 'setSaslAuthData'),
- 'Cannot test exception when sasl has been compiled in.'
- );
- $MemcachedEngine = new MemcachedEngine();
- $config = [
- 'engine' => 'Memcached',
- 'servers' => ['127.0.0.1:11211'],
- 'persistent' => false,
- 'username' => 'test',
- 'password' => 'password',
- ];
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Memcached extension is not built with SASL support');
- $MemcachedEngine->init($config);
- }
- /**
- * testConfig method
- *
- * @return void
- */
- public function testMultipleServers()
- {
- $servers = ['127.0.0.1:11211', '127.0.0.1:11222'];
- $available = true;
- $Memcached = new \Memcached();
- foreach ($servers as $server) {
- list($host, $port) = explode(':', $server);
- //@codingStandardsIgnoreStart
- if (!$Memcached->addServer($host, $port)) {
- $available = false;
- }
- //@codingStandardsIgnoreEnd
- }
- $this->skipIf(!$available, 'Need memcached servers at ' . implode(', ', $servers) . ' to run this test.');
- $Memcached = new MemcachedEngine();
- $Memcached->init(['engine' => 'Memcached', 'servers' => $servers]);
- $config = $Memcached->getConfig();
- $this->assertEquals($config['servers'], $servers);
- Cache::drop('dual_server');
- }
- /**
- * test connecting to an ipv6 server.
- *
- * @return void
- */
- public function testConnectIpv6()
- {
- $Memcached = new MemcachedEngine();
- $result = $Memcached->init([
- 'prefix' => 'cake_',
- 'duration' => 200,
- 'engine' => 'Memcached',
- 'servers' => [
- '[::1]:11211',
- ],
- ]);
- $this->assertTrue($result);
- }
- /**
- * test domain starts with u
- *
- * @return void
- */
- public function testParseServerStringWithU()
- {
- $Memcached = new MemcachedEngine();
- $result = $Memcached->parseServerString('udomain.net:13211');
- $this->assertEquals(['udomain.net', '13211'], $result);
- }
- /**
- * test non latin domains.
- *
- * @return void
- */
- public function testParseServerStringNonLatin()
- {
- $Memcached = new MemcachedEngine();
- $result = $Memcached->parseServerString('schülervz.net:13211');
- $this->assertEquals(['schülervz.net', '13211'], $result);
- $result = $Memcached->parseServerString('sülül:1111');
- $this->assertEquals(['sülül', '1111'], $result);
- }
- /**
- * test unix sockets.
- *
- * @return void
- */
- public function testParseServerStringUnix()
- {
- $Memcached = new MemcachedEngine();
- $result = $Memcached->parseServerString('unix:///path/to/memcachedd.sock');
- $this->assertEquals(['/path/to/memcachedd.sock', 0], $result);
- }
- /**
- * testReadAndWriteCache method
- *
- * @return void
- */
- public function testReadAndWriteCache()
- {
- $this->_configCache(['duration' => 1]);
- $result = Cache::read('test', 'memcached');
- $expecting = '';
- $this->assertEquals($expecting, $result);
- $data = 'this is a test of the emergency broadcasting system';
- $result = Cache::write('test', $data, 'memcached');
- $this->assertTrue($result);
- $result = Cache::read('test', 'memcached');
- $expecting = $data;
- $this->assertEquals($expecting, $result);
- Cache::delete('test', 'memcached');
- }
- /**
- * testReadMany method
- *
- * @return void
- */
- public function testReadMany()
- {
- $this->_configCache(['duration' => 2]);
- $data = [
- 'App.falseTest' => false,
- 'App.trueTest' => true,
- 'App.nullTest' => null,
- 'App.zeroTest' => 0,
- 'App.zeroTest2' => '0',
- ];
- foreach ($data as $key => $value) {
- Cache::write($key, $value, 'memcached');
- }
- $read = Cache::readMany(array_merge(array_keys($data), ['App.doesNotExist']), 'memcached');
- $this->assertFalse($read['App.falseTest']);
- $this->assertTrue($read['App.trueTest']);
- $this->assertNull($read['App.nullTest']);
- $this->assertSame($read['App.zeroTest'], 0);
- $this->assertSame($read['App.zeroTest2'], '0');
- $this->assertFalse($read['App.doesNotExist']);
- }
- /**
- * testWriteMany method
- *
- * @return void
- */
- public function testWriteMany()
- {
- $this->_configCache(['duration' => 2]);
- $data = [
- 'App.falseTest' => false,
- 'App.trueTest' => true,
- 'App.nullTest' => null,
- 'App.zeroTest' => 0,
- 'App.zeroTest2' => '0',
- ];
- Cache::writeMany($data, 'memcached');
- $this->assertFalse(Cache::read('App.falseTest', 'memcached'));
- $this->assertTrue(Cache::read('App.trueTest', 'memcached'));
- $this->assertNull(Cache::read('App.nullTest', 'memcached'));
- $this->assertSame(Cache::read('App.zeroTest', 'memcached'), 0);
- $this->assertSame(Cache::read('App.zeroTest2', 'memcached'), '0');
- }
- /**
- * testExpiry method
- *
- * @return void
- */
- public function testExpiry()
- {
- $this->_configCache(['duration' => 1]);
- $result = Cache::read('test', 'memcached');
- $this->assertFalse($result);
- $data = 'this is a test of the emergency broadcasting system';
- $result = Cache::write('other_test', $data, 'memcached');
- $this->assertTrue($result);
- sleep(2);
- $result = Cache::read('other_test', 'memcached');
- $this->assertFalse($result);
- $this->_configCache(['duration' => '+1 second']);
- $data = 'this is a test of the emergency broadcasting system';
- $result = Cache::write('other_test', $data, 'memcached');
- $this->assertTrue($result);
- sleep(3);
- $result = Cache::read('other_test', 'memcached');
- $this->assertFalse($result);
- $result = Cache::read('other_test', 'memcached');
- $this->assertFalse($result);
- $this->_configCache(['duration' => '+29 days']);
- $data = 'this is a test of the emergency broadcasting system';
- $result = Cache::write('long_expiry_test', $data, 'memcached');
- $this->assertTrue($result);
- sleep(2);
- $result = Cache::read('long_expiry_test', 'memcached');
- $expecting = $data;
- $this->assertEquals($expecting, $result);
- }
- /**
- * testDeleteCache method
- *
- * @return void
- */
- public function testDeleteCache()
- {
- $data = 'this is a test of the emergency broadcasting system';
- $result = Cache::write('delete_test', $data, 'memcached');
- $this->assertTrue($result);
- $result = Cache::delete('delete_test', 'memcached');
- $this->assertTrue($result);
- }
- /**
- * testDeleteMany method
- *
- * @return void
- */
- public function testDeleteMany()
- {
- $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not implement deleteMulti');
- $this->_configCache();
- $data = [
- 'App.falseTest' => false,
- 'App.trueTest' => true,
- 'App.nullTest' => null,
- 'App.zeroTest' => 0,
- 'App.zeroTest2' => '0',
- ];
- foreach ($data as $key => $value) {
- Cache::write($key, $value, 'memcached');
- }
- Cache::write('App.keepTest', 'keepMe', 'memcached');
- Cache::deleteMany(array_merge(array_keys($data), ['App.doesNotExist']), 'memcached');
- $this->assertFalse(Cache::read('App.falseTest', 'memcached'));
- $this->assertFalse(Cache::read('App.trueTest', 'memcached'));
- $this->assertFalse(Cache::read('App.nullTest', 'memcached'));
- $this->assertFalse(Cache::read('App.zeroTest', 'memcached'));
- $this->assertFalse(Cache::read('App.zeroTest2', 'memcached'));
- $this->assertSame(Cache::read('App.keepTest', 'memcached'), 'keepMe');
- }
- /**
- * testDecrement method
- *
- * @return void
- */
- public function testDecrement()
- {
- $result = Cache::write('test_decrement', 5, 'memcached');
- $this->assertTrue($result);
- $result = Cache::decrement('test_decrement', 1, 'memcached');
- $this->assertEquals(4, $result);
- $result = Cache::read('test_decrement', 'memcached');
- $this->assertEquals(4, $result);
- $result = Cache::decrement('test_decrement', 2, 'memcached');
- $this->assertEquals(2, $result);
- $result = Cache::read('test_decrement', 'memcached');
- $this->assertEquals(2, $result);
- Cache::delete('test_decrement', 'memcached');
- }
- /**
- * test decrementing compressed keys
- *
- * @return void
- */
- public function testDecrementCompressedKeys()
- {
- Cache::setConfig('compressed_memcached', [
- 'engine' => 'Memcached',
- 'duration' => '+2 seconds',
- 'servers' => ['127.0.0.1:11211'],
- 'compress' => true,
- ]);
- $result = Cache::write('test_decrement', 5, 'compressed_memcached');
- $this->assertTrue($result);
- $result = Cache::decrement('test_decrement', 1, 'compressed_memcached');
- $this->assertEquals(4, $result);
- $result = Cache::read('test_decrement', 'compressed_memcached');
- $this->assertEquals(4, $result);
- $result = Cache::decrement('test_decrement', 2, 'compressed_memcached');
- $this->assertEquals(2, $result);
- $result = Cache::read('test_decrement', 'compressed_memcached');
- $this->assertEquals(2, $result);
- Cache::delete('test_decrement', 'compressed_memcached');
- }
- /**
- * testIncrement method
- *
- * @return void
- */
- public function testIncrement()
- {
- $result = Cache::write('test_increment', 5, 'memcached');
- $this->assertTrue($result);
- $result = Cache::increment('test_increment', 1, 'memcached');
- $this->assertEquals(6, $result);
- $result = Cache::read('test_increment', 'memcached');
- $this->assertEquals(6, $result);
- $result = Cache::increment('test_increment', 2, 'memcached');
- $this->assertEquals(8, $result);
- $result = Cache::read('test_increment', 'memcached');
- $this->assertEquals(8, $result);
- Cache::delete('test_increment', 'memcached');
- }
- /**
- * Test that increment and decrement set ttls.
- *
- * @return void
- */
- public function testIncrementDecrementExpiring()
- {
- $this->_configCache(['duration' => 1]);
- Cache::write('test_increment', 1, 'memcached');
- Cache::write('test_decrement', 1, 'memcached');
- $this->assertSame(2, Cache::increment('test_increment', 1, 'memcached'));
- $this->assertSame(0, Cache::decrement('test_decrement', 1, 'memcached'));
- sleep(1);
- $this->assertFalse(Cache::read('test_increment', 'memcached'));
- $this->assertFalse(Cache::read('test_decrement', 'memcached'));
- }
- /**
- * test incrementing compressed keys
- *
- * @return void
- */
- public function testIncrementCompressedKeys()
- {
- Cache::setConfig('compressed_memcached', [
- 'engine' => 'Memcached',
- 'duration' => '+2 seconds',
- 'servers' => ['127.0.0.1:11211'],
- 'compress' => true,
- ]);
- $result = Cache::write('test_increment', 5, 'compressed_memcached');
- $this->assertTrue($result);
- $result = Cache::increment('test_increment', 1, 'compressed_memcached');
- $this->assertEquals(6, $result);
- $result = Cache::read('test_increment', 'compressed_memcached');
- $this->assertEquals(6, $result);
- $result = Cache::increment('test_increment', 2, 'compressed_memcached');
- $this->assertEquals(8, $result);
- $result = Cache::read('test_increment', 'compressed_memcached');
- $this->assertEquals(8, $result);
- Cache::delete('test_increment', 'compressed_memcached');
- }
- /**
- * test that configurations don't conflict, when a file engine is declared after a memcached one.
- *
- * @return void
- */
- public function testConfigurationConflict()
- {
- Cache::setConfig('long_memcached', [
- 'engine' => 'Memcached',
- 'duration' => '+3 seconds',
- 'servers' => ['127.0.0.1:11211'],
- ]);
- Cache::setConfig('short_memcached', [
- 'engine' => 'Memcached',
- 'duration' => '+2 seconds',
- 'servers' => ['127.0.0.1:11211'],
- ]);
- $this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcached'));
- $this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcached'));
- $this->assertEquals('yay', Cache::read('duration_test', 'long_memcached'), 'Value was not read %s');
- $this->assertEquals('boo', Cache::read('short_duration_test', 'short_memcached'), 'Value was not read %s');
- usleep(500000);
- $this->assertEquals('yay', Cache::read('duration_test', 'long_memcached'), 'Value was not read %s');
- usleep(3000000);
- $this->assertFalse(Cache::read('short_duration_test', 'short_memcached'), 'Cache was not invalidated %s');
- $this->assertFalse(Cache::read('duration_test', 'long_memcached'), 'Value did not expire %s');
- Cache::delete('duration_test', 'long_memcached');
- Cache::delete('short_duration_test', 'short_memcached');
- }
- /**
- * test clearing memcached.
- *
- * @return void
- */
- public function testClear()
- {
- Cache::setConfig('memcached2', [
- 'engine' => 'Memcached',
- 'prefix' => 'cake2_',
- 'duration' => 3600,
- ]);
- Cache::write('some_value', 'cache1', 'memcached');
- $result = Cache::clear(true, 'memcached');
- $this->assertTrue($result);
- $this->assertEquals('cache1', Cache::read('some_value', 'memcached'));
- Cache::write('some_value', 'cache2', 'memcached2');
- $result = Cache::clear(false, 'memcached');
- $this->assertTrue($result);
- $this->assertFalse(Cache::read('some_value', 'memcached'));
- $this->assertEquals('cache2', Cache::read('some_value', 'memcached2'));
- Cache::clear(false, 'memcached2');
- }
- /**
- * test that a 0 duration can successfully write.
- *
- * @return void
- */
- public function testZeroDuration()
- {
- $this->_configCache(['duration' => 0]);
- $result = Cache::write('test_key', 'written!', 'memcached');
- $this->assertTrue($result);
- $result = Cache::read('test_key', 'memcached');
- $this->assertEquals('written!', $result);
- }
- /**
- * Tests that configuring groups for stored keys return the correct values when read/written
- * Shows that altering the group value is equivalent to deleting all keys under the same
- * group
- *
- * @return void
- */
- public function testGroupReadWrite()
- {
- Cache::setConfig('memcached_groups', [
- 'engine' => 'Memcached',
- 'duration' => 3600,
- 'groups' => ['group_a', 'group_b'],
- 'prefix' => 'test_',
- ]);
- Cache::setConfig('memcached_helper', [
- 'engine' => 'Memcached',
- 'duration' => 3600,
- 'prefix' => 'test_',
- ]);
- $this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
- $this->assertEquals('value', Cache::read('test_groups', 'memcached_groups'));
- Cache::increment('group_a', 1, 'memcached_helper');
- $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
- $this->assertTrue(Cache::write('test_groups', 'value2', 'memcached_groups'));
- $this->assertEquals('value2', Cache::read('test_groups', 'memcached_groups'));
- Cache::increment('group_b', 1, 'memcached_helper');
- $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
- $this->assertTrue(Cache::write('test_groups', 'value3', 'memcached_groups'));
- $this->assertEquals('value3', Cache::read('test_groups', 'memcached_groups'));
- }
- /**
- * Tests that deleting from a groups-enabled config is possible
- *
- * @return void
- */
- public function testGroupDelete()
- {
- Cache::setConfig('memcached_groups', [
- 'engine' => 'Memcached',
- 'duration' => 3600,
- 'groups' => ['group_a', 'group_b'],
- ]);
- $this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
- $this->assertEquals('value', Cache::read('test_groups', 'memcached_groups'));
- $this->assertTrue(Cache::delete('test_groups', 'memcached_groups'));
- $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
- }
- /**
- * Test clearing a cache group
- *
- * @return void
- */
- public function testGroupClear()
- {
- Cache::setConfig('memcached_groups', [
- 'engine' => 'Memcached',
- 'duration' => 3600,
- 'groups' => ['group_a', 'group_b'],
- ]);
- $this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
- $this->assertTrue(Cache::clearGroup('group_a', 'memcached_groups'));
- $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
- $this->assertTrue(Cache::write('test_groups', 'value2', 'memcached_groups'));
- $this->assertTrue(Cache::clearGroup('group_b', 'memcached_groups'));
- $this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
- }
- /**
- * Test add
- *
- * @return void
- */
- public function testAdd()
- {
- Cache::delete('test_add_key', 'memcached');
- $result = Cache::add('test_add_key', 'test data', 'memcached');
- $this->assertTrue($result);
- $expected = 'test data';
- $result = Cache::read('test_add_key', 'memcached');
- $this->assertEquals($expected, $result);
- $result = Cache::add('test_add_key', 'test data 2', 'memcached');
- $this->assertFalse($result);
- }
- }
|