From: Christian Stocker Date: Sat, 17 Mar 2001 16:06:31 +0000 (+0000) Subject: GarbageCollection was moved into a PEAR-Deconstructor X-Git-Tag: php-4.0.6RC1~664 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fcdd641539289cae380ed6d789957bc6bd18d986;p=php GarbageCollection was moved into a PEAR-Deconstructor --- diff --git a/pear/Cache.php b/pear/Cache.php index 90d5dff0eb..7b7556373d 100644 --- a/pear/Cache.php +++ b/pear/Cache.php @@ -25,12 +25,16 @@ require_once "Cache/Error.php"; * * TODO: Simple usage example goes here. * +* WARNING: No File/DB-Table-Row locking is implemented yet, +* it's possible, that you get corrupted data-entries under +* bad circumstances (especially with the file container) +* * @author Ulf Wendel * @version $Id$ * @package Cache -* @access public +* @access public */ -class Cache { +class Cache extends PEAR { /** * Disables the caching. @@ -45,8 +49,8 @@ class Cache { /** * 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 + * If set to a value above 0 a garbage collection will + * flush all cache entries older than the specified number * of seconds. * * @var integer @@ -68,35 +72,40 @@ class Cache { /** * 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 = "") { + $this->PEAR(); $storage_driver = strtolower($storage_driver); $storage_class = 'Cache_Container_' . $storage_driver; $storage_classfile = 'Cache/Container/' . $storage_driver . '.php'; include_once $storage_classfile; $this->container = new $storage_class($storage_options); + } + + //deconstructor + function _Cache() + { $this->garbageCollection(); - } /** * Returns the requested dataset it if exists and is not expired - * + * * @param string dataset ID * @param string cache group * @return mixed cached data or NULL on failure @@ -105,16 +114,16 @@ class Cache { function get($id, $group = "default") { if ($this->no_cache) return ""; - + if ($this->isCached($id, $group) && !$this->isExpired($id, $group)) return $this->load($id, $group); - - return NULL; + + return NULL; } // end func get /** * 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 @@ -125,13 +134,13 @@ class Cache { function save($id, $data, $expires = 0, $group = "default") { if ($this->no_cache) return true; - + return $this->container->save($id, $data, $expires, $group, ""); } // end func save /** * Stores a dataset without additional userdefined data. - * + * * @param string dataset ID * @param mixed data to store * @param string additional userdefined data @@ -151,16 +160,16 @@ class Cache { /** * Loads the given ID from the cache. - * + * * @param string dataset ID * @param string cache group - * @return mixed cached data or NULL on failure + * @return mixed cached data or NULL on failure * @access public */ function load($id, $group = "default") { if ($this->no_cache) return ""; - + return $this->container->load($id, $group); } // end func load @@ -176,13 +185,13 @@ class Cache { function getUserdata($id, $group = "default") { if ($this->no_cache) return ""; - + return $this->container->getUserdata($id, $group); } // end func getUserdata /** * Removes the specified dataset from the cache. - * + * * @param string dataset ID * @param string cache group * @return boolean @@ -191,28 +200,28 @@ class Cache { function delete($id, $group = "default") { if ($this->no_cache) return true; - + return $this->container->delete($id, $group); } // end func delete /** * Flushes the cache - removes all data from it - * + * * @param string cache group, if empty all groups will be flashed * @return integer number of removed datasets */ function flush($group = "") { if ($this->no_cache) return true; - + return $this->container->flush($group); } // end func flush /** * Checks if a dataset exists. - * + * * Note: this does not say that the cached data is not expired! - * + * * @param string dataset ID * @param string cache group * @return boolean @@ -221,20 +230,20 @@ class Cache { function isCached($id, $group = "default") { if ($this->no_cache) return false; - + return $this->container->isCached($id, $group); } // end func isCached /** * Checks if a dataset is expired - * + * * @param string dataset ID * @param string cache group * @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 + * 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 @@ -242,13 +251,13 @@ class Cache { function isExpired($id, $group = "default", $max_age = 0) { if ($this->no_cache) return true; - + return $this->container->isExpired($id, $group, $max_age); } // 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! * @@ -263,24 +272,24 @@ class Cache { /** * Calls the garbage collector of the storage object with a certain probability - * + * * @param boolean Force a garbage collection run? * @see $gc_probability, $gc_time */ 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 + +} // end class cache ?>