cvs: php4 /pear/Cache Function.php

From: Date: Fri, 10 Aug 2001 10:36:23 +0000
Subject: cvs: php4 /pear/Cache Function.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-458@lists.php.net to get a copy of this message
sebastian Fri Aug 10 06:36:23 2001 EDT Modified files: /php4/pear/Cache Function.php Log: Complete rewrite. Index: php4/pear/Cache/Function.php diff -u php4/pear/Cache/Function.php:1.7 php4/pear/Cache/Function.php:1.8 --- php4/pear/Cache/Function.php:1.7 Tue Jul 3 11:17:03 2001 +++ php4/pear/Cache/Function.php Fri Aug 10 06:36:23 2001 @@ -1,29 +1,39 @@ <?php /** * Function_Cache -* +* * Purpose: -* +* * Caching the result and output of functions. -* +* * Example: -* -* require_once 'Cache/Function.php'; * -* $FUNCTION_CACHE_CONTAINER = 'file'; -* $FUNCTION_CACHE_CONTAINER_OPTIONS = array( -* 'cache_dir' => '/tmp/', -* 'filename_prefix' => 'cache_'); -* -* function foo($string) -* { -* print $string . '<br>'; -* for($i=1;$i<10000000;$i++){} -* return strrev($string); +* require_once 'Cache/Function.php'; +* +* class foo { +* function bar($test) { +* echo "foo::bar($test)<br>"; * } -* -* print cached_function_call('foo', 'test'); -* +* } +* +* class bar { +* function foobar($object) { +* echo '$'.$object.'->foobar('.$object.')<br>'; +* } +* } +* +* $bar = new bar; +* +* function foobar() { +* echo 'foobar()'; +* } +* +* $cache = new Cache_Function(); +* +* $cache->call('foo::bar', 'test'); +* $cache->call('bar->foobar', 'bar'); +* $cache->call('foobar'); +* * Note: * * You cannot cache every function. You should only cache @@ -35,7 +45,7 @@ * @module Function_Cache * @modulegroup Function_Cache * @package Cache -* @version $Revision: 1.7 $ +* @version $Revision: 1.8 $ * @access public */ @@ -55,54 +65,94 @@ // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: Function.php,v 1.7 2001/07/03 15:17:03 sbergmann Exp $ +// $Id: Function.php,v 1.8 2001/08/10 10:36:23 sebastian Exp $ require_once 'Cache.php'; -/** -* Calls a cacheable function or method. -* -* @return mixed $result -* @access public -*/ -function cached_function_call() { - global $FUNCTION_CACHE_CONTAINER, $FUNCTION_CACHE_CONTAINER_OPTIONS; - static $cache; - - // create Cache object, if needed - if (!is_object($cache)) { - $cache = new Cache($FUNCTION_CACHE_CONTAINER, $FUNCTION_CACHE_CONTAINER_OPTIONS); - } +class Cache_Function extends Cache { + var $expires; - // get arguments - $arguments = func_get_args(); - - // generate cache id - $id = md5(serialize($arguments)); - - // query cache - $cached_object = $cache->get($id); - - if ($cached_object != NULL) { - // cache hit: return cached output and result + /** + * Constructor + * + * @param string Name of storage container class + * @param array Array with storage class dependend config options + */ + function Cache_Function($storage_driver = 'file', + $storage_options = array('cache_dir' => '.', + 'filename_prefix' => 'cache_' + ), + $expires = 3600 + ) + { + $this->Cache($storage_driver, $storage_options); + $this->expires = $expires; + } - $output = $cached_object[0]; - $result = $cached_object[1]; + /** + * PEAR-Deconstructor + * Call deconstructor of parent + */ + function _Cache_Function() { + $this->_Cache(); + } - } else { - // cache miss: call function, store output and result in cache + /** + * Calls a cacheable function or method. + * + * @return mixed $result + * @access public + */ + function call() { + // get arguments + $arguments = func_get_args(); + + // generate cache id + $id = md5(serialize($arguments)); + + // query cache + $cached_object = $this->get($id, 'function_cache'); + + if ($cached_object != NULL) { + // cache hit: return cached output and result + + $output = $cached_object[0]; + $result = $cached_object[1]; + + } else { + // cache miss: call function, store output and result in cache + + ob_start(); + $target = array_shift($arguments); + + // classname::staticMethod + if (strstr($target, '::')) { + list($class, $method) = explode('::', $target); + + $result = call_user_method_array($method, $class, $arguments); + } + + // object->method + elseif (strstr($target, '->')) { + list($object, $method) = explode('->', $target); + global $$object; + + $result = call_user_method_array($method, $$object, $arguments); + } + + // function + else { + $result = call_user_func_array($target, $arguments); + } - $function_name = array_shift($arguments); + $output = ob_get_contents(); + ob_end_clean(); - ob_start(); - $result = call_user_func_array($function_name, $arguments); - $output = ob_get_contents(); - ob_end_clean(); + $this->save($id, array($output, $result), $this->expire, 'function_cache'); + } - $cache->save($id, array($output, $result)); + echo $output; + return $result; } - - print $output; - return $result; } -?> \ No newline at end of file +?>

« previous php.pear.cvs (#458) next »