updating cache engines so key will expire when duration is changed, updated datasource and configure to for new settings, closes #5083

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7364 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2008-07-26 20:00:20 +00:00
parent 4451c02b1c
commit caa7bb6218
11 changed files with 80 additions and 45 deletions

View file

@ -378,6 +378,9 @@ class CacheEngine extends Object {
*/ */
function init($settings = array()) { function init($settings = array()) {
$this->settings = array_merge(array('prefix' => 'cake_', 'duration'=> 3600, 'probability'=> 100), $this->settings, $settings); $this->settings = array_merge(array('prefix' => 'cake_', 'duration'=> 3600, 'probability'=> 100), $this->settings, $settings);
if (!is_numeric($this->settings['duration'])) {
$this->settings['duration'] = strtotime($this->settings['duration']) - time();
}
return true; return true;
} }
/** /**

View file

@ -57,6 +57,8 @@ class ApcEngine extends CacheEngine {
* @access public * @access public
*/ */
function write($key, &$value, $duration) { function write($key, &$value, $duration) {
$expires = time() + $duration;
apc_store($key.'_expires', $expires, $duration);
return apc_store($key, $value, $duration); return apc_store($key, $value, $duration);
} }
/** /**
@ -67,6 +69,11 @@ class ApcEngine extends CacheEngine {
* @access public * @access public
*/ */
function read($key) { function read($key) {
$time = time();
$cachetime = intval(apc_fetch($key.'_expires'));
if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
return false;
}
return apc_fetch($key); return apc_fetch($key);
} }
/** /**

View file

@ -128,9 +128,6 @@ class FileEngine extends CacheEngine {
return false; return false;
} }
if ($duration == null) {
$duration = $this->settings['duration'];
}
$lineBreak = "\n"; $lineBreak = "\n";
if ($this->settings['isWindows']) { if ($this->settings['isWindows']) {
@ -168,9 +165,10 @@ class FileEngine extends CacheEngine {
if ($this->settings['lock']) { if ($this->settings['lock']) {
$this->__File->lock = true; $this->__File->lock = true;
} }
$cachetime = $this->__File->read(11); $time = time();
$cachetime = intval($this->__File->read(11));
if ($cachetime !== false && intval($cachetime) < time()) { if ($cachetime !== false && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
$this->__File->close(); $this->__File->close();
$this->__File->delete(); $this->__File->delete();
return false; return false;

View file

@ -100,6 +100,8 @@ class MemcacheEngine extends CacheEngine {
* @access public * @access public
*/ */
function write($key, &$value, $duration) { function write($key, &$value, $duration) {
$expires = time() + $duration;
$this->__Memcache->set($key.'_expires', $expires, $this->settings['compress'], $duration);
return $this->__Memcache->set($key, $value, $this->settings['compress'], $duration); return $this->__Memcache->set($key, $value, $this->settings['compress'], $duration);
} }
/** /**
@ -110,6 +112,11 @@ class MemcacheEngine extends CacheEngine {
* @access public * @access public
*/ */
function read($key) { function read($key) {
$time = time();
$cachetime = intval($this->__Memcache->get($key.'_expires'));
if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
return false;
}
return $this->__Memcache->get($key); return $this->__Memcache->get($key);
} }
/** /**

View file

@ -69,6 +69,8 @@ class XcacheEngine extends CacheEngine {
* @access public * @access public
*/ */
function write($key, &$value, $duration) { function write($key, &$value, $duration) {
$expires = time() + $duration;
xcache_set($key.'_expires', $expires, $duration);
return xcache_set($key, $value, $duration); return xcache_set($key, $value, $duration);
} }
/** /**
@ -80,6 +82,11 @@ class XcacheEngine extends CacheEngine {
*/ */
function read($key) { function read($key) {
if (xcache_isset($key)) { if (xcache_isset($key)) {
$time = time();
$cachetime = intval(xcache_get($key.'_expires'));
if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
return false;
}
return xcache_get($key); return xcache_get($key);
} }
return false; return false;

View file

@ -659,7 +659,9 @@ class Configure extends Object {
$duration = $cache['settings']['duration']; $duration = $cache['settings']['duration'];
if (Configure::read() >= 1) { if (Configure::read() >= 1) {
$duration = 10; $duration = '+10 seconds';
} else {
$duration = '+999 days';
} }
if (Cache::config('_cake_core_') === false) { if (Cache::config('_cake_core_') === false) {

View file

@ -199,18 +199,12 @@ class DataSource extends Object {
return $this->_sources; return $this->_sources;
} }
if (Configure::read() > 0) {
$expires = "+30 seconds";
} else {
$expires = "+999 days";
}
$key = ConnectionManager::getSourceName($this) . '_' . Inflector::slug($this->config['database']) . '_list'; $key = ConnectionManager::getSourceName($this) . '_' . Inflector::slug($this->config['database']) . '_list';
$sources = Cache::read($key, '_cake_model_'); $sources = Cache::read($key, '_cake_model_');
if ($sources == null) { if (empty($sources)) {
$sources = $data; $sources = $data;
Cache::write($key, $data, array('duration' => $expires, 'config' => '_cake_model_')); Cache::write($key, $data, '_cake_model_');
} }
$this->_sources = $sources; $this->_sources = $sources;
@ -385,11 +379,6 @@ class DataSource extends Object {
if ($this->cacheSources === false) { if ($this->cacheSources === false) {
return null; return null;
} }
if (Configure::read() > 0) {
$expires = "+15 seconds";
} else {
$expires = "+999 days";
}
if ($data !== null) { if ($data !== null) {
$this->__descriptions[$object] =& $data; $this->__descriptions[$object] =& $data;
@ -400,7 +389,7 @@ class DataSource extends Object {
if (empty($cache)) { if (empty($cache)) {
$cache = $data; $cache = $data;
Cache::write($key, $cache, array('duration' => $expires, 'config' => '_cake_model_')); Cache::write($key, $cache, '_cake_model_');
} }
return $cache; return $cache;

View file

@ -99,6 +99,7 @@ class CacheTest extends CakeTestCase {
'duration' => 3600, 'duration' => 3600,
'probability' => 100, 'probability' => 100,
'engine' => 'File', 'engine' => 'File',
'isWindows' => (substr(PHP_OS, 0, 3) == "WIN")
); );
$this->assertEqual($expected, Cache::settings('File')); $this->assertEqual($expected, Cache::settings('File'));
} }
@ -118,7 +119,8 @@ class CacheTest extends CakeTestCase {
'path'=> TMP . 'tests', 'path'=> TMP . 'tests',
'prefix'=> 'cake_', 'prefix'=> 'cake_',
'lock' => false, 'lock' => false,
'serialize'=> true 'serialize'=> true,
'isWindows' => (substr(PHP_OS, 0, 3) == "WIN")
); );
$this->assertEqual($settings, $expecting); $this->assertEqual($settings, $expecting);
} }

View file

@ -294,9 +294,7 @@ class FileEngineTest extends CakeTestCase {
* @return void * @return void
*/ */
function testRemoveWindowsSlashesFromCache() { function testRemoveWindowsSlashesFromCache() {
Cache::engine('File', array('isWindows' => true, 'prefix' => null, 'path' => TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'tmp')); Cache::engine('File', array('isWindows' => true, 'prefix' => null, 'path' => TMP));
$data = Cache::read('dir_map');
$expected = array ( $expected = array (
'C:\dev\prj2\sites\cake\libs' => array( 'C:\dev\prj2\sites\cake\libs' => array(
@ -326,6 +324,9 @@ class FileEngineTest extends CakeTestCase {
'C:\dev\prj2\sites\main_site\views\helpers' => array( 'C:\dev\prj2\sites\main_site\views\helpers' => array(
0 => 'C:\dev\prj2\sites\main_site\views\helpers')); 0 => 'C:\dev\prj2\sites\main_site\views\helpers'));
$data = Cache::write('test_dir_map', $expected);
$data = Cache::read('test_dir_map');
Cache::delete('dir_map');
$this->assertEqual($expected, $data); $this->assertEqual($expected, $data);
} }

View file

@ -137,6 +137,21 @@ class MemcacheEngineTest extends UnitTestCase {
sleep(2); sleep(2);
$result = Cache::read('other_test'); $result = Cache::read('other_test');
$this->assertFalse($result); $this->assertFalse($result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data);
$this->assertTrue($result);
$result = Cache::read('other_test');
$this->assertEqual($result, $data);
Cache::engine('Memcache', array('duration' => '+1 second'));
sleep(2);
$result = Cache::read('other_test');
$this->assertFalse($result);
Cache::engine('Memcache', array('duration' => 3600));
} }
/** /**
* testDeleteCache method * testDeleteCache method

View file

@ -70,8 +70,8 @@ class XcacheEngineTest extends UnitTestCase {
'duration'=> 3600, 'duration'=> 3600,
'probability' => 100, 'probability' => 100,
'engine' => 'Xcache', 'engine' => 'Xcache',
'PHP_AUTH_USER' => 'cake', 'PHP_AUTH_USER' => 'user',
'PHP_AUTH_PW' => 'cake', 'PHP_AUTH_PW' => 'password',
); );
$this->assertEqual($settings, $expecting); $this->assertEqual($settings, $expecting);
} }
@ -101,6 +101,8 @@ class XcacheEngineTest extends UnitTestCase {
* @return void * @return void
*/ */
function testExpiry() { function testExpiry() {
Cache::engine('Xcache', array('duration' => 4));
sleep(3); sleep(3);
$result = Cache::read('test'); $result = Cache::read('test');
$this->assertFalse($result); $this->assertFalse($result);
@ -109,7 +111,7 @@ class XcacheEngineTest extends UnitTestCase {
$result = Cache::write('other_test', $data, 1); $result = Cache::write('other_test', $data, 1);
$this->assertTrue($result); $this->assertTrue($result);
sleep(3); sleep(2);
$result = Cache::read('other_test'); $result = Cache::read('other_test');
$this->assertFalse($result); $this->assertFalse($result);
@ -117,9 +119,11 @@ class XcacheEngineTest extends UnitTestCase {
$result = Cache::write('other_test', $data, "+1 second"); $result = Cache::write('other_test', $data, "+1 second");
$this->assertTrue($result); $this->assertTrue($result);
sleep(3); sleep(2);
$result = Cache::read('other_test'); $result = Cache::read('other_test');
$this->assertFalse($result); $this->assertFalse($result);
Cache::engine('Xcache', array('duration' => 3600));
} }
/** /**
* testDeleteCache method * testDeleteCache method