2 // +----------------------------------------------------------------------+
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 // +----------------------------------------------------------------------+
20 // $Id: Output.php 178289 2005-01-26 09:47:28Z dufuz $
22 require_once 'Cache.php';
25 * Class to cache the output of a script using the output buffering functions
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.
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).
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/');
42 * // get a cache object
43 * $cache = new Cache_Output(CACHE_STORAGE_CLASS, array('cache_dir' => CACHE_DIR));
45 * // compute the unique handle.
46 * // if your script depends on Cookie and HTTP Post data as well
48 * // $cache_handle = array(
49 * // 'file' => $REQUEST_URI,
50 * // 'post' => $HTTP_POST_VARS,
51 * // 'cookie' => $HTTP_COOKIE_VARS
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);
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)) {
62 * print '<p>Cache hit</p>';
66 * // time consumption script goes here.
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>";
72 * If you do not want to cache a whole page - no problem:
74 * if (!($content = $cache->start($cache_handle))) {
75 * // do the computation here
81 * If you need an example script check the (auto_)prepend and (auto_)append
82 * files of my homepage:
84 * http://www.ulf-wendel.de/php/show_source.php?file=prepend
85 * http://www.ulf-wendel.de/php/show_source.php?file=append
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 ;).
92 * @authors Ulf Wendel <ulf.wendel@phpdoc.de>
97 class Cache_Output extends Cache
101 * ID passed to start()
104 * @see start(), end()
109 * Group passed to start()
112 * @see start(), end()
114 var $output_group = '';
118 * Call deconstructor of parent
120 function _Cache_Output()
126 * starts the output buffering and returns an empty string or returns the cached output from the cache.
128 * @param string dataset ID
129 * @param string cache group
133 function start($id, $group = 'default')
135 if (!$this->caching) {
138 // this is already cached return it from the cache so that the user
139 // can use the cache content and stop script execution
140 if ($content = $this->get($id, $group)) {
143 // remember some data to be able to fill the cache on calling end()
144 $this->output_id = $id;
145 $this->output_group = $group;
147 // WARNING: we need the output buffer - possible clashes
149 ob_implicit_flush(false);
155 * Stores the content of the output buffer into the cache and returns the content.
157 * @param mixed lifetime of the cached data in seconds - 0 for endless. More formats available. see Container::getExpiresAbsolute()
158 * @param string additional userdefined data
159 * @return string cached output
161 * @see endPrint(), endGet(), Container::getExpiresAbsolute()
163 function end($expire = 0, $userdata = '')
165 $content = ob_get_contents();
168 // store in the cache
169 if ($this->caching) {
170 $this->container->save($this->output_id, $content, $expire, $this->output_group, $userdata);
176 * Stores the content of the output buffer into the cache and prints the content.
180 function endPrint($expire = 0, $userdata = '')
182 $this->printContent($this->end($expire, $userdata));
183 } // end func endPrint
186 * Sends the data to the user.
187 * This is for compatibility with OutputCompression
192 function printContent($content = '')
194 if ($content == '') {
195 $content = &$this->container->cachedata;
200 * Returns the content of the output buffer but does not store it into the cache.
202 * Use this method if the content of your script is markup (XML)
203 * that has to be parsed/converted (XSLT) before you can output
204 * and store it into the cache using save().
208 * @see endPrint(), end()
212 $content = ob_get_contents();
215 $this->output_id = '';
216 $this->output_group = '';
220 } // end class output