updating Cache engines, fixes #4759

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7045 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2008-05-26 19:24:09 +00:00
parent ba052c6e7e
commit 13a717aed6
7 changed files with 62 additions and 34 deletions

View file

@ -97,17 +97,21 @@ class Cache extends Object {
* @return array(engine, settings) on success, false on failure
* @access public
*/
function config($name = 'default', $settings = array()) {
function config($name = null, $settings = array()) {
$_this =& Cache::getInstance();
if (is_array($name)) {
$settings = $name;
}
if ($name === null) {
$name = 'default';
}
if (!empty($settings)) {
$_this->__name == null;
$_this->__name = null;
$_this->__config[$name] = $settings;
} elseif (isset($_this->__config[$name])) {
$settings = array_merge($_this->__config[$name], $settings);
$settings = $_this->__config[$name];
} elseif ($_this->__name !== null && isset($_this->__config[$_this->__name])) {
$name = $_this->__name;
$settings = $_this->__config[$_this->__name];
@ -126,10 +130,9 @@ class Cache extends Object {
return false;
}
$_this->__name = $name;
$_this->__config[$name] = $_this->settings($engine);
}
$settings = $_this->__config[$name];
$settings = $_this->__config[$name] = $_this->settings($engine);
return compact('engine', 'settings');
}
/**
@ -195,6 +198,7 @@ class Cache extends Object {
$config = $duration;
$duration = null;
}
$current = $_this->__name;
$config = $_this->config($config);
if (!is_array($config)) {
@ -222,8 +226,9 @@ class Cache extends Object {
if ($duration < 1) {
return false;
}
$success = $_this->_Engine[$engine]->write($settings['prefix'] . $key, $value, $duration);
$_this->_Engine[$engine]->init($settings);
$_this->config($current);
return $success;
}
/**
@ -236,12 +241,13 @@ class Cache extends Object {
*/
function read($key, $config = null) {
$_this =& Cache::getInstance();
$current = $_this->__name;
$config = $_this->config($config);
if (!is_array($config)) {
return null;
}
extract($config);
if (!$_this->isInitialized($engine)) {
@ -251,7 +257,7 @@ class Cache extends Object {
return false;
}
$success = $_this->_Engine[$engine]->read($settings['prefix'] . $key);
$_this->_Engine[$engine]->init($settings);
$_this->config($current);
return $success;
}
/**
@ -264,7 +270,7 @@ class Cache extends Object {
*/
function delete($key, $config = null) {
$_this =& Cache::getInstance();
$current = $_this->__name;
$config = $_this->config($config);
extract($config);
@ -277,7 +283,7 @@ class Cache extends Object {
}
$success = $_this->_Engine[$engine]->delete($settings['prefix'] . $key);
$_this->_Engine[$engine]->init($settings);
$_this->config($current);
return $success;
}
/**
@ -290,6 +296,7 @@ class Cache extends Object {
*/
function clear($check = false, $config = null) {
$_this =& Cache::getInstance();
$current = $_this->__name;
$config = $_this->config($config);
extract($config);
@ -297,7 +304,7 @@ class Cache extends Object {
return false;
}
$success = $_this->_Engine[$engine]->clear($check);
$_this->_Engine[$engine]->init($settings);
$_this->config($current);
return $success;
}
/**
@ -365,7 +372,7 @@ class CacheEngine extends Object {
* @var int
* @access public
*/
var $settings;
var $settings = array();
/**
* Iitialize the cache engine
*
@ -376,7 +383,7 @@ class CacheEngine extends Object {
* @access public
*/
function init($settings = array()) {
$this->settings = array_merge(array('prefix' => 'cake_', 'duration'=> 3600, 'probability'=> 100), $settings);
$this->settings = array_merge(array('prefix' => 'cake_', 'duration'=> 3600, 'probability'=> 100), $this->settings, $settings);
return true;
}
/**

View file

@ -31,7 +31,7 @@
* @package cake
* @subpackage cake.cake.libs.cache
*/
class APCEngine extends CacheEngine {
class ApcEngine extends CacheEngine {
/**
* Initialize the Cache Engine
*
@ -44,7 +44,7 @@ class APCEngine extends CacheEngine {
* @access public
*/
function init($settings = array()) {
parent::init($settings);
parent::init(array_merge(array('engine' => 'Apc'), $settings));
return function_exists('apc_cache_info');
}
/**

View file

@ -85,9 +85,7 @@ class FileEngine extends CacheEngine {
* @access public
*/
function init($settings = array()) {
parent::init($settings);
$defaults = array('path' => CACHE, 'prefix'=> 'cake_', 'lock'=> false, 'serialize'=> true);
$this->settings = array_merge($defaults, $this->settings, $settings);
parent::init(array_merge(array('engine' => 'File', 'path' => CACHE, 'prefix'=> 'cake_', 'lock'=> false, 'serialize'=> true), $settings));
if(!isset($this->__File)) {
$this->__File =& new File($this->settings['path'] . DS . 'cake');
}
@ -134,6 +132,7 @@ class FileEngine extends CacheEngine {
$lineBreak = "\r\n";
$windows = true;
}
if (!empty($this->settings['serialize'])) {
if ($windows) {
$data = str_replace('\\', '\\\\\\\\', serialize($data));

View file

@ -62,9 +62,7 @@ class MemcacheEngine extends CacheEngine {
if (!class_exists('Memcache')) {
return false;
}
parent::init($settings);
$defaults = array('servers' => array('127.0.0.1'), 'compress'=> false);
$this->settings = array_merge($this->settings, $defaults, $settings);
parent::init(array_merge(array('engine'=> 'Memcache', 'servers' => array('127.0.0.1'), 'compress'=> false), $settings));
if ($this->settings['compress']) {
$this->settings['compress'] = MEMCACHE_COMPRESSED;

View file

@ -53,9 +53,7 @@ class XcacheEngine extends CacheEngine {
* @access public
*/
function init($settings) {
parent::init($settings);
$defaults = array('PHP_AUTH_USER' => 'cake', 'PHP_AUTH_PW' => 'cake');
$this->settings = array_merge($this->settings, $defaults, $settings);
parent::init(array_merge(array('engine' => 'Xcache', 'PHP_AUTH_USER' => 'cake', 'PHP_AUTH_PW' => 'cake'), $settings));
return function_exists('xcache_info');
}
/**

View file

@ -26,7 +26,7 @@
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
uses('cache');
App::import('Cache');
/**
* Short description for class.
*
@ -39,6 +39,7 @@ class CacheTest extends UnitTestCase {
$this->skipif (false, 'CacheTest not implemented');
}
function testConfig() {
$settings = array('engine' => 'File', 'path' => TMP . 'tests', 'prefix' => 'cake_test_');
$results = Cache::config('new', $settings);
@ -53,10 +54,29 @@ class CacheTest extends UnitTestCase {
$this->assertEqual($result['settings'], Cache::settings('File'));
}
function testWritingWithConfig() {
Cache::config('sessions');
Cache::write('test_somthing', 'this is the test data', 'tests');
$expected = array(
'path' => TMP . 'sessions',
'prefix' => 'cake_',
'lock' => false,
'serialize' => true,
'duration' => 3600,
'probability' => 100,
'engine' => 'File',
);
$this->assertEqual($expected, Cache::settings('File'));
}
function testInitSettings() {
Cache::engine('File', array('path' => TMP . 'tests'));
$settings = Cache::settings();
$expecting = array(
'engine' => 'File',
'duration'=> 3600,
'probability' => 100,
'path'=> TMP . 'tests',

View file

@ -26,17 +26,24 @@
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
uses('cache', 'cache' . DS . 'file');
App::import(array('Cache', 'cache' . DS . 'file'));
/**
* Short description for class.
*
* @package cake.tests
* @subpackage cake.tests.cases.libs.cache
*/
class FileEngineTest extends UnitTestCase {
class FileEngineTest extends CakeTestCase {
function startTest() {
Cache::config();
var $config = array();
function start() {
$this->config = Cache::config('default');
$settings = Cache::config('default', array('engine'=> 'File', 'path' => CACHE));
}
function end() {
Cache::config('default', $this->config['settings']);
}
function testCacheDirChange() {
@ -118,7 +125,7 @@ class FileEngineTest extends UnitTestCase {
$this->assertIdentical($read, serialize($data));
$this->assertIdentical($newread, $data);
$this->assertIdentical(unserialize($newread), $data);
}
@ -152,7 +159,7 @@ class FileEngineTest extends UnitTestCase {
$this->assertFalse(file_exists(CACHE . 'cake_serialize_test2'));
$this->assertFalse(file_exists(CACHE . 'cake_serialize_test3'));
$result = Cache::config('tests', array('engine'=> 'File', 'path' => CACHE . 'views'));
$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);
@ -206,6 +213,8 @@ class FileEngineTest extends UnitTestCase {
$this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_12'));
clearCache('controller_view');
Cache::engine('File', array('path' => CACHE));
}
function testKeyPath() {
@ -261,8 +270,5 @@ class FileEngineTest extends UnitTestCase {
$this->assertEqual($expected, $data);
}
function tearDown() {
Cache::config('default');
}
}
?>