home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / php / pear / Cache / Function.php < prev    next >
PHP Script  |  2001-08-11  |  5KB  |  160 lines

  1. <?php
  2. /**
  3. * Function_Cache
  4. *
  5. * Purpose:
  6. *
  7. *   Caching the result and output of functions.
  8. *
  9. * Example:
  10. *
  11. *   require_once 'Cache/Function.php';
  12. *
  13. *   class foo {
  14. *     function bar($test) {
  15. *       echo "foo::bar($test)<br>";
  16. *     }
  17. *   }
  18. *
  19. *   class bar {
  20. *     function foobar($object) {
  21. *       echo '$'.$object.'->foobar('.$object.')<br>';
  22. *     }
  23. *   }
  24. *
  25. *   $bar = new bar;
  26. *
  27. *   function foobar() {
  28. *     echo 'foobar()';
  29. *   }
  30. *
  31. *   $cache = new Cache_Function();
  32. *
  33. *   $cache->call('foo::bar', 'test');
  34. *   $cache->call('bar->foobar', 'bar');
  35. *   $cache->call('foobar');
  36. *
  37. * Note:
  38. *   You cannot cache every function. You should only cache 
  39. *   functions that only depend on their arguments and don't use
  40. *   global or static variables, don't rely on database queries or 
  41. *   files, and so on.
  42. * @author       Sebastian Bergmann <sb@sebastian-bergmann.de>
  43. * @module       Function_Cache
  44. * @modulegroup  Function_Cache
  45. * @package      Cache
  46. * @version      $Revision: 1.11 $
  47. * @access       public
  48. */
  49.  
  50. // +----------------------------------------------------------------------+
  51. // | PHP version 4.0                                                      |
  52. // +----------------------------------------------------------------------+
  53. // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
  54. // +----------------------------------------------------------------------+
  55. // | This source file is subject to version 2.0 of the PHP license,       |
  56. // | that is bundled with this package in the file LICENSE, and is        |
  57. // | available at through the world-wide-web at                           |
  58. // | http://www.php.net/license/2_02.txt.                                 |
  59. // | If you did not receive a copy of the PHP license and are unable to   |
  60. // | obtain it through the world-wide-web, please send a note to          |
  61. // | license@php.net so we can mail you a copy immediately.               |
  62. // +----------------------------------------------------------------------+
  63. // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de>               |
  64. // +----------------------------------------------------------------------+
  65. //
  66. // $Id: Function.php,v 1.11 2001/08/11 17:04:17 sebastian Exp $
  67.  
  68. require_once 'Cache.php';
  69.  
  70. class Cache_Function extends Cache {
  71.     var $expires;
  72.  
  73.     /**
  74.     * Constructor
  75.     *
  76.     * @param    string  Name of container class
  77.     * @param    array   Array with container class options
  78.     * @param    integer Number of seconds for which to cache
  79.     */
  80.     function Cache_Function($container  = 'file',
  81.                             $container_options = array('cache_dir'       => '.',
  82.                                                        'filename_prefix' => 'cache_'
  83.                                                       ),
  84.                             $expires = 3600
  85.                            )
  86.     {
  87.       $this->Cache($container, $container_options);
  88.       $this->expires = $expires;      
  89.     }
  90.  
  91.     /**
  92.     * PEAR-Deconstructor
  93.     * Call deconstructor of parent
  94.     */
  95.     function _Cache_Function() {
  96.         $this->_Cache();
  97.     }
  98.  
  99.     /**
  100.     * Calls a cacheable function or method.
  101.     *
  102.     * @return mixed $result
  103.     * @access public
  104.     */
  105.     function call() {
  106.         // get arguments
  107.         $arguments = func_get_args();
  108.  
  109.         // generate cache id
  110.         $id = md5(serialize($arguments));
  111.  
  112.         // query cache
  113.         $cached_object = $this->get($id, 'function_cache');
  114.  
  115.         if ($cached_object != NULL) {
  116.             // cache hit: return cached output and result
  117.  
  118.             $output = $cached_object[0];
  119.             $result = $cached_object[1];
  120.  
  121.         } else {
  122.             // cache miss: call function, store output and result in cache
  123.  
  124.             ob_start();
  125.             $target = array_shift($arguments);
  126.  
  127.             // classname::staticMethod
  128.             if (strstr($target, '::')) {
  129.                 list($class, $method) = explode('::', $target);
  130.  
  131.                 $result = call_user_method_array($method, $class, $arguments);
  132.             }
  133.  
  134.             // object->method
  135.             elseif (strstr($target, '->')) {
  136.                 list($object, $method) = explode('->', $target);
  137.                 global $$object;
  138.  
  139.                 $result = call_user_method_array($method, $$object, $arguments);
  140.             }
  141.  
  142.             // function
  143.             else {
  144.                 $result = call_user_func_array($target, $arguments);
  145.             }
  146.  
  147.             $output = ob_get_contents();
  148.             ob_end_clean();
  149.  
  150.             $this->save($id, array($output, $result), $this->expires, 'function_cache');
  151.         }
  152.  
  153.         echo $output;
  154.         return $result;
  155.     }
  156. }
  157. ?>
  158.