updating cache documentation and tests refs #5347

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8017 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2009-02-06 22:56:06 +00:00
parent 9d24c1746f
commit bf1e080aeb
5 changed files with 77 additions and 66 deletions

View file

@ -220,19 +220,17 @@ class Cache extends Object {
*
* @param string $key Identifier for the data
* @param mixed $value Data to be cached - anything except a resource
* @param mixed $config Optional - string configuration name, a duration for expiration,
* or array('config' => 'string configuration name', 'duration' => 'duration for expiration')
* @param string $config Optional - string configuration name
* @return boolean True if the data was successfully cached, false on failure
* @access public
* @static
*/
function write($key, $value, $config = null) {
$_this =& Cache::getInstance();
$thisDuration = null;
if (is_array($config)) {
extract($config);
} else if ($config && (is_numeric($config) || is_numeric($config[0]) || (isset($config[1]) && is_numeric($config[1])))) {
$thisDuration = $config;
$config = null;
}
@ -259,13 +257,6 @@ class Cache extends Object {
return false;
}
if ($thisDuration !== null) {
if (!is_numeric($thisDuration)) {
$thisDuration = strtotime($thisDuration) - time();
}
$duration = $thisDuration;
}
if ($duration < 1) {
return false;
}

View file

@ -63,17 +63,21 @@ class ApcEngineTest extends UnitTestCase {
* @return void
*/
function testReadAndWriteCache() {
Cache::set(array('duration' => 1));
$result = Cache::read('test');
$expecting = '';
$this->assertEqual($result, $expecting);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('test', $data, 1);
$result = Cache::write('test', $data);
$this->assertTrue($result);
$result = Cache::read('test');
$expecting = $data;
$this->assertEqual($result, $expecting);
Cache::delete('test');
}
/**
* testExpiry method
@ -82,25 +86,32 @@ class ApcEngineTest extends UnitTestCase {
* @return void
*/
function testExpiry() {
sleep(2);
Cache::set(array('duration' => 1));
$result = Cache::read('test');
$this->assertFalse($result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 1);
$result = Cache::write('other_test', $data);
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test');
$this->assertFalse($result);
Cache::set(array('duration' => "+1 second"));
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, "+1 second");
$result = Cache::write('other_test', $data);
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test');
$this->assertFalse($result);
sleep(2);
$result = Cache::read('other_test');
$this->assertFalse($result);
}
/**
* testDeleteCache method

View file

@ -90,17 +90,21 @@ class FileEngineTest extends CakeTestCase {
$result = Cache::write(null, 'here');
$this->assertFalse($result);
Cache::set(array('duration' => 1));
$result = Cache::read('test');
$expecting = '';
$this->assertEqual($result, $expecting);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('test', $data, 1);
$result = Cache::write('test', $data);
$this->assertTrue(file_exists(CACHE . 'cake_test'));
$result = Cache::read('test');
$expecting = $data;
$this->assertEqual($result, $expecting);
Cache::delete('test');
}
/**
* testExpiry method
@ -109,20 +113,23 @@ class FileEngineTest extends CakeTestCase {
* @return void
*/
function testExpiry() {
sleep(2);
Cache::set(array('duration' => 1));
$result = Cache::read('test');
$this->assertFalse($result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 1);
$result = Cache::write('other_test', $data);
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test');
$this->assertFalse($result);
Cache::set(array('duration' => "+1 second"));
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, "+1 second");
$result = Cache::write('other_test', $data);
$this->assertTrue($result);
sleep(2);
@ -179,14 +186,14 @@ class FileEngineTest extends CakeTestCase {
* @return void
*/
function testClear() {
Cache::engine('File', array('duration' => 1));
$data = 'this is a test of the emergency broadcasting system';
$write = Cache::write('serialize_test1', $data, 1);
$write = Cache::write('serialize_test2', $data, 1);
$write = Cache::write('serialize_test3', $data, 1);
$write = Cache::write('serialize_test1', $data);
$write = Cache::write('serialize_test2', $data);
$write = Cache::write('serialize_test3', $data);
$this->assertTrue(file_exists(CACHE . 'cake_serialize_test1'));
$this->assertTrue(file_exists(CACHE . 'cake_serialize_test2'));
$this->assertTrue(file_exists(CACHE . 'cake_serialize_test3'));
Cache::engine('File', array('duration' => 1));
sleep(2);
$result = Cache::clear(true);
$this->assertTrue($result);
@ -195,9 +202,9 @@ class FileEngineTest extends CakeTestCase {
$this->assertFalse(file_exists(CACHE . 'cake_serialize_test3'));
$data = 'this is a test of the emergency broadcasting system';
$write = Cache::write('serialize_test1', $data, 1);
$write = Cache::write('serialize_test2', $data, 1);
$write = Cache::write('serialize_test3', $data, 1);
$write = Cache::write('serialize_test1', $data);
$write = Cache::write('serialize_test2', $data);
$write = Cache::write('serialize_test3', $data);
$this->assertTrue(file_exists(CACHE . 'cake_serialize_test1'));
$this->assertTrue(file_exists(CACHE . 'cake_serialize_test2'));
$this->assertTrue(file_exists(CACHE . 'cake_serialize_test3'));
@ -211,12 +218,12 @@ class FileEngineTest extends CakeTestCase {
$result = Cache::engine('File', array('path' => CACHE . 'views'));
$data = 'this is a test of the emergency broadcasting system';
$write = Cache::write('controller_view_1', $data, 1);
$write = Cache::write('controller_view_2', $data, 1);
$write = Cache::write('controller_view_3', $data, 1);
$write = Cache::write('controller_view_10', $data, 1);
$write = Cache::write('controller_view_11', $data, 1);
$write = Cache::write('controller_view_12', $data, 1);
$write = Cache::write('controller_view_1', $data);
$write = Cache::write('controller_view_2', $data);
$write = Cache::write('controller_view_3', $data);
$write = Cache::write('controller_view_10', $data);
$write = Cache::write('controller_view_11', $data);
$write = Cache::write('controller_view_12', $data);
$this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_1'));
$this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_2'));
$this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_3'));
@ -240,12 +247,12 @@ class FileEngineTest extends CakeTestCase {
$this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_11'));
$this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_12'));
$write = Cache::write('controller_view_1', $data, 1);
$write = Cache::write('controller_view_2', $data, 1);
$write = Cache::write('controller_view_3', $data, 1);
$write = Cache::write('controller_view_10', $data, 1);
$write = Cache::write('controller_view_11', $data, 1);
$write = Cache::write('controller_view_12', $data, 1);
$write = Cache::write('controller_view_1', $data);
$write = Cache::write('controller_view_2', $data);
$write = Cache::write('controller_view_3', $data);
$write = Cache::write('controller_view_10', $data);
$write = Cache::write('controller_view_11', $data);
$write = Cache::write('controller_view_12', $data);
$this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_1'));
$this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_2'));
$this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_3'));

View file

@ -137,17 +137,21 @@ class MemcacheEngineTest extends CakeTestCase {
* @return void
*/
function testReadAndWriteCache() {
Cache::set(array('duration' => 1));
$result = Cache::read('test');
$expecting = '';
$this->assertEqual($result, $expecting);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('test', $data, 1);
$result = Cache::write('test', $data);
$this->assertTrue($result);
$result = Cache::read('test');
$expecting = $data;
$this->assertEqual($result, $expecting);
Cache::delete('test');
}
/**
* testExpiry method
@ -156,32 +160,28 @@ class MemcacheEngineTest extends CakeTestCase {
* @return void
*/
function testExpiry() {
sleep(2);
Cache::set(array('duration' => 1));
$result = Cache::read('test');
$this->assertFalse($result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 1);
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test');
$this->assertFalse($result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, "+1 second");
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test');
$this->assertFalse($result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data);
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test');
$this->assertEqual($result, $data);
$this->assertFalse($result);
Cache::set(array('duration' => "+1 second"));
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data);
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test');
$this->assertFalse($result);
Cache::engine('Memcache', array('duration' => '+1 second'));
sleep(2);
@ -206,4 +206,4 @@ class MemcacheEngineTest extends CakeTestCase {
$this->assertTrue($result);
}
}
?>
?>

View file

@ -80,17 +80,21 @@ class XcacheEngineTest extends UnitTestCase {
* @return void
*/
function testReadAndWriteCache() {
Cache::set(array('duration' => 1));
$result = Cache::read('test');
$expecting = '';
$this->assertEqual($result, $expecting);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('test', $data, 1);
$result = Cache::write('test', $data);
$this->assertTrue($result);
$result = Cache::read('test');
$expecting = $data;
$this->assertEqual($result, $expecting);
Cache::delete('test');
}
/**
* testExpiry method
@ -99,29 +103,27 @@ class XcacheEngineTest extends UnitTestCase {
* @return void
*/
function testExpiry() {
Cache::engine('Xcache', array('duration' => 4));
sleep(3);
Cache::set(array('duration' => 1));
$result = Cache::read('test');
$this->assertFalse($result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 1);
$result = Cache::write('other_test', $data);
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test');
$this->assertFalse($result);
Cache::set(array('duration' => "+1 second"));
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, "+1 second");
$result = Cache::write('other_test', $data);
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test');
$this->assertFalse($result);
Cache::engine('Xcache', array('duration' => 3600));
}
/**
* testDeleteCache method