chregu Sat Mar 17 08:06:31 2001 EDT Modified files: /php4/pear Cache.php /php4/pear/Cache Output.php Log: GarbageCollection was moved into a PEAR-Deconstructor
Index: php4/pear/Cache.php diff -u php4/pear/Cache.php:1.7 php4/pear/Cache.php:1.8 --- php4/pear/Cache.php:1.7 Thu Mar 8 12:41:39 2001 +++ php4/pear/Cache.php Sat Mar 17 08:06:31 2001 @@ -16,7 +16,7 @@ // | Sebastian Bergmann <[EMAIL PROTECTED]> | // +----------------------------------------------------------------------+ // -// $Id: Cache.php,v 1.7 2001/03/08 20:41:39 uw Exp $ +// $Id: Cache.php,v 1.8 2001/03/17 16:06:31 chregu Exp $ require_once "Cache/Error.php"; @@ -25,12 +25,16 @@ * * 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 <[EMAIL PROTECTED]> -* @version $Id: Cache.php,v 1.7 2001/03/08 20:41:39 uw Exp $ +* @version $Id: Cache.php,v 1.8 2001/03/17 16:06:31 chregu Exp $ * @package Cache -* @access public +* @access public */ -class Cache { +class Cache extends PEAR { /** * Disables the caching. @@ -45,8 +49,8 @@ /** * 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 @@ /** * 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 @@ 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 @@ 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 @@ /** * 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ /** * 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 ?> Index: php4/pear/Cache/Output.php diff -u php4/pear/Cache/Output.php:1.14 php4/pear/Cache/Output.php:1.15 --- php4/pear/Cache/Output.php:1.14 Fri Mar 16 00:50:39 2001 +++ php4/pear/Cache/Output.php Sat Mar 17 08:06:31 2001 @@ -17,21 +17,21 @@ // | Vinai Kopp <[EMAIL PROTECTED]> | // +----------------------------------------------------------------------+ // -// $Id: Output.php,v 1.14 2001/03/16 08:50:39 eschmid Exp $ +// $Id: Output.php,v 1.15 2001/03/17 16:06:31 chregu Exp $ require_once 'Cache.php'; /** * Class to cache the output of a script using the output buffering functions * -* Simple output cache. Some pages require lots of time to compute. Caching the +* Simple output cache. Some pages require lots of time to compute. Caching the * output can increase the overall speed dramatically, especially if you use * a Shared Memory storage container. -* -* As you can see in the example the usage is extemely simple. To cache a script -* simple put some few lines of code in front of your script and some at the end. -* A preferrable place for this are the auto_prepend and auto_append files (=> php.ini). -* +* +* As you can see in the example the usage is extemely simple. To cache a script +* simple put some few lines of code in front of your script and some at the end. +* A preferrable place for this are the auto_prepend and auto_append files (=> +php.ini). +* * Usage example: * * // place this somewhere in a central config file @@ -43,9 +43,9 @@ * $cache = new Cache_Output(CACHE_STORAGE_CLASS, array("cache_dir" => CACHE_DIR)); * * // compute the unique handle. -* // if your script depends on Cookie and HTTP Post data as well +* // if your script depends on Cookie and HTTP Post data as well * // you should use: -* // $cache_handle = array( +* // $cache_handle = array( * // "file" => $REQUEST_URI, * // "post" => $HTTP_POST_VAS" * // "cookie" => $HTTP_COOKIE_VARS @@ -54,33 +54,33 @@ * // can be used for a DOS attack. Calling http://www.example.com/example.php?whatever * // where whatever is a random text might be used to flood your cache. * $cache_handle = $cache->generateID($REQUEST_URI); -* -* // now the magic happens: if cached call die() +* +* // now the magic happens: if cached call die() * // to end the time consumptiong script script execution and use the cached value! * if ($content = $cache->start($cache_handle)) { * print $content; * print "<p>Cache hit</p>"; * die(); * } -* -* // time consumption script goes here. -* +* +* // time consumption script goes here. +* * // store the output of the cache into the cache and print the output. * print $cache->end(); * print "<p>Cache miss, stored using the ID '$id'.</p>"; -* +* * If you do not want to cache a whole page - no problem: -* +* * if (!($content = $cache->start($cache_handle))) { * // do the computation here * print $cache->end() * } else { print $content; * } -* -* If you need an example script check the (auto_)prepend and (auto_)append +* +* If you need an example script check the (auto_)prepend and (auto_)append * files of my homepage: -* +* * http://www.ulf-wendel.de/php/show_source.php?file=prepend * http://www.ulf-wendel.de/php/show_source.php?file=append * @@ -88,7 +88,7 @@ * Ask Christian he was patient with me and he'll be so with your questions ;). * * Have fun! -* +* * @authors Ulf Wendel <[EMAIL PROTECTED]> * @version $ID: $ * @package Cache @@ -98,7 +98,7 @@ /** * ID passed to start() - * + * * @var string * @see start(), end() */ @@ -106,25 +106,34 @@ /** * Group passed to start() - * - * @var string + * + * @var string * @see start(), end() */ var $output_group = ""; /** + * PEAR-Deconstructor + * Call deconstructor of parent + */ + function _Cache_Output() + { + $this->_Cache(); + } + + /** * starts the output buffering and returns an empty string or returns the cached output from the cache. - * + * * @param string dataset ID * @param string cache group - * @return string + * @return string * @access public */ function start($id, $group = "default") { if ($this->no_cache) return ""; - // this is already cached return it from the cache so that the user + // this is already cached return it from the cache so that the user // can use the cache content and stop script execution if ($content = $this->get($id, $group)) return $content; @@ -166,14 +175,14 @@ * @brother end() */ function endPrint($expire = 0, $userdata = "") { - print $this->end($expire, $userdata); + print $this->end($expire, $userdata); } // end func endPrint /** * Returns the content of the output buffer but does not store it into the cache. * - * Use this method if the content of your script is markup (XML) - * that has to be parsed/converted (XSLT) before you can output + * Use this method if the content of your script is markup (XML) + * that has to be parsed/converted (XSLT) before you can output * and store it into the cache using save(). * * @return string
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]