[ Index ] |
|
Code source de PHP PEAR 1.4.5 |
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,v 1.6 2003/01/04 11:54:45 mj Exp $ 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 * 59 * You cannot cache every function. You should only cache 60 * functions that only depend on their arguments and don't use 61 * global or static variables, don't rely on database queries or 62 * files, and so on. 63 * 64 * @author Sebastian Bergmann <sb@sebastian-bergmann.de> 65 * @module Function_Cache 66 * @modulegroup Function_Cache 67 * @package Cache 68 * @version $Revision: 1.6 $ 69 * @access public 70 */ 71 class Cache_Function extends Cache { 72 var $expires; 73 74 /** 75 * Constructor 76 * 77 * @param string Name of container class 78 * @param array Array with container class options 79 * @param integer Number of seconds for which to cache 80 */ 81 function Cache_Function($container = 'file', 82 $container_options = array('cache_dir' => '.', 83 'filename_prefix' => 'cache_' 84 ), 85 $expires = 3600 86 ) 87 { 88 $this->Cache($container, $container_options); 89 $this->expires = $expires; 90 } 91 92 /** 93 * PEAR-Deconstructor 94 * Call deconstructor of parent 95 */ 96 function _Cache_Function() { 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 // get arguments 108 $arguments = func_get_args(); 109 110 // generate cache id 111 $id = md5(serialize($arguments)); 112 113 // query cache 114 $cached_object = $this->get($id, 'function_cache'); 115 116 if ($cached_object != NULL) { 117 // cache hit: return cached output and result 118 119 $output = $cached_object[0]; 120 $result = $cached_object[1]; 121 122 } else { 123 // cache miss: call function, store output and result in cache 124 125 ob_start(); 126 $target = array_shift($arguments); 127 128 // classname::staticMethod 129 if (strstr($target, '::')) { 130 list($class, $method) = explode('::', $target); 131 132 $result = call_user_func_array(array($class, $method), $arguments); 133 } 134 135 // object->method 136 elseif (strstr($target, '->')) { 137 list($object, $method) = explode('->', $target); 138 global $$object; 139 140 $result = call_user_func_array(array($$object, $method), $arguments); 141 } 142 143 // function 144 else { 145 $result = call_user_func_array($target, $arguments); 146 } 147 148 $output = ob_get_contents(); 149 ob_end_clean(); 150 151 $this->save($id, array($output, $result), $this->expires, 'function_cache'); 152 } 153 154 echo $output; 155 return $result; 156 } 157 } 158 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 14:08:00 2007 | par Balluche grâce à PHPXref 0.7 |