Home > Archive > PHP Language > August 2007 > detecting a function call
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
detecting a function call
|
|
|
| Asking for direction if possible.
What I would like to be able to do is detect when particular functions are
called, so as to be able to set up a results cache, and use that instead of
continual db calls and processing.
Other alternative I suppose, is to look at building in caching in each
function...
... in hope ;)
PhilM
| |
| ZeldorBlat 2007-08-10, 6:59 pm |
| On Aug 10, 6:52 am, "philm" <gate...@microsoft.com> wrote:
> Asking for direction if possible.
>
> What I would like to be able to do is detect when particular functions are
> called, so as to be able to set up a results cache, and use that instead of
> continual db calls and processing.
>
> Other alternative I suppose, is to look at building in caching in each
> function...
>
> .. in hope ;)
> PhilM
The second way is the way to do it. For instance:
funciton getFoo() {
//try to get foo from the cache
$foo = Cache::fetch('foo');
if($foo == false) {
$foo = ... //code to get foo from the database.
Cache::write('foo', $foo); //put foo in the cache
}
return $foo;
}
| |
|
| On Fri, 10 Aug 2007 12:52:59 +0200, philm <gates.w@microsoft.com> wrote:=
> Asking for direction if possible.
>
> What I would like to be able to do is detect when particular functions=
=
> are
> called, so as to be able to set up a results cache, and use that inste=
ad =
> of
> continual db calls and processing.
At runtime? Just using flags I suppose.. Might be worth your while to =
build an (singleton?) object out of several functions.
> Other alternative I suppose, is to look at building in caching in each=
> function...
A variation of:
function foo($param){
static $results =3D array();
if(isset($results[$param])) return $results[$param];
//..
//actual processing, captured in $return;
//..
$results[$param] =3D $return;
return $return;
}
Or possibly, a wrapper function (be very aware of unserializable argumen=
ts =
though):
function cache_results($function, $args =3D array()){
static $cache =3D array();
$checkargs =3D serialize($args);
if(isset($cache[$function][$checkargs]))
return =
$cache[$function][$checkargs];
$return =3D call_user_func_array($function,$args);
$cache[$function][$checkargs] =3D $return;
return $return;
}
-- =
Rik Wasmus
|
|
|
|
|