cvs: pear /Cache_Lite Cache_Lite_Function.php
| From: | Fabien MARTY | Date: | Sat, 10 Aug 2002 21:08:44 +0000 |
| Subject: | cvs: pear /Cache_Lite Cache_Lite_Function.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-5666@lists.php.net to get a copy of this message | ||
fab Sat Aug 10 17:08:44 2002 EDT
Added files:
/pear/Cache_Lite Cache_Lite_Function.php
Log:
Add of Cache_Lite_Function (to cache the result/output of function/method) (simple rewriting of
Sebastian Bergmann Cache/Function)
Index: pear/Cache_Lite/Cache_Lite_Function.php
+++ pear/Cache_Lite/Cache_Lite_Function.php
<?php
/**
* This class extends Cache_Lite and can be used to cache the result and output of functions/methods
*
* This class is completly inspired from Sebastian Bergmann's
* PEAR/Cache_Function class. This is only an adaptation to
* Cache_Lite
*
* There are some examples in the 'docs/examples' file
* Technical choices are described in the 'docs/technical' file
*
* @package Cache_Lite
* @version $Id: Cache_Lite_Function.php,v 1.1 2002/08/10 21:08:44 fab Exp $
* @author Sebastian BERGMANN <sb@sebastian-bergmann.de>
* @author Fabien MARTY <fab@php.net>
*/
require_once('Cache_Lite/Cache_Lite.php');
class Cache_Lite_Function extends Cache_Lite
{
// --- Private properties ---
/**
* Default cache group for function caching
*
* @var string $_defaultGroup
*/
var $_defaultGroup = 'Cache_Lite_Function';
// --- Public methods ----
/**
* Constructor
*
* $options is an assoc. To have a look at availables options,
* see the constructor of the Cache_Lite class in 'Cache_Lite.php'
*
* Comparing to Cache_Lite constructor, there is another option :
* $options = array(
* (...) see Cache_Lite constructor
* 'defaultGroup' => default cache group for function caching (string)
* );
*
* @param array $options options
* @access public
*/
function Cache_Lite_Function($options)
{
if (isset($options['defaultGroup'])) {
$this->_defaultGroup = $options['defaultGroup'];
}
$this->Cache_Lite($options);
}
/**
* Calls a cacheable function or method (or not if there is already a cache for it)
*
* Arguments of this method are read with func_get_args. So it doesn't appear
* in the function definition. Synopsis :
* call('functionName', $arg1, $arg2, ...)
* (arg1, arg2... are arguments of 'functionName')
*
* @return mixed result of the function/method
* @access public
*/
function call()
{
$arguments = func_get_args();
$id = md5(serialize($arguments)); // Generate a cache id
if ($data = $this->get($id, $this->_defaultGroup)) {
$array = unserialize($data);
$output = $array['output'];
$result = $array['result'];
} else {
ob_start();
ob_implicit_flush(false);
$target = array_shift($arguments);
if (strstr($target, '::')) { // classname::staticMethod
list($class, $method) = explode('::', $target);
$result = call_user_method_array($method, $class, $arguments);
} else if (strstr($target, '->')) { // object->method
list($object_123456789, $method) = explode('->', $target);
global $$object_123456789;
$result = call_user_method_array($method, $$object_123456789, $arguments);
} else { // function
$result = call_user_func_array($target, $arguments);
}
$output = ob_get_contents();
ob_end_clean();
$array['output'] = $output;
$array['result'] = $result;
$this->save(serialize($array), $id, $this->_defaultGroup);
}
echo($output);
return $result;
}
}
?>