2012-03-29 04:00:33 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* RedisEngineTest file
|
|
|
|
*
|
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-03-29 04:00:33 +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
|
2012-03-29 04:00:33 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-03-29 04:00:33 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
|
|
|
* @package Cake.Test.Case.Cache.Engine
|
|
|
|
* @since CakePHP(tm) v 2.2
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2012-03-29 04:00:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
App::uses('Cache', 'Cache');
|
|
|
|
App::uses('RedisEngine', 'Cache/Engine');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RedisEngineTest class
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Cache.Engine
|
|
|
|
*/
|
2013-06-02 22:12:51 +00:00
|
|
|
class RedisEngineTest extends CakeTestCase {
|
2012-03-29 04:00:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* setUp method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
2013-07-28 23:22:52 +00:00
|
|
|
parent::setUp();
|
2012-03-29 04:00:33 +00:00
|
|
|
$this->skipIf(!class_exists('Redis'), 'Redis is not installed or configured properly.');
|
|
|
|
|
|
|
|
$this->_cacheDisable = Configure::read('Cache.disable');
|
|
|
|
Configure::write('Cache.disable', false);
|
2016-02-19 02:14:55 +00:00
|
|
|
|
|
|
|
// @codingStandardsIgnoreStart
|
|
|
|
$socket = @fsockopen('127.0.0.1', 6379, $errno, $errstr, 1);
|
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
$this->skipIf(!$socket, 'Redis is not running.');
|
|
|
|
fclose($socket);
|
|
|
|
|
2012-03-29 04:00:33 +00:00
|
|
|
Cache::config('redis', array(
|
|
|
|
'engine' => 'Redis',
|
|
|
|
'prefix' => 'cake_',
|
|
|
|
'duration' => 3600
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tearDown method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function tearDown() {
|
2013-07-28 23:22:52 +00:00
|
|
|
parent::tearDown();
|
2012-03-29 04:00:33 +00:00
|
|
|
Configure::write('Cache.disable', $this->_cacheDisable);
|
|
|
|
Cache::drop('');
|
|
|
|
Cache::drop('redis_groups');
|
|
|
|
Cache::drop('redis_helper');
|
|
|
|
Cache::config('default');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSettings method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSettings() {
|
|
|
|
$settings = Cache::settings('redis');
|
|
|
|
$expecting = array(
|
|
|
|
'prefix' => 'cake_',
|
|
|
|
'duration' => 3600,
|
|
|
|
'probability' => 100,
|
|
|
|
'groups' => array(),
|
|
|
|
'engine' => 'Redis',
|
|
|
|
'server' => '127.0.0.1',
|
2012-05-29 22:21:18 +00:00
|
|
|
'port' => 6379,
|
|
|
|
'timeout' => 0,
|
2012-11-02 01:12:48 +00:00
|
|
|
'persistent' => true,
|
|
|
|
'password' => false,
|
2013-10-31 13:40:45 +00:00
|
|
|
'database' => 0,
|
2014-10-15 02:47:34 +00:00
|
|
|
'unix_socket' => false,
|
2012-03-29 04:00:33 +00:00
|
|
|
);
|
|
|
|
$this->assertEquals($expecting, $settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testConnect method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testConnect() {
|
|
|
|
$Redis = new RedisEngine();
|
|
|
|
$this->assertTrue($Redis->init(Cache::settings('redis')));
|
|
|
|
}
|
|
|
|
|
2013-10-31 13:40:45 +00:00
|
|
|
/**
|
|
|
|
* testMultiDatabaseOperations method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testMultiDatabaseOperations() {
|
|
|
|
Cache::config('redisdb0', array(
|
|
|
|
'engine' => 'Redis',
|
|
|
|
'prefix' => 'cake2_',
|
|
|
|
'duration' => 3600,
|
|
|
|
'persistent' => false,
|
|
|
|
));
|
|
|
|
|
|
|
|
Cache::config('redisdb1', array(
|
|
|
|
'engine' => 'Redis',
|
|
|
|
'database' => 1,
|
|
|
|
'prefix' => 'cake2_',
|
|
|
|
'duration' => 3600,
|
|
|
|
'persistent' => false,
|
|
|
|
));
|
|
|
|
|
|
|
|
$result = Cache::write('save_in_0', true, 'redisdb0');
|
|
|
|
$exist = Cache::read('save_in_0', 'redisdb0');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertTrue($exist);
|
|
|
|
|
|
|
|
$result = Cache::write('save_in_1', true, 'redisdb1');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$exist = Cache::read('save_in_0', 'redisdb1');
|
|
|
|
$this->assertFalse($exist);
|
|
|
|
$exist = Cache::read('save_in_1', 'redisdb1');
|
|
|
|
$this->assertTrue($exist);
|
|
|
|
|
|
|
|
Cache::delete('save_in_0', 'redisdb0');
|
|
|
|
$exist = Cache::read('save_in_0', 'redisdb0');
|
|
|
|
$this->assertFalse($exist);
|
|
|
|
|
|
|
|
Cache::delete('save_in_1', 'redisdb1');
|
|
|
|
$exist = Cache::read('save_in_1', 'redisdb1');
|
|
|
|
$this->assertFalse($exist);
|
|
|
|
|
|
|
|
Cache::drop('redisdb0');
|
|
|
|
Cache::drop('redisdb1');
|
|
|
|
}
|
|
|
|
|
2016-02-27 05:07:58 +00:00
|
|
|
/**
|
|
|
|
* test write numbers method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testWriteNumbers() {
|
|
|
|
$result = Cache::write('test-counter', 1, 'redis');
|
|
|
|
$this->assertSame(1, Cache::read('test-counter', 'redis'));
|
|
|
|
|
|
|
|
$result = Cache::write('test-counter', 0, 'redis');
|
|
|
|
$this->assertSame(0, Cache::read('test-counter', 'redis'));
|
|
|
|
|
|
|
|
$result = Cache::write('test-counter', -1, 'redis');
|
|
|
|
$this->assertSame(-1, Cache::read('test-counter', 'redis'));
|
|
|
|
}
|
|
|
|
|
2012-03-29 04:00:33 +00:00
|
|
|
/**
|
|
|
|
* testReadAndWriteCache method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testReadAndWriteCache() {
|
|
|
|
Cache::set(array('duration' => 1), null, 'redis');
|
|
|
|
|
|
|
|
$result = Cache::read('test', 'redis');
|
|
|
|
$expecting = '';
|
|
|
|
$this->assertEquals($expecting, $result);
|
|
|
|
|
|
|
|
$data = 'this is a test of the emergency broadcasting system';
|
|
|
|
$result = Cache::write('test', $data, 'redis');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = Cache::read('test', 'redis');
|
|
|
|
$expecting = $data;
|
|
|
|
$this->assertEquals($expecting, $result);
|
|
|
|
|
|
|
|
$data = array(1, 2, 3);
|
|
|
|
$this->assertTrue(Cache::write('array_data', $data, 'redis'));
|
|
|
|
$this->assertEquals($data, Cache::read('array_data', 'redis'));
|
|
|
|
|
|
|
|
Cache::delete('test', 'redis');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testExpiry method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testExpiry() {
|
|
|
|
Cache::set(array('duration' => 1), 'redis');
|
|
|
|
|
|
|
|
$result = Cache::read('test', 'redis');
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = 'this is a test of the emergency broadcasting system';
|
|
|
|
$result = Cache::write('other_test', $data, 'redis');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
$result = Cache::read('other_test', 'redis');
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
Cache::set(array('duration' => "+1 second"), 'redis');
|
|
|
|
|
|
|
|
$data = 'this is a test of the emergency broadcasting system';
|
|
|
|
$result = Cache::write('other_test', $data, 'redis');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
$result = Cache::read('other_test', 'redis');
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
Cache::config('redis', array('duration' => '+1 second'));
|
|
|
|
sleep(2);
|
|
|
|
|
|
|
|
$result = Cache::read('other_test', 'redis');
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
Cache::config('redis', array('duration' => '+29 days'));
|
|
|
|
$data = 'this is a test of the emergency broadcasting system';
|
|
|
|
$result = Cache::write('long_expiry_test', $data, 'redis');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
$result = Cache::read('long_expiry_test', 'redis');
|
|
|
|
$expecting = $data;
|
|
|
|
$this->assertEquals($expecting, $result);
|
|
|
|
|
|
|
|
Cache::config('redis', array('duration' => 3600));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testDeleteCache method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDeleteCache() {
|
|
|
|
$data = 'this is a test of the emergency broadcasting system';
|
|
|
|
$result = Cache::write('delete_test', $data, 'redis');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = Cache::delete('delete_test', 'redis');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testDecrement method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDecrement() {
|
|
|
|
Cache::delete('test_decrement', 'redis');
|
|
|
|
$result = Cache::write('test_decrement', 5, 'redis');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = Cache::decrement('test_decrement', 1, 'redis');
|
|
|
|
$this->assertEquals(4, $result);
|
|
|
|
|
|
|
|
$result = Cache::read('test_decrement', 'redis');
|
|
|
|
$this->assertEquals(4, $result);
|
|
|
|
|
|
|
|
$result = Cache::decrement('test_decrement', 2, 'redis');
|
|
|
|
$this->assertEquals(2, $result);
|
|
|
|
|
|
|
|
$result = Cache::read('test_decrement', 'redis');
|
|
|
|
$this->assertEquals(2, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testIncrement method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testIncrement() {
|
|
|
|
Cache::delete('test_increment', 'redis');
|
|
|
|
$result = Cache::increment('test_increment', 1, 'redis');
|
|
|
|
$this->assertEquals(1, $result);
|
|
|
|
|
|
|
|
$result = Cache::read('test_increment', 'redis');
|
|
|
|
$this->assertEquals(1, $result);
|
|
|
|
|
|
|
|
$result = Cache::increment('test_increment', 2, 'redis');
|
|
|
|
$this->assertEquals(3, $result);
|
|
|
|
|
|
|
|
$result = Cache::read('test_increment', 'redis');
|
|
|
|
$this->assertEquals(3, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test clearing redis.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testClear() {
|
|
|
|
Cache::config('redis2', array(
|
|
|
|
'engine' => 'Redis',
|
|
|
|
'prefix' => 'cake2_',
|
|
|
|
'duration' => 3600
|
|
|
|
));
|
|
|
|
|
|
|
|
Cache::write('some_value', 'cache1', 'redis');
|
|
|
|
$result = Cache::clear(true, 'redis');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertEquals('cache1', Cache::read('some_value', 'redis'));
|
|
|
|
|
|
|
|
Cache::write('some_value', 'cache2', 'redis2');
|
|
|
|
$result = Cache::clear(false, 'redis');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertFalse(Cache::read('some_value', 'redis'));
|
|
|
|
$this->assertEquals('cache2', Cache::read('some_value', 'redis2'));
|
|
|
|
|
|
|
|
Cache::clear(false, 'redis2');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that a 0 duration can successfully write.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testZeroDuration() {
|
|
|
|
Cache::config('redis', array('duration' => 0));
|
|
|
|
$result = Cache::write('test_key', 'written!', 'redis');
|
|
|
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$result = Cache::read('test_key', 'redis');
|
|
|
|
$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::config('redis_groups', array(
|
|
|
|
'engine' => 'Redis',
|
|
|
|
'duration' => 3600,
|
|
|
|
'groups' => array('group_a', 'group_b'),
|
|
|
|
'prefix' => 'test_'
|
|
|
|
));
|
|
|
|
Cache::config('redis_helper', array(
|
|
|
|
'engine' => 'Redis',
|
|
|
|
'duration' => 3600,
|
|
|
|
'prefix' => 'test_'
|
|
|
|
));
|
|
|
|
$this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
|
|
|
|
$this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
|
|
|
|
|
|
|
|
Cache::increment('group_a', 1, 'redis_helper');
|
|
|
|
$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
|
|
|
|
$this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
|
|
|
|
$this->assertEquals('value2', Cache::read('test_groups', 'redis_groups'));
|
|
|
|
|
|
|
|
Cache::increment('group_b', 1, 'redis_helper');
|
|
|
|
$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
|
|
|
|
$this->assertTrue(Cache::write('test_groups', 'value3', 'redis_groups'));
|
|
|
|
$this->assertEquals('value3', Cache::read('test_groups', 'redis_groups'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that deleteing from a groups-enabled config is possible
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGroupDelete() {
|
|
|
|
Cache::config('redis_groups', array(
|
|
|
|
'engine' => 'Redis',
|
|
|
|
'duration' => 3600,
|
|
|
|
'groups' => array('group_a', 'group_b')
|
|
|
|
));
|
|
|
|
$this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
|
|
|
|
$this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
|
|
|
|
$this->assertTrue(Cache::delete('test_groups', 'redis_groups'));
|
|
|
|
|
|
|
|
$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test clearing a cache group
|
|
|
|
*
|
|
|
|
* @return void
|
2013-01-11 14:06:54 +00:00
|
|
|
*/
|
2012-03-29 04:00:33 +00:00
|
|
|
public function testGroupClear() {
|
|
|
|
Cache::config('redis_groups', array(
|
|
|
|
'engine' => 'Redis',
|
|
|
|
'duration' => 3600,
|
|
|
|
'groups' => array('group_a', 'group_b')
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
|
|
|
|
$this->assertTrue(Cache::clearGroup('group_a', 'redis_groups'));
|
|
|
|
$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
|
|
|
|
|
|
|
|
$this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
|
|
|
|
$this->assertTrue(Cache::clearGroup('group_b', 'redis_groups'));
|
|
|
|
$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
|
|
|
|
}
|
|
|
|
|
2015-07-27 21:42:00 +00:00
|
|
|
/**
|
2015-08-10 10:06:19 +00:00
|
|
|
* Test add method.
|
2015-07-27 21:42:00 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAdd() {
|
|
|
|
Cache::delete('test_add_key', 'redis');
|
|
|
|
|
|
|
|
$result = Cache::add('test_add_key', 'test data', 'redis');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$expected = 'test data';
|
|
|
|
$result = Cache::read('test_add_key', 'redis');
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$result = Cache::add('test_add_key', 'test data 2', 'redis');
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2012-04-24 02:27:27 +00:00
|
|
|
}
|