2005-07-04 01:07:14 +00:00
|
|
|
<?php
|
2005-08-21 06:49:02 +00:00
|
|
|
/* SVN FILE: $Id$ */
|
2005-06-30 02:09:47 +00:00
|
|
|
/**
|
2007-04-30 06:01:54 +00:00
|
|
|
* Caching for CakePHP.
|
2005-12-27 03:33:44 +00:00
|
|
|
*
|
|
|
|
*
|
2005-08-21 06:49:02 +00:00
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2007-02-02 10:39:45 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
2006-05-26 05:29:17 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2005-12-23 21:57:26 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2005-12-27 03:33:44 +00:00
|
|
|
* @filesource
|
2007-02-02 10:39:45 +00:00
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
2007-04-30 06:01:54 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4933
|
2006-05-26 05:29:17 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2005-08-21 06:49:02 +00:00
|
|
|
*/
|
2005-06-30 02:09:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Included libraries.
|
|
|
|
*/
|
2007-04-30 06:01:54 +00:00
|
|
|
if(!class_exists('Object')) {
|
|
|
|
uses('object');
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
/**
|
2007-04-30 06:01:54 +00:00
|
|
|
* Set defines if not set in core.php
|
|
|
|
*/
|
|
|
|
if(!defined('CACHE_DEFAULT_DURATION')) {
|
|
|
|
define('CACHE_DEFAULT_DURATION', 3600);
|
|
|
|
}
|
|
|
|
if(!defined('CACHE_GC_PROBABILITY')) {
|
|
|
|
define('CACHE_GC_PROBABILITY', 100);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Caching for CakePHP.
|
2005-12-27 03:33:44 +00:00
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
2005-08-21 06:49:02 +00:00
|
|
|
*/
|
2007-04-30 06:01:54 +00:00
|
|
|
class Cache extends Object {
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
2007-04-30 06:01:54 +00:00
|
|
|
* Cache engine to use
|
2007-05-20 05:35:13 +00:00
|
|
|
*
|
2007-04-30 06:01:54 +00:00
|
|
|
* @var object
|
2007-05-22 02:38:12 +00:00
|
|
|
* @access protected
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2007-04-30 06:01:54 +00:00
|
|
|
var $_Engine = null;
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
2007-05-20 05:35:13 +00:00
|
|
|
* Create cache.
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2007-04-30 06:01:54 +00:00
|
|
|
function __construct() {
|
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
2007-04-30 06:01:54 +00:00
|
|
|
* Returns a singleton instance
|
2006-02-18 23:42:21 +00:00
|
|
|
*
|
2007-04-30 06:01:54 +00:00
|
|
|
* @return object
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function &getInstance() {
|
|
|
|
static $instance = array();
|
|
|
|
if(!isset($instance[0]) || !$instance[0]) {
|
|
|
|
$instance[0] =& new Cache();
|
|
|
|
}
|
|
|
|
return $instance[0];
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Tries to find and include a file for a cache engine
|
2007-05-20 05:35:13 +00:00
|
|
|
*
|
|
|
|
* @param $name Name of the engine (without 'Engine')
|
2007-04-30 06:01:54 +00:00
|
|
|
* @return boolean
|
2007-05-22 02:38:12 +00:00
|
|
|
* @access protected
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2007-04-30 06:01:54 +00:00
|
|
|
function _includeEngine($name) {
|
|
|
|
if (class_exists($name.'Engine')) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-04-30 07:38:47 +00:00
|
|
|
$fileName = strtolower($name);
|
2007-04-30 06:01:54 +00:00
|
|
|
|
|
|
|
if(vendor('cache_engines/'.$fileName)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$fileName = dirname(__FILE__) . DS . 'cache' . DS . $fileName . '.php';
|
|
|
|
|
|
|
|
if(is_readable($fileName)) {
|
|
|
|
include $fileName;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
2007-04-30 06:01:54 +00:00
|
|
|
* Set the cache engine to use
|
2006-02-18 23:42:21 +00:00
|
|
|
*
|
2007-04-30 06:01:54 +00:00
|
|
|
* @param string $name Name of the engine (without 'Engine')
|
|
|
|
* @param array $parmas Optional associative array of parameters passed to the engine
|
|
|
|
* @return boolean True on success, false on failure
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2007-04-30 09:32:51 +00:00
|
|
|
function engine($name = 'File', $params = array()) {
|
2007-04-30 06:01:54 +00:00
|
|
|
if(defined('DISABLE_CACHE')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$_this =& Cache::getInstance();
|
2007-04-30 07:38:47 +00:00
|
|
|
$cacheClass = $name.'Engine';
|
2007-04-30 06:01:54 +00:00
|
|
|
|
|
|
|
if (!Cache::_includeEngine($name) || !class_exists($cacheClass)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$_this->_Engine =& new $cacheClass();
|
|
|
|
|
|
|
|
if ($_this->_Engine->init($params)) {
|
|
|
|
if(time() % CACHE_GC_PROBABILITY == 0) {
|
|
|
|
$_this->_Engine->gc();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-04-30 07:38:47 +00:00
|
|
|
$_this->_Engine = null;
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
2007-04-30 06:01:54 +00:00
|
|
|
* Write a value in the cache
|
2006-02-18 23:42:21 +00:00
|
|
|
*
|
2007-04-30 06:01:54 +00:00
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @param mixed $value Data to be cached - anything except a resource
|
|
|
|
* @param mixed $duration Optional - how long to cache the data, either in seconds or a string that can be parsed by the strtotime() function
|
|
|
|
* @return boolean True if the data was succesfully cached, false on failure
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2007-04-30 06:01:54 +00:00
|
|
|
function write($key, $value, $duration = CACHE_DEFAULT_DURATION) {
|
|
|
|
if (defined('DISABLE_CACHE')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$key = strval($key);
|
|
|
|
|
|
|
|
if(empty($key)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_resource($value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$duration = ife(is_string($duration), strtotime($duration), intval($duration));
|
|
|
|
|
|
|
|
if($duration < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$_this =& Cache::getInstance();
|
|
|
|
|
|
|
|
if(!isset($_this->_Engine)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $_this->_Engine->write($key, $value, $duration);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
2007-04-30 06:01:54 +00:00
|
|
|
* Read a value from the cache
|
2006-02-18 23:42:21 +00:00
|
|
|
*
|
2007-04-30 06:01:54 +00:00
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2007-04-30 09:12:49 +00:00
|
|
|
function read($key) {
|
2007-04-30 06:01:54 +00:00
|
|
|
if(defined('DISABLE_CACHE')) {
|
2007-04-30 09:12:49 +00:00
|
|
|
return false;
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
$key = strval($key);
|
|
|
|
|
|
|
|
if(empty($key)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$_this =& Cache::getInstance();
|
|
|
|
|
|
|
|
if(!isset($_this->_Engine)) {
|
|
|
|
return false;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-04-30 09:12:49 +00:00
|
|
|
return $_this->_Engine->read($key);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
2007-04-30 06:01:54 +00:00
|
|
|
* Delete a value from the cache
|
2006-02-18 23:42:21 +00:00
|
|
|
*
|
2007-04-30 06:01:54 +00:00
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2007-04-30 06:01:54 +00:00
|
|
|
function delete($key) {
|
|
|
|
if(defined('DISABLE_CACHE')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$key = strval($key);
|
|
|
|
|
|
|
|
if(empty($key)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$_this =& Cache::getInstance();
|
|
|
|
|
|
|
|
if(!isset($_this->_Engine)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $_this->_Engine->delete($key);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
2007-04-30 06:01:54 +00:00
|
|
|
* Delete all values from the cache
|
2006-02-18 23:42:21 +00:00
|
|
|
*
|
2007-04-30 06:01:54 +00:00
|
|
|
* @return boolean True if the cache was succesfully cleared, false otherwise
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2007-04-30 06:01:54 +00:00
|
|
|
function clear() {
|
|
|
|
if(defined('DISABLE_CACHE')) {
|
|
|
|
return false;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-04-30 06:01:54 +00:00
|
|
|
$_this =& Cache::getInstance();
|
|
|
|
|
|
|
|
if(!isset($_this->_Engine)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $_this->_Engine->clear();
|
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
2007-04-30 06:01:54 +00:00
|
|
|
* Check if Cache has initialized a working storage engine
|
2006-02-18 23:42:21 +00:00
|
|
|
*
|
|
|
|
* @return boolean
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2007-04-30 06:01:54 +00:00
|
|
|
function isInitialized() {
|
|
|
|
if(defined('DISABLE_CACHE')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$_this =& Cache::getInstance();
|
|
|
|
return isset($_this->_Engine);
|
|
|
|
}
|
2007-04-30 07:38:47 +00:00
|
|
|
|
2007-05-20 05:35:13 +00:00
|
|
|
/**
|
|
|
|
* Return the settings for current cache engine
|
|
|
|
*
|
|
|
|
* @return array list of settings for this engine
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-04-30 07:38:47 +00:00
|
|
|
function settings() {
|
|
|
|
$_this =& Cache::getInstance();
|
|
|
|
if(!is_null($_this->_Engine)) {
|
|
|
|
return $_this->_Engine->settings();
|
|
|
|
}
|
|
|
|
}
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Storage engine for CakePHP caching
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
*/
|
|
|
|
class CacheEngine extends Object {
|
|
|
|
/**
|
|
|
|
* Set up the cache engine
|
|
|
|
*
|
|
|
|
* Called automatically by the cache frontend
|
|
|
|
*
|
|
|
|
* @param array $params Associative array of parameters for the engine
|
|
|
|
* @return boolean True if the engine has been succesfully initialized, false if not
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function init($params) {
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Garbage collection
|
|
|
|
*
|
|
|
|
* Permanently remove all expired and deleted data
|
2007-05-20 05:35:13 +00:00
|
|
|
*
|
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function gc() {
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Write a value in the cache
|
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @param mixed $value Data to be cached
|
|
|
|
* @param mixed $duration How long to cache the data, in seconds
|
|
|
|
* @return boolean True if the data was succesfully cached, false on failure
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function write($key, &$value, $duration = CACHE_DEFAULT_DURATION) {
|
2007-04-30 07:38:47 +00:00
|
|
|
trigger_error(sprintf(__('Method write() not implemented in %s', true), get_class($this)), E_USER_ERROR);
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Read a value from the cache
|
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function read($key) {
|
2007-04-30 07:38:47 +00:00
|
|
|
trigger_error(sprintf(__('Method read() not implemented in %s', true), get_class($this)), E_USER_ERROR);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
2007-04-30 06:01:54 +00:00
|
|
|
* Delete a value from the cache
|
2006-02-18 23:42:21 +00:00
|
|
|
*
|
2007-04-30 06:01:54 +00:00
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2007-04-30 06:01:54 +00:00
|
|
|
function delete($key) {
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
2007-04-30 06:01:54 +00:00
|
|
|
* Delete all values from the cache
|
2006-02-18 23:42:21 +00:00
|
|
|
*
|
2007-04-30 06:01:54 +00:00
|
|
|
* @return boolean True if the cache was succesfully cleared, false otherwise
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function clear() {
|
|
|
|
}
|
2007-04-30 07:38:47 +00:00
|
|
|
/**
|
|
|
|
* Delete all values from the cache
|
|
|
|
*
|
|
|
|
* @return boolean True if the cache was succesfully cleared, false otherwise
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 07:38:47 +00:00
|
|
|
*/
|
|
|
|
function settings() {
|
|
|
|
trigger_error(sprintf(__('Method settings() not implemented in %s', true), get_class($this)), E_USER_ERROR);
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
?>
|