2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* MemcacheEngineTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 16:31:21 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Cache.Engine
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5434
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-03-06 04:56:31 +00:00
|
|
|
|
|
|
|
App::uses('Cache', 'Cache');
|
|
|
|
App::uses('MemcacheEngine', 'Cache/Engine');
|
2010-09-26 16:04:06 +00:00
|
|
|
|
2013-05-30 22:11:14 +00:00
|
|
|
/**
|
|
|
|
* Class TestMemcacheEngine
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Cache.Engine
|
|
|
|
*/
|
2010-09-26 16:04:06 +00:00
|
|
|
class TestMemcacheEngine extends MemcacheEngine {
|
2012-03-11 03:29:35 +00:00
|
|
|
|
2010-09-26 16:04:06 +00:00
|
|
|
/**
|
|
|
|
* public accessor to _parseServerString
|
|
|
|
*
|
2011-10-28 05:01:17 +00:00
|
|
|
* @param string $server
|
2010-09-26 16:04:06 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function parseServerString($server) {
|
2010-09-26 16:04:06 +00:00
|
|
|
return $this->_parseServerString($server);
|
|
|
|
}
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setMemcache($memcache) {
|
2011-01-19 01:20:49 +00:00
|
|
|
$this->_Memcache = $memcache;
|
2011-01-14 01:27:10 +00:00
|
|
|
}
|
2012-03-11 03:29:35 +00:00
|
|
|
|
2010-09-26 16:04:06 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* MemcacheEngineTest class
|
2008-07-26 20:00:20 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Cache.Engine
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-11-19 21:19:23 +00:00
|
|
|
class MemcacheEngineTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* setUp method
|
2008-07-26 20:00:20 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2012-04-16 02:20:34 +00:00
|
|
|
parent::setUp();
|
2011-05-31 00:49:46 +00:00
|
|
|
$this->skipIf(!class_exists('Memcache'), 'Memcache is not installed or configured properly.');
|
|
|
|
|
2009-03-21 23:55:39 +00:00
|
|
|
$this->_cacheDisable = Configure::read('Cache.disable');
|
|
|
|
Configure::write('Cache.disable', false);
|
2010-04-17 22:47:45 +00:00
|
|
|
Cache::config('memcache', array(
|
|
|
|
'engine' => 'Memcache',
|
|
|
|
'prefix' => 'cake_',
|
|
|
|
'duration' => 3600
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-19 21:19:23 +00:00
|
|
|
/**
|
|
|
|
* tearDown method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function tearDown() {
|
2012-04-16 02:20:34 +00:00
|
|
|
parent::tearDown();
|
2009-03-21 23:55:39 +00:00
|
|
|
Configure::write('Cache.disable', $this->_cacheDisable);
|
2009-11-19 00:47:40 +00:00
|
|
|
Cache::drop('memcache');
|
2012-03-26 00:38:37 +00:00
|
|
|
Cache::drop('memcache_groups');
|
|
|
|
Cache::drop('memcache_helper');
|
2008-11-19 21:19:23 +00:00
|
|
|
Cache::config('default');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testSettings method
|
2008-07-26 20:00:20 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSettings() {
|
2010-09-18 16:32:43 +00:00
|
|
|
$settings = Cache::settings('memcache');
|
2009-11-14 23:50:23 +00:00
|
|
|
unset($settings['serialize'], $settings['path']);
|
|
|
|
$expecting = array(
|
|
|
|
'prefix' => 'cake_',
|
2011-12-01 07:21:31 +00:00
|
|
|
'duration' => 3600,
|
2009-11-14 23:50:23 +00:00
|
|
|
'probability' => 100,
|
|
|
|
'servers' => array('127.0.0.1'),
|
2011-05-13 21:44:51 +00:00
|
|
|
'persistent' => true,
|
2009-11-14 23:50:23 +00:00
|
|
|
'compress' => false,
|
2011-05-14 02:10:58 +00:00
|
|
|
'engine' => 'Memcache',
|
2012-03-27 04:50:35 +00:00
|
|
|
'groups' => array()
|
2009-11-14 23:50:23 +00:00
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expecting, $settings);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-19 21:19:23 +00:00
|
|
|
/**
|
|
|
|
* testSettings method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMultipleServers() {
|
2008-11-19 21:19:23 +00:00
|
|
|
$servers = array('127.0.0.1:11211', '127.0.0.1:11222');
|
|
|
|
$available = true;
|
2010-11-13 04:05:44 +00:00
|
|
|
$Memcache = new Memcache();
|
2009-11-19 00:47:40 +00:00
|
|
|
|
2011-11-30 15:44:11 +00:00
|
|
|
foreach ($servers as $server) {
|
2008-11-19 21:19:23 +00:00
|
|
|
list($host, $port) = explode(':', $server);
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreStart
|
2010-01-21 18:57:42 +00:00
|
|
|
if (!@$Memcache->connect($host, $port)) {
|
2008-11-19 21:19:23 +00:00
|
|
|
$available = false;
|
|
|
|
}
|
2012-11-14 09:00:15 +00:00
|
|
|
//@codingStandardsIgnoreEnd
|
2008-11-19 21:19:23 +00:00
|
|
|
}
|
|
|
|
|
2011-05-31 00:49:46 +00:00
|
|
|
$this->skipIf(!$available, 'Need memcache servers at ' . implode(', ', $servers) . ' to run this test.');
|
|
|
|
|
2010-11-13 04:05:44 +00:00
|
|
|
$Memcache = new MemcacheEngine();
|
2009-11-19 00:47:40 +00:00
|
|
|
$Memcache->init(array('engine' => 'Memcache', 'servers' => $servers));
|
2008-11-19 21:19:23 +00:00
|
|
|
|
2009-11-19 00:47:40 +00:00
|
|
|
$settings = $Memcache->settings();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($settings['servers'], $servers);
|
2009-11-19 00:47:40 +00:00
|
|
|
Cache::drop('dual_server');
|
2008-11-19 21:19:23 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testConnect method
|
2008-07-26 20:00:20 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testConnect() {
|
2010-11-13 04:05:44 +00:00
|
|
|
$Memcache = new MemcacheEngine();
|
2009-11-19 00:47:40 +00:00
|
|
|
$Memcache->init(Cache::settings('memcache'));
|
|
|
|
$result = $Memcache->connect('127.0.0.1');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-26 05:11:19 +00:00
|
|
|
/**
|
|
|
|
* test connecting to an ipv6 server.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testConnectIpv6() {
|
2010-11-13 04:05:44 +00:00
|
|
|
$Memcache = new MemcacheEngine();
|
2010-09-26 05:11:19 +00:00
|
|
|
$result = $Memcache->init(array(
|
|
|
|
'prefix' => 'cake_',
|
|
|
|
'duration' => 200,
|
|
|
|
'engine' => 'Memcache',
|
|
|
|
'servers' => array(
|
|
|
|
'[::1]:11211'
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
2015-03-19 09:42:14 +00:00
|
|
|
/**
|
|
|
|
* test domain starts with u
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testParseServerStringWithU() {
|
|
|
|
$Memcached = new TestMemcachedEngine();
|
|
|
|
$result = $Memcached->parseServerString('udomain.net:13211');
|
|
|
|
$this->assertEquals(array('udomain.net', '13211'), $result);
|
|
|
|
}
|
|
|
|
|
2010-09-26 16:04:06 +00:00
|
|
|
/**
|
|
|
|
* test non latin domains.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testParseServerStringNonLatin() {
|
2010-11-13 04:05:44 +00:00
|
|
|
$Memcache = new TestMemcacheEngine();
|
2010-09-26 16:04:06 +00:00
|
|
|
$result = $Memcache->parseServerString('schülervz.net:13211');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('schülervz.net', '13211'), $result);
|
2010-09-26 16:04:06 +00:00
|
|
|
|
|
|
|
$result = $Memcache->parseServerString('sülül:1111');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('sülül', '1111'), $result);
|
2010-09-26 16:04:06 +00:00
|
|
|
}
|
|
|
|
|
2011-09-07 11:18:47 +00:00
|
|
|
/**
|
|
|
|
* test unix sockets.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-17 07:13:12 +00:00
|
|
|
public function testParseServerStringUnix() {
|
2012-02-23 16:27:40 +00:00
|
|
|
$Memcache = new TestMemcacheEngine();
|
2011-12-16 06:52:07 +00:00
|
|
|
$result = $Memcache->parseServerString('unix:///path/to/memcached.sock');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('unix:///path/to/memcached.sock', 0), $result);
|
2011-12-16 06:52:07 +00:00
|
|
|
}
|
2011-09-07 11:18:47 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testReadAndWriteCache method
|
2008-07-26 20:00:20 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadAndWriteCache() {
|
2010-09-18 16:32:43 +00:00
|
|
|
Cache::set(array('duration' => 1), null, 'memcache');
|
2009-02-06 22:56:06 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('test', 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$expecting = '';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$data = 'this is a test of the emergency broadcasting system';
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::write('test', $data, 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('test', 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$expecting = $data;
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2009-02-06 22:56:06 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
Cache::delete('test', 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testExpiry method
|
2008-07-26 20:00:20 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testExpiry() {
|
2010-09-18 16:32:43 +00:00
|
|
|
Cache::set(array('duration' => 1), 'memcache');
|
2009-02-06 22:56:06 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('test', 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = 'this is a test of the emergency broadcasting system';
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::write('other_test', $data, 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
sleep(2);
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('other_test', 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($result);
|
|
|
|
|
2012-03-11 03:29:35 +00:00
|
|
|
Cache::set(array('duration' => "+1 second"), 'memcache');
|
2008-07-26 20:00:20 +00:00
|
|
|
|
|
|
|
$data = 'this is a test of the emergency broadcasting system';
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::write('other_test', $data, 'memcache');
|
2008-07-26 20:00:20 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2012-10-26 01:56:00 +00:00
|
|
|
sleep(3);
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('other_test', 'memcache');
|
2009-02-06 22:56:06 +00:00
|
|
|
$this->assertFalse($result);
|
2008-07-26 20:00:20 +00:00
|
|
|
|
2009-11-19 00:47:40 +00:00
|
|
|
Cache::config('memcache', array('duration' => '+1 second'));
|
2008-07-26 20:00:20 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('other_test', 'memcache');
|
2008-07-26 20:00:20 +00:00
|
|
|
$this->assertFalse($result);
|
|
|
|
|
2010-10-24 04:05:27 +00:00
|
|
|
Cache::config('memcache', array('duration' => '+29 days'));
|
2009-02-11 23:00:19 +00:00
|
|
|
$data = 'this is a test of the emergency broadcasting system';
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::write('long_expiry_test', $data, 'memcache');
|
2009-02-11 23:00:19 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
sleep(2);
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('long_expiry_test', 'memcache');
|
2009-02-11 23:00:19 +00:00
|
|
|
$expecting = $data;
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2009-02-11 23:00:19 +00:00
|
|
|
|
2009-11-19 00:47:40 +00:00
|
|
|
Cache::config('memcache', array('duration' => 3600));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testDeleteCache method
|
2008-07-26 20:00:20 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDeleteCache() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$data = 'this is a test of the emergency broadcasting system';
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::write('delete_test', $data, 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::delete('delete_test', 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
2010-01-21 16:41:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* testDecrement method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDecrement() {
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::write('test_decrement', 5, 'memcache');
|
2010-01-21 16:41:44 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::decrement('test_decrement', 1, 'memcache');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(4, $result);
|
2010-01-21 16:41:44 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('test_decrement', 'memcache');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(4, $result);
|
2010-01-21 16:41:44 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::decrement('test_decrement', 2, 'memcache');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(2, $result);
|
2010-01-21 16:41:44 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('test_decrement', 'memcache');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(2, $result);
|
2010-01-21 16:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testIncrement method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testIncrement() {
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::write('test_increment', 5, 'memcache');
|
2010-01-21 16:41:44 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::increment('test_increment', 1, 'memcache');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(6, $result);
|
2010-01-21 16:41:44 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('test_increment', 'memcache');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(6, $result);
|
2010-01-21 16:41:44 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::increment('test_increment', 2, 'memcache');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(8, $result);
|
2010-01-21 16:41:44 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('test_increment', 'memcache');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(8, $result);
|
2010-01-21 16:41:44 +00:00
|
|
|
}
|
2010-04-17 22:47:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test that configurations don't conflict, when a file engine is declared after a memcache one.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testConfigurationConflict() {
|
2010-04-17 22:47:45 +00:00
|
|
|
Cache::config('long_memcache', array(
|
2011-12-19 13:18:28 +00:00
|
|
|
'engine' => 'Memcache',
|
|
|
|
'duration' => '+2 seconds',
|
|
|
|
'servers' => array('127.0.0.1:11211'),
|
2010-04-17 22:47:45 +00:00
|
|
|
));
|
|
|
|
Cache::config('short_memcache', array(
|
2011-12-19 13:18:28 +00:00
|
|
|
'engine' => 'Memcache',
|
|
|
|
'duration' => '+1 seconds',
|
|
|
|
'servers' => array('127.0.0.1:11211'),
|
2010-04-17 22:47:45 +00:00
|
|
|
));
|
|
|
|
Cache::config('some_file', array('engine' => 'File'));
|
|
|
|
|
|
|
|
$this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcache'));
|
|
|
|
$this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcache'));
|
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('yay', Cache::read('duration_test', 'long_memcache'), 'Value was not read %s');
|
|
|
|
$this->assertEquals('boo', Cache::read('short_duration_test', 'short_memcache'), 'Value was not read %s');
|
2010-04-17 22:47:45 +00:00
|
|
|
|
|
|
|
sleep(1);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('yay', Cache::read('duration_test', 'long_memcache'), 'Value was not read %s');
|
2010-04-17 22:47:45 +00:00
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
$this->assertFalse(Cache::read('short_duration_test', 'short_memcache'), 'Cache was not invalidated %s');
|
|
|
|
$this->assertFalse(Cache::read('duration_test', 'long_memcache'), 'Value did not expire %s');
|
|
|
|
|
|
|
|
Cache::delete('duration_test', 'long_memcache');
|
|
|
|
Cache::delete('short_duration_test', 'short_memcache');
|
|
|
|
}
|
|
|
|
|
2010-09-18 17:15:37 +00:00
|
|
|
/**
|
|
|
|
* test clearing memcache.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testClear() {
|
2011-08-20 02:12:37 +00:00
|
|
|
Cache::config('memcache2', array(
|
|
|
|
'engine' => 'Memcache',
|
|
|
|
'prefix' => 'cake2_',
|
|
|
|
'duration' => 3600
|
|
|
|
));
|
2010-09-18 17:15:37 +00:00
|
|
|
|
2011-08-20 02:12:37 +00:00
|
|
|
Cache::write('some_value', 'cache1', 'memcache');
|
|
|
|
$result = Cache::clear(true, 'memcache');
|
|
|
|
$this->assertTrue($result);
|
2012-12-27 12:55:25 +00:00
|
|
|
$this->assertEquals('cache1', Cache::read('some_value', 'memcache'));
|
2011-08-20 02:12:37 +00:00
|
|
|
|
|
|
|
Cache::write('some_value', 'cache2', 'memcache2');
|
2010-09-18 17:15:37 +00:00
|
|
|
$result = Cache::clear(false, 'memcache');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertFalse(Cache::read('some_value', 'memcache'));
|
2011-08-20 02:12:37 +00:00
|
|
|
$this->assertEquals('cache2', Cache::read('some_value', 'memcache2'));
|
|
|
|
|
|
|
|
Cache::clear(false, 'memcache2');
|
2010-09-18 17:15:37 +00:00
|
|
|
}
|
2011-12-06 20:52:48 +00:00
|
|
|
|
2010-09-19 16:20:07 +00:00
|
|
|
/**
|
2012-02-23 23:29:53 +00:00
|
|
|
* test that a 0 duration can successfully write.
|
2010-09-19 16:20:07 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testZeroDuration() {
|
2010-09-19 16:20:07 +00:00
|
|
|
Cache::config('memcache', array('duration' => 0));
|
|
|
|
$result = Cache::write('test_key', 'written!', 'memcache');
|
|
|
|
|
2012-03-25 22:40:25 +00:00
|
|
|
$this->assertTrue($result);
|
2010-09-19 16:20:07 +00:00
|
|
|
$result = Cache::read('test_key', 'memcache');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('written!', $result);
|
2011-01-14 01:27:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that durations greater than 30 days never expire
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testLongDurationEqualToZero() {
|
2012-02-23 16:27:40 +00:00
|
|
|
$memcache = new TestMemcacheEngine();
|
2011-01-14 01:27:10 +00:00
|
|
|
$memcache->settings['compress'] = false;
|
|
|
|
|
2011-01-19 01:20:49 +00:00
|
|
|
$mock = $this->getMock('Memcache');
|
2011-01-14 01:27:10 +00:00
|
|
|
$memcache->setMemcache($mock);
|
2011-01-19 01:20:49 +00:00
|
|
|
$mock->expects($this->once())
|
|
|
|
->method('set')
|
|
|
|
->with('key', 'value', false, 0);
|
2011-01-14 01:27:10 +00:00
|
|
|
|
|
|
|
$value = 'value';
|
|
|
|
$memcache->write('key', $value, 50 * DAY);
|
2010-09-19 16:20:07 +00:00
|
|
|
}
|
|
|
|
|
2012-03-26 00:38:37 +00:00
|
|
|
/**
|
|
|
|
* 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::config('memcache_groups', array(
|
|
|
|
'engine' => 'Memcache',
|
|
|
|
'duration' => 3600,
|
2012-03-27 04:04:03 +00:00
|
|
|
'groups' => array('group_a', 'group_b'),
|
|
|
|
'prefix' => 'test_'
|
2012-03-26 00:38:37 +00:00
|
|
|
));
|
|
|
|
Cache::config('memcache_helper', array(
|
|
|
|
'engine' => 'Memcache',
|
|
|
|
'duration' => 3600,
|
2012-03-27 04:04:03 +00:00
|
|
|
'prefix' => 'test_'
|
2012-03-26 00:38:37 +00:00
|
|
|
));
|
|
|
|
$this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
|
|
|
|
$this->assertEquals('value', Cache::read('test_groups', 'memcache_groups'));
|
|
|
|
|
|
|
|
Cache::increment('group_a', 1, 'memcache_helper');
|
|
|
|
$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
|
|
|
|
$this->assertTrue(Cache::write('test_groups', 'value2', 'memcache_groups'));
|
|
|
|
$this->assertEquals('value2', Cache::read('test_groups', 'memcache_groups'));
|
|
|
|
|
|
|
|
Cache::increment('group_b', 1, 'memcache_helper');
|
|
|
|
$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
|
|
|
|
$this->assertTrue(Cache::write('test_groups', 'value3', 'memcache_groups'));
|
|
|
|
$this->assertEquals('value3', Cache::read('test_groups', 'memcache_groups'));
|
|
|
|
}
|
2012-03-26 00:40:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that deleteing from a groups-enabled config is possible
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGroupDelete() {
|
|
|
|
Cache::config('memcache_groups', array(
|
|
|
|
'engine' => 'Memcache',
|
|
|
|
'duration' => 3600,
|
|
|
|
'groups' => array('group_a', 'group_b')
|
|
|
|
));
|
|
|
|
$this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
|
|
|
|
$this->assertEquals('value', Cache::read('test_groups', 'memcache_groups'));
|
|
|
|
$this->assertTrue(Cache::delete('test_groups', 'memcache_groups'));
|
|
|
|
|
|
|
|
$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
|
|
|
|
}
|
2012-03-26 01:53:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test clearing a cache group
|
|
|
|
*
|
|
|
|
* @return void
|
2013-01-11 14:06:54 +00:00
|
|
|
*/
|
2012-03-26 01:53:27 +00:00
|
|
|
public function testGroupClear() {
|
|
|
|
Cache::config('memcache_groups', array(
|
|
|
|
'engine' => 'Memcache',
|
|
|
|
'duration' => 3600,
|
|
|
|
'groups' => array('group_a', 'group_b')
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
|
|
|
|
$this->assertTrue(Cache::clearGroup('group_a', 'memcache_groups'));
|
|
|
|
$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
|
|
|
|
|
|
|
|
$this->assertTrue(Cache::write('test_groups', 'value2', 'memcache_groups'));
|
|
|
|
$this->assertTrue(Cache::clearGroup('group_b', 'memcache_groups'));
|
|
|
|
$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
|
|
|
|
}
|
2015-07-27 21:42:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that failed add write return false.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAdd() {
|
|
|
|
Cache::delete('test_add_key', 'memcache');
|
|
|
|
|
|
|
|
$result = Cache::add('test_add_key', 'test data', 'memcache');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$expected = 'test data';
|
|
|
|
$result = Cache::read('test_add_key', 'memcache');
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$result = Cache::add('test_add_key', 'test data 2', 'memcache');
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|