Cache
[ class tree: Cache ] [ index: Cache ] [ all elements ]

Source for file Function.php

Documentation is available at Function.php

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

Documentation generated on Mon, 11 Mar 2019 15:44:49 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.