2001-03-02 00:32:28 +08:00
|
|
|
<?php
|
|
|
|
// +----------------------------------------------------------------------+
|
|
|
|
// | PHP version 4.0 |
|
|
|
|
// +----------------------------------------------------------------------+
|
|
|
|
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
|
|
|
|
// +----------------------------------------------------------------------+
|
|
|
|
// | This source file is subject to version 2.0 of the PHP license, |
|
|
|
|
// | that is bundled with this package in the file LICENSE, and is |
|
|
|
|
// | available at through the world-wide-web at |
|
|
|
|
// | http://www.php.net/license/2_02.txt. |
|
|
|
|
// | If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
// | obtain it through the world-wide-web, please send a note to |
|
|
|
|
// | license@php.net so we can mail you a copy immediately. |
|
|
|
|
// +----------------------------------------------------------------------+
|
|
|
|
// | Authors: Ulf Wendel <ulf.wendel@phpdoc.de> |
|
|
|
|
// | Sebastian Bergmann <sb@sebastian-bergmann.de> |
|
|
|
|
// +----------------------------------------------------------------------+
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache is a base class for cache implementations.
|
|
|
|
*
|
|
|
|
* TODO: Simple usage example goes here.
|
|
|
|
*
|
|
|
|
* @author Ulf Wendel <ulf.wendel@phpdoc.de>
|
|
|
|
* @version $Id$
|
|
|
|
* @package Cache
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
class Cache {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disables the caching.
|
|
|
|
*
|
|
|
|
* TODO: Add explanation what this is good for.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $no_cache = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Garbage collection: probability in seconds
|
|
|
|
*
|
|
|
|
* If set to a value above 0 a garbage collection will
|
|
|
|
* flush all cache entries older than the specified number
|
|
|
|
* of seconds.
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
* @see $gc_probability
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $gc_time = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Garbage collection: probability in percent
|
|
|
|
*
|
|
|
|
* TODO: Add an explanation.
|
|
|
|
*
|
|
|
|
* @var integer 0 => never
|
|
|
|
* @see $gc_time
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $gc_probability = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Storage container object.
|
|
|
|
*
|
|
|
|
* @var object Cache_Container
|
|
|
|
*/
|
|
|
|
var $container;
|
|
|
|
|
|
|
|
//
|
|
|
|
// public methods
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string Name of storage container class
|
|
|
|
* @param array Array with storage class dependend config options
|
|
|
|
*/
|
|
|
|
function Cache($storage_driver, $storage_options = "")
|
|
|
|
{
|
|
|
|
$storage_driver = strtolower($storage_driver);
|
|
|
|
$storage_class = 'Cache_Container_' . $storage_driver;
|
2001-03-02 18:29:34 +08:00
|
|
|
$storage_classfile = 'Cache/Container/' . $storage_driver . '.php';
|
2001-03-04 03:21:49 +08:00
|
|
|
|
2001-03-04 22:26:58 +08:00
|
|
|
include_once $storage_classfile;
|
2001-03-04 03:21:49 +08:00
|
|
|
$this->container = new $storage_class($storage_options);
|
|
|
|
$this->garbageCollection();
|
|
|
|
|
2001-03-02 00:32:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the requested dataset it if exists and is not expired
|
|
|
|
*
|
|
|
|
* @param string dataset ID
|
2001-03-04 03:21:49 +08:00
|
|
|
* @param string cache group
|
2001-03-02 00:32:28 +08:00
|
|
|
* @return mixed cached data or NULL on failure
|
|
|
|
* @access public
|
|
|
|
*/
|
2001-03-04 03:21:49 +08:00
|
|
|
function get($id, $group = "default") {
|
2001-03-02 00:32:28 +08:00
|
|
|
if ($this->no_cache)
|
|
|
|
return "";
|
|
|
|
|
2001-03-04 03:21:49 +08:00
|
|
|
if ($this->isCached($id, $group) && !$this->isExpired($id, $group))
|
|
|
|
return $this->load($id, $group);
|
2001-03-02 00:32:28 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
} // end func get
|
|
|
|
|
2001-03-04 03:21:49 +08:00
|
|
|
|
2001-03-02 00:32:28 +08:00
|
|
|
/**
|
|
|
|
* Stores the given data in the cache.
|
|
|
|
*
|
|
|
|
* @param string dataset ID used as cache identifier
|
|
|
|
* @param mixed data to cache
|
|
|
|
* @param integer lifetime of the cached data in seconds - 0 for endless
|
2001-03-04 03:21:49 +08:00
|
|
|
* @param string cache group
|
2001-03-02 00:32:28 +08:00
|
|
|
* @return boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
2001-03-04 03:21:49 +08:00
|
|
|
function save($id, $data, $expires = 0, $group = "default") {
|
2001-03-02 00:32:28 +08:00
|
|
|
if ($this->no_cache)
|
|
|
|
return true;
|
|
|
|
|
2001-03-04 03:21:49 +08:00
|
|
|
return $this->container->save($id, $data, $expires, $group, "");
|
2001-03-02 00:32:28 +08:00
|
|
|
} // end func save
|
|
|
|
|
2001-03-04 03:21:49 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores a dataset without additional userdefined data.
|
|
|
|
*
|
|
|
|
* @param string dataset ID
|
|
|
|
* @param mixed data to store
|
|
|
|
* @param string additional userdefined data
|
|
|
|
* @param mixed userdefined expire date
|
|
|
|
* @param string cache group
|
|
|
|
* @return boolean
|
|
|
|
* @throws CacheError
|
|
|
|
* @access public
|
|
|
|
* @see getUserdata()
|
|
|
|
*/
|
|
|
|
function extSave($id, $cachedata, $userdata, $expires = 0, $group = "default") {
|
|
|
|
if ($this->no_cache)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return $this->container->save($id, $cachedata, $expires, $group, $userdata);
|
|
|
|
} // end func extSave
|
|
|
|
|
|
|
|
|
2001-03-02 00:32:28 +08:00
|
|
|
/**
|
|
|
|
* Loads the given ID from the cache.
|
|
|
|
*
|
|
|
|
* @param string dataset ID
|
2001-03-04 03:21:49 +08:00
|
|
|
* @param string cache group
|
2001-03-02 00:32:28 +08:00
|
|
|
* @return mixed cached data or NULL on failure
|
|
|
|
* @access public
|
|
|
|
*/
|
2001-03-04 03:21:49 +08:00
|
|
|
function load($id, $group = "default") {
|
2001-03-02 00:32:28 +08:00
|
|
|
if ($this->no_cache)
|
|
|
|
return "";
|
|
|
|
|
2001-03-04 03:21:49 +08:00
|
|
|
return $this->container->load($id, $group);
|
2001-03-02 00:32:28 +08:00
|
|
|
} // end func load
|
|
|
|
|
2001-03-04 03:21:49 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the userdata field of a cached data set.
|
|
|
|
*
|
|
|
|
* @param string dataset ID
|
|
|
|
* @param string cache group
|
|
|
|
* @return string userdata
|
|
|
|
* @access public
|
|
|
|
* @see extSave()
|
|
|
|
*/
|
|
|
|
function getUserdata($id, $group = "default") {
|
|
|
|
if ($this->no_cache)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
return $this->container->getUserdata($id, $group);
|
|
|
|
} // end func getUserdata
|
|
|
|
|
|
|
|
|
2001-03-02 00:32:28 +08:00
|
|
|
/**
|
|
|
|
* Removes the specified dataset from the cache.
|
|
|
|
*
|
|
|
|
* @param string dataset ID
|
2001-03-04 03:21:49 +08:00
|
|
|
* @param string cache group
|
2001-03-02 00:32:28 +08:00
|
|
|
* @return boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
2001-03-04 03:21:49 +08:00
|
|
|
function delete($id, $group = "default") {
|
2001-03-02 00:32:28 +08:00
|
|
|
if ($this->no_cache)
|
|
|
|
return true;
|
|
|
|
|
2001-03-04 03:21:49 +08:00
|
|
|
return $this->container->delete($id, $group);
|
2001-03-02 00:32:28 +08:00
|
|
|
} // end func delete
|
|
|
|
|
2001-03-04 03:21:49 +08:00
|
|
|
|
2001-03-02 00:32:28 +08:00
|
|
|
/**
|
|
|
|
* Flushes the cache - removes all data from it
|
|
|
|
*
|
2001-03-04 03:21:49 +08:00
|
|
|
* @param string cache group, if empty all groups will be flashed
|
2001-03-02 00:32:28 +08:00
|
|
|
* @return integer number of removed datasets
|
|
|
|
*/
|
2001-03-04 03:21:49 +08:00
|
|
|
function flush($group = "") {
|
2001-03-02 00:32:28 +08:00
|
|
|
if ($this->no_cache)
|
|
|
|
return true;
|
|
|
|
|
2001-03-04 03:21:49 +08:00
|
|
|
return $this->container->flush($group);
|
2001-03-02 00:32:28 +08:00
|
|
|
} // end func flush
|
|
|
|
|
2001-03-04 03:21:49 +08:00
|
|
|
|
2001-03-02 00:32:28 +08:00
|
|
|
/**
|
|
|
|
* Checks if a dataset exists.
|
|
|
|
*
|
|
|
|
* Note: this does not say that the cached data is not expired!
|
|
|
|
*
|
|
|
|
* @param string dataset ID
|
2001-03-04 03:21:49 +08:00
|
|
|
* @param string cache group
|
2001-03-02 00:32:28 +08:00
|
|
|
* @return boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
2001-03-04 03:21:49 +08:00
|
|
|
function isCached($id, $group = "default") {
|
2001-03-02 00:32:28 +08:00
|
|
|
if ($this->no_cache)
|
|
|
|
return false;
|
|
|
|
|
2001-03-04 03:21:49 +08:00
|
|
|
return $this->container->isCached($id, $group);
|
2001-03-02 00:32:28 +08:00
|
|
|
} // end func isCached
|
|
|
|
|
2001-03-04 03:21:49 +08:00
|
|
|
|
2001-03-02 00:32:28 +08:00
|
|
|
/**
|
|
|
|
* Checks if a dataset is expired
|
|
|
|
*
|
|
|
|
* @param string dataset ID
|
2001-03-04 03:21:49 +08:00
|
|
|
* @param string cache group
|
2001-03-02 00:32:28 +08:00
|
|
|
* @param integer maximum age for the cached data in seconds - 0 for endless
|
|
|
|
* If the cached data is older but the given lifetime it will
|
|
|
|
* be removed from the cache. You don't have to provide this
|
|
|
|
* argument if you call isExpired(). Every dataset knows
|
|
|
|
* it's expire date and will be removed automatically. Use
|
|
|
|
* this only if you know what you're doing...
|
|
|
|
* @return boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
2001-03-04 03:21:49 +08:00
|
|
|
function isExpired($id, $group = "default", $max_age = 0) {
|
2001-03-02 00:32:28 +08:00
|
|
|
if ($this->no_cache)
|
|
|
|
return true;
|
|
|
|
|
2001-03-04 03:21:49 +08:00
|
|
|
return $this->container->isExpired($id, $group, $max_age);
|
2001-03-02 00:32:28 +08:00
|
|
|
} // end func isExpired
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a "unique" ID for the given value
|
|
|
|
*
|
|
|
|
* This is a quick but dirty hack to get a "unique" ID for a any kind of variable.
|
|
|
|
* ID clashes might occur from time to time although they are extreme unlikely!
|
|
|
|
*
|
|
|
|
* @param mixed variable to generate a ID for
|
|
|
|
* @return string "unique" ID
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function generateID($variable) {
|
|
|
|
// WARNING: ID clashes are possible although unlikely
|
|
|
|
return md5(serialize($variable));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls the garbage collector of the storage object with a certain probability
|
|
|
|
*
|
|
|
|
* @param boolean Force a garbage collection run?
|
2001-03-04 03:21:49 +08:00
|
|
|
* @see $gc_probability, $gc_time
|
2001-03-02 00:32:28 +08:00
|
|
|
*/
|
|
|
|
function garbageCollection($force = false) {
|
|
|
|
static $last_run = 0;
|
|
|
|
|
|
|
|
if ($this->no_cache)
|
|
|
|
return;
|
|
|
|
|
|
|
|
srand((double) microtime() * 1000000);
|
|
|
|
|
|
|
|
// time and probability based
|
|
|
|
if (($force) || ($last_run && $last_run < time() + $this->gc_time) || (rand(1, 100) < $this->gc_probability)) {
|
|
|
|
$this->container->garbageCollection();
|
|
|
|
$last_run = time();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end func garbageCollection
|
|
|
|
|
|
|
|
|
|
|
|
} // end class cache
|
|
|
|
?>
|