◆ Function Cache Output Pear Cache
In addition to cache the output content, PEAR can also cache the call results of a function. This is a very interesting feature. If your program needs to use a function frequently, and if the result of the call is the same, I suggest you try, especially when this function runs slower.
Below we implements a buffer call to a Slow function SlowFunction () that is slow.
php require_once 'cache / function.php';
$ cachedir = './pear_cache/'; $ cache = new cache_function ('file', array ('cache_dir' => $ cachedir)); $ arr = array ('Apple', 'pear', 'watermelon'); $ Cache-> Call ('SlowFunction', $ Arr); Echo '
'; $ arr = array ('Apple', 'Pear', 'Watermelon'); SlowFunction ($ Arr); Function SlowFunction ($ Arr = NULL) {echo "A function that is slower: (
"; echo "Current time is". Date ('MDY H: I: S A', TIME ()). '
'; Foreach ($ Arr As $ Fruit) {echo "I ate a $ fruit
";})
?>
The following is a script execution result:
A function that is very slow: (Current time is jul-28-2004 17:15:57 PM I ate an apple I ate a pear I ate a watermelon an execution of a very slow function: (current time is Jul-28-2004 17:17:55 PM I ate an apple I ate a pear I ate a watermelon
In the code, the cache / function.php class is used to perform function buffering functions. $ cache variable is a cache_function object that uses a file-based function cache to store the $ cachedir directory. To cache a function, cache_function object $ CACHE's call () method is like this: $ cache-> call ('SlowFunction', $ ARR);
Here, the slowFunction () function is called, the parameter is an array $ ARR, which is cached in a file in the $ cacdir directory. Any call to this function will be returned by the result of $ cache-> call ().
Function caching and usage methods and content caches are very similar, no more, please check the PEAR manual for details.