Re: Function caching...
| From: | Ulf Wendel | Date: | Fri, 08 Dec 2000 21:20:19 +0000 |
| Subject: | Re: Function caching... | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-40607@lists.php.net to get a copy of this message | ||
Zeev Suraski wrote:
>
> I'm not exactly sure what you meant in the 'Cachable function' term. What
> would be cached? The status of the local variables? The output? If it's
> one of those two, then both of them are fairly easy to implement today
> (even though we can always think of ways to improve it, if you think it's
> necessary). If it's something else - please clarify.
It would be nice to be able to cache the output and return value of a
function. The goal is to cache calls to functions that have a long
runtime. If a cached function gets called a second time with the same
arguments the return value and output gets taken from the cache. The
function will not be executed again.
We do not want to cache functions with static variables or those that
depend on global resources ($GLOBALS, DB, Files...), of course a cache
could not manage these things.
> At 17:01 7/12/2000, Kristian Köhntopp wrote:
>
> >Based on discussions with Ulf Wendel and
> >Sebastian Bergmann, I came up with the attached
> >code to gather function results and output.
> >
> >This should actually be part of the language, as
> >sessions are, and it should be made to work with
> >objects as well.
> >
> >A PHP program should be able to mark a function as
> >cacheable
> >
> >cacheable function blah($arg1, $arg2) {
> > output_something();
> > return generate_result();
> >}
> >
> >and the result and output of the function should be
> >cached, keyed by the serialization of the args. If
Maybe an example clearyfies what is wanted. If you implement the
function call cache on the user side it's something like this.
function cache_call_func() {
// get arguments
$arguments = func_get_args();
// check, if function call is cached
if (isset($GLOBALS[ "cache" ][ $hash ])) {
print $GLOBALS[ "cache"][ $hash ][ "output" ];
return $GLOBALS[ "cache"][ $hash ][ "return" ];
}
// calculate hash for function call
$hash = md5( serialize( $arguments ) );
...
// start output buffering
ob_start();
$cache[ $hash ][ "return" ] = call_user_func_array( $func_name,
$arguments );
// store function's output in cache
$cache[ $hash ][ "output" ] = ob_get_contents();
// end output buffering
ob_end_clean();
}
Kris preferrs a slightly different code as in the example. He likes to
save the cached return values and the output to disk. This makes the
cache persistent between multiple invokations of a script. The API
should be very much like the session API where the user can override the
default handling (storage) methods.
Ulf