Re: Function caching...
| From: | Zeev Suraski | Date: | Fri, 08 Dec 2000 19:22:35 +0000 |
| Subject: | Re: Function caching... | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-40585@lists.php.net to get a copy of this message | ||
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.
Thanks,
Zeev
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 you oppose adding keywords to the language, you should be able to function blah($arg1, $arg2) { output_something(); return generate_result(); } cache_register("blah"); This is actually preferred to adding a keyword, because you could add an additional cache identifier (default: main) cache_register("blah", "blah_module"); In both cases, all calls to blah($x, $y) should be transparently cached. The cache mechanism should have an API similar to the session API which allows to provide user callbacks to actually create the cache in shared memory, in files, or in any other persistent store. That would be a string get_res_cache(string hash, string identifier); boolean set_res_cache(string hash, string value, string identifer); string get_out_cache(string hash, string identifier); boolean set_out_cache(string hash, string value, string identifer); Also, there should be boolean invalidate_cache(string identifier); to selectively invalidate the cache identified by identifier. What do you think about the proposal? Kristian PS: The attached example is deliberately uncacheable. Cacheable-- Zeev Suraski <zeev@zend.com> CTO, Zend Technologies Ltd. http://www.zend.com/functions are not using global nor static and call onlyidempotent functions themselves. -- Kristian Köhntopp, NetUSE AG Siemenswall, D-24107 Kiel Tel: +49 431 386 436 00, Fax: +49 431 386 435 99 Using PHP3? See our web development library at http://phplib.netuse.de/<?php define(CACHEDIR, "/home/www/servers/kris.dev.netuse.de/cache"); function bufferme() {$args = func_get_args();?> <table border="1"> <tr> <td>Bla</td> <td><?php echo date("d-M-Y H:i:s") ?></td> </tr> <tr> <td>Bla</td> <td><?php echo serialize($args) ?></td> </tr> </table> <?php } function buffered_call() {if (!is_dir(CACHEDIR)) mkdir(CACHEDIR, 0755);$args = func_get_args(); $hash = CACHEDIR . "/" . md5(serialize($args));if (!file_exists("$hash.res")) {ob_start(); $result = call_user_func_array( array_shift( $args ), $args ); $output = ob_get_contents(); $fp = fopen("$hash.res", "w"); fputs($fp, serialize($result)); fclose($fp);$fp = fopen("$hash.out", "w"); fputs($fp, serialize($output)); fclose($fp);ob_end_clean();echo $output; return $result;} else {echo unserialize(join(file("$hash.out"), "")); return unserialize(join(file("$hash.res"), ""));}} buffered_call("bufferme", 3, 2, 3); ?> -- PHP Development Mailing List <http://www.php.net/> To unsubscribe, e-mail: php-dev-unsubscribe@lists.php.net For additional commands, e-mail: php-dev-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net