[ 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: Ulf Wendel <ulf.wendel@phpdoc.de> | 16 // | Christian Stocker <chregu@phant.ch> | 17 // | Vinai Kopp <kopp@netzarbeiter.de> | 18 // +----------------------------------------------------------------------+ 19 // 20 // $Id: Output.php,v 1.6 2003/01/04 11:54:45 mj Exp $ 21 22 require_once 'Cache.php'; 23 24 /** 25 * Class to cache the output of a script using the output buffering functions 26 * 27 * Simple output cache. Some pages require lots of time to compute. Caching the 28 * output can increase the overall speed dramatically, especially if you use 29 * a Shared Memory storage container. 30 * 31 * As you can see in the example the usage is extemely simple. To cache a script 32 * simple put some few lines of code in front of your script and some at the end. 33 * A preferrable place for this are the auto_prepend and auto_append files (=> php.ini). 34 * 35 * Usage example: 36 * 37 * // place this somewhere in a central config file 38 * define(CACHE_STORAGE_CLASS, 'file'); 39 * // file storage needs a dir to put the cache files 40 * define(CACHE_DIR, '/var/tmp/'); 41 * 42 * // get a cache object 43 * $cache = new Cache_Output(CACHE_STORAGE_CLASS, array('cache_dir' => CACHE_DIR)); 44 * 45 * // compute the unique handle. 46 * // if your script depends on Cookie and HTTP Post data as well 47 * // you should use: 48 * // $cache_handle = array( 49 * // 'file' => $REQUEST_URI, 50 * // 'post' => $HTTP_POST_VARS, 51 * // 'cookie' => $HTTP_COOKIE_VARS 52 * // ); 53 * // But be warned, using all GET or POST Variables as a seed 54 * // can be used for a DOS attack. Calling http://www.example.com/example.php?whatever 55 * // where whatever is a random text might be used to flood your cache. 56 * $cache_handle = $cache->generateID($REQUEST_URI); 57 * 58 * // now the magic happens: if cached call die() 59 * // to end the time consumptiong script script execution and use the cached value! 60 * if ($content = $cache->start($cache_handle)) { 61 * print $content; 62 * print '<p>Cache hit</p>'; 63 * die(); 64 * } 65 * 66 * // time consumption script goes here. 67 * 68 * // store the output of the cache into the cache and print the output. 69 * print $cache->end(); 70 * print "<p>Cache miss, stored using the ID '$id'.</p>"; 71 * 72 * If you do not want to cache a whole page - no problem: 73 * 74 * if (!($content = $cache->start($cache_handle))) { 75 * // do the computation here 76 * print $cache->end() 77 * } else { 78 * print $content; 79 * } 80 * 81 * If you need an example script check the (auto_)prepend and (auto_)append 82 * files of my homepage: 83 * 84 * http://www.ulf-wendel.de/php/show_source.php?file=prepend 85 * http://www.ulf-wendel.de/php/show_source.php?file=append 86 * 87 * Don't know how to use it or you need profiling informations?` 88 * Ask Christian he was patient with me and he'll be so with your questions ;). 89 * 90 * Have fun! 91 * 92 * @authors Ulf Wendel <ulf.wendel@phpdoc.de> 93 * @version $ID: $ 94 * @package Cache 95 * @access public 96 */ 97 class Cache_Output extends Cache { 98 99 /** 100 * ID passed to start() 101 * 102 * @var string 103 * @see start(), end() 104 */ 105 var $output_id = ''; 106 107 /** 108 * Group passed to start() 109 * 110 * @var string 111 * @see start(), end() 112 */ 113 var $output_group = ''; 114 115 /** 116 * PEAR-Deconstructor 117 * Call deconstructor of parent 118 */ 119 function _Cache_Output() 120 { 121 $this->_Cache(); 122 } 123 124 /** 125 * starts the output buffering and returns an empty string or returns the cached output from the cache. 126 * 127 * @param string dataset ID 128 * @param string cache group 129 * @return string 130 * @access public 131 */ 132 function start($id, $group = 'default') { 133 if (!$this->caching) 134 return ''; 135 136 // this is already cached return it from the cache so that the user 137 // can use the cache content and stop script execution 138 if ($content = $this->get($id, $group)) 139 return $content; 140 141 // remember some data to be able to fill the cache on calling end() 142 $this->output_id = $id; 143 $this->output_group = $group; 144 145 // WARNING: we need the output buffer - possible clashes 146 ob_start(); 147 ob_implicit_flush(false); 148 149 return ''; 150 } // end func start 151 152 /* 153 * Stores the content of the output buffer into the cache and returns the content. 154 * 155 * @param mixed lifetime of the cached data in seconds - 0 for endless. More formats available. see Container::getExpiresAbsolute() 156 * @param string additional userdefined data 157 * @return string cached output 158 * @access public 159 * @see endPrint(), endGet(), Container::getExpiresAbsolute() 160 */ 161 function end($expire = 0, $userdata = '') { 162 $content = ob_get_contents(); 163 ob_end_clean(); 164 165 // store in the cache 166 if ($this->caching) 167 $this->container->save($this->output_id, $content, $expire, $this->output_group, $userdata); 168 169 return $content; 170 } // end func end() 171 172 /** 173 * Stores the content of the output buffer into the cache and prints the content. 174 * 175 * @brother end() 176 */ 177 function endPrint($expire = 0, $userdata = '') { 178 $this->printContent($this->end($expire, $userdata)); 179 } // end func endPrint 180 181 /** 182 * Sends the data to the user. 183 * This is for compatibility with OutputCompression 184 * 185 * @param string 186 * @access public 187 */ 188 function printContent($content = '') { 189 if ('' == $content) 190 $content = &$this->container->cachedata; 191 192 print $content; 193 } 194 /** 195 * Returns the content of the output buffer but does not store it into the cache. 196 * 197 * Use this method if the content of your script is markup (XML) 198 * that has to be parsed/converted (XSLT) before you can output 199 * and store it into the cache using save(). 200 * 201 * @return string 202 * @access public 203 * @see endPrint(), end() 204 */ 205 function endGet() { 206 $content = ob_get_contents(); 207 ob_end_clean(); 208 209 $this->output_id = ''; 210 $this->output_group = ''; 211 212 return $content; 213 } // end func endGet 214 } // end class output 215 ?>
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 |