Refactoring new cache classes

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4936 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2007-04-30 07:38:47 +00:00
parent fad2dc55ad
commit bca9595101
7 changed files with 117 additions and 17 deletions

View file

@ -39,6 +39,7 @@ if (!defined('PHP5')) {
} }
$TIME_START = getMicrotime(); $TIME_START = getMicrotime();
require LIBS . 'object.php'; require LIBS . 'object.php';
require LIBS . 'cache.php';
require LIBS . 'session.php'; require LIBS . 'session.php';
require LIBS . 'security.php'; require LIBS . 'security.php';
require LIBS . 'inflector.php'; require LIBS . 'inflector.php';
@ -47,6 +48,21 @@ if (!defined('PHP5')) {
Configure::store(null, 'class.paths'); Configure::store(null, 'class.paths');
Configure::load('class.paths'); Configure::load('class.paths');
Configure::write('debug', DEBUG); Configure::write('debug', DEBUG);
if(isset($cakeCache)) {
$cache = 'File';
$settings = array();
if(isset($cakeCache[0])) {
$cache = $cakeCache[0];
}
if(isset($cakeCache[1])) {
$settings = $cakeCache[1];
}
Cache::engine($cache, $settings);
} else {
Cache::engine();
}
/** /**
* Check for IIS Server * Check for IIS Server
*/ */

View file

@ -79,7 +79,7 @@ class Cache extends Object {
if (class_exists($name.'Engine')) { if (class_exists($name.'Engine')) {
return true; return true;
} }
$fileName = strtolower($name).'_engine'; $fileName = strtolower($name);
if(vendor('cache_engines/'.$fileName)) { if(vendor('cache_engines/'.$fileName)) {
return true; return true;
@ -99,12 +99,12 @@ class Cache extends Object {
* @param array $parmas Optional associative array of parameters passed to the engine * @param array $parmas Optional associative array of parameters passed to the engine
* @return boolean True on success, false on failure * @return boolean True on success, false on failure
*/ */
function engine($name, &$params = array()) { function engine($name = 'File', &$params = array()) {
if(defined('DISABLE_CACHE')) { if(defined('DISABLE_CACHE')) {
return false; return false;
} }
$_this =& Cache::getInstance(); $_this =& Cache::getInstance();
$cacheClass= $name.'Engine'; $cacheClass = $name.'Engine';
if (!Cache::_includeEngine($name) || !class_exists($cacheClass)) { if (!Cache::_includeEngine($name) || !class_exists($cacheClass)) {
return false; return false;
@ -117,7 +117,7 @@ class Cache extends Object {
} }
return true; return true;
} }
$this->_Engine = null; $_this->_Engine = null;
return false; return false;
} }
/** /**
@ -227,6 +227,13 @@ class Cache extends Object {
$_this =& Cache::getInstance(); $_this =& Cache::getInstance();
return isset($_this->_Engine); return isset($_this->_Engine);
} }
function settings() {
$_this =& Cache::getInstance();
if(!is_null($_this->_Engine)) {
return $_this->_Engine->settings();
}
}
} }
/** /**
* Storage engine for CakePHP caching * Storage engine for CakePHP caching
@ -261,7 +268,7 @@ class CacheEngine extends Object {
* @return boolean True if the data was succesfully cached, false on failure * @return boolean True if the data was succesfully cached, false on failure
*/ */
function write($key, &$value, $duration = CACHE_DEFAULT_DURATION) { function write($key, &$value, $duration = CACHE_DEFAULT_DURATION) {
trigger_error(sprintf(__('Method set() not implemented in %s', true), get_class($this)), E_USER_ERROR); trigger_error(sprintf(__('Method write() not implemented in %s', true), get_class($this)), E_USER_ERROR);
} }
/** /**
* Read a value from the cache * Read a value from the cache
@ -270,7 +277,7 @@ class CacheEngine extends Object {
* @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
*/ */
function read($key) { function read($key) {
trigger_error(sprintf(__('Method get() not implemented in %s', true), get_class($this)), E_USER_ERROR); trigger_error(sprintf(__('Method read() not implemented in %s', true), get_class($this)), E_USER_ERROR);
} }
/** /**
* Delete a value from the cache * Delete a value from the cache
@ -287,5 +294,13 @@ class CacheEngine extends Object {
*/ */
function clear() { function clear() {
} }
/**
* Delete all values from the cache
*
* @return boolean True if the cache was succesfully cleared, false otherwise
*/
function settings() {
trigger_error(sprintf(__('Method settings() not implemented in %s', true), get_class($this)), E_USER_ERROR);
}
} }
?> ?>

View file

@ -80,5 +80,13 @@ class APCEngine extends CacheEngine {
function clear() { function clear() {
return apc_clear_cache('user'); return apc_clear_cache('user');
} }
/**
* Return the settings for this cache engine
*
* @return array list of settings for this engine
*/
function settings() {
return array('class' => get_class($this));
}
} }
?> ?>

View file

@ -236,6 +236,21 @@ class FileEngine extends CacheEngine {
$dir->close(); $dir->close();
return true; return true;
} }
/**
* Return the settings for this cache engine
*
* @return array list of settings for this engine
*/
function settings() {
$lock = 'false';
if($this->_lock) {
$lock = 'true';
}
return array('class' => get_class($this),
'directory' => $this->_dir,
'prefix' => $this->_prefix,
'lock' => $lock);
}
/** /**
* Get a filename-safe version of a string * Get a filename-safe version of a string
* *

View file

@ -107,5 +107,14 @@ class MemcacheEngine extends CacheEngine {
function clear() { function clear() {
return $this->__Memcache->flush(); return $this->__Memcache->flush();
} }
/**
* Return the settings for this cache engine
*
* @return array list of settings for this engine
*/
function settings() {
return array('class' => get_class($this),
'compress' => $this->_compress);
}
} }
?> ?>

View file

@ -117,5 +117,20 @@ class ModelEngine extends CacheEngine {
function clear() { function clear() {
return $this->_Model->deleteAll(null); return $this->_Model->deleteAll(null);
} }
/**
* Return the settings for this cache engine
*
* @return array list of settings for this engine
*/
function settings() {
$class = null;
if(is_a($this->_Model, 'Model')) {
$class = get_class($this->_Model);
}
return array('class' => get_class($this),
'modelName' => $class,
'dataField' => $this->_dataField,
'expiryField' => $this->_expiryField);
}
} }
?> ?>

View file

@ -28,7 +28,7 @@
<p> <p>
<span class="notice"> <span class="notice">
<?php <?php
__('Your /app/tmp directory is '); __('Your tmp directory is ');
if(is_writable(TMP)): if(is_writable(TMP)):
__('writable.'); __('writable.');
else: else:
@ -37,6 +37,28 @@
?> ?>
</span> </span>
</p> </p>
<p>
<span class="notice">
<?php
__('Your cache is ');
if (Cache::isInitialized()) {
__('set up and initialized properly.');
$settings = Cache::settings();
echo '<br />' . $settings['class'];
__(' is being used to cache, to change this edit config/core.php ');
echo '<br /> Settings: <ul>';
foreach ($settings as $name => $value): ?>
<li><?php echo $name . ': ' . $value;?> </li>
<?php
endforeach;
} else {
__('NOT working.');
echo '<br />';
__('Edit: config/core.php to insure you have the newset version of this file and the variable $cakeCache set properly');
}
?>
</span>
</p>
<p> <p>
<span class="notice"> <span class="notice">
<?php <?php
@ -48,7 +70,7 @@
else: else:
__('NOT present.'); __('NOT present.');
echo '<br/>'; echo '<br/>';
__('Rename /app/config/database.php.default to /app/config/database.php'); __('Rename config/database.php.default to config/database.php');
endif; endif;
?> ?>
</span> </span>