2 // +----------------------------------------------------------------------+
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 1997, 1998, 1999, 2000, 2001 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 // | Sebastian Bergmann <sb@sebastian-bergmann.de> |
17 // +----------------------------------------------------------------------+
20 require_once 'PEAR.php';
21 require_once 'Cache/Error.php';
24 * Cache is a base class for cache implementations.
26 * The pear cache module is a generic data cache which can be used to
27 * cache script runs. The idea behind the cache is quite simple. If you have
28 * the same input parameters for whatever tasks/algorithm you use you'll
29 * usually get the same output. So why not caching templates, functions calls,
30 * graphic generation etc. Caching certain actions e.g. XSLT tranformations
31 * saves you lots of time.
33 * The design of the cache reminds of PHPLibs session implementation. A
34 * (PHPLib: session) controller uses storage container (PHPLib: ct_*.inc) to save
35 * certain data (PHPLib: session data). In contrast to the session stuff it's up to
36 * you to generate an ID for the data to cache. If you're using the output cache
37 * you might use the script name as a seed for cache::generateID(), if your using the
38 * function cache you'd use an array with all function parameters.
40 * Usage example of the generic data cache:
42 * require_once('Cache.php');
44 * $cache = new Cache('file', array('cache_dir' => 'cache/') );
45 * $id = $cache->generateID('testentry');
47 * if ($data = $cache->get($id)) {
48 * print "Cache hit.<br>Data: $data";
51 * $data = 'data of any kind';
52 * $cache->save($id, $data);
53 * print 'Cache miss.<br>';
56 * WARNING: No File/DB-Table-Row locking is implemented yet,
57 * it's possible, that you get corrupted data-entries under
58 * bad circumstances (especially with the file container)
60 * @author Ulf Wendel <ulf.wendel@phpdoc.de>
65 class Cache extends PEAR {
68 * Enables / disables caching.
70 * TODO: Add explanation what this is good for.
78 * Garbage collection: probability in seconds
80 * If set to a value above 0 a garbage collection will
81 * flush all cache entries older than the specified number
85 * @see $gc_probability, $gc_maxlifetime
91 * Garbage collection: probability in percent
93 * TODO: Add an explanation.
95 * @var integer 0 => never
96 * @see $gc_time, $gc_maxlifetime
99 var $gc_probability = 1;
102 * Garbage collection: delete all entries not use for n seconds.
104 * Default is one day, 60 * 60 * 24 = 86400 seconds.
107 * @see $gc_probability, $gc_time
109 var $gc_maxlifetime = 86400;
112 * Storage container object.
114 * @var object Cache_Container
124 * @param string Name of container class
125 * @param array Array with container class options
127 function Cache($container, $container_options = '')
130 $container = strtolower($container);
131 $container_class = 'Cache_Container_' . $container;
132 $container_classfile = 'Cache/Container/' . $container . '.php';
134 include_once $container_classfile;
135 $this->container = new $container_class($container_options);
141 $this->garbageCollection();
145 * Returns the current caching state.
147 * @return boolean The current caching state.
150 function getCaching()
152 return ($this->caching);
156 * Enables or disables caching.
158 * @param boolean The new caching state.
161 function setCaching($state)
163 $this->caching = $state;
167 * Returns the requested dataset it if exists and is not expired
169 * @param string dataset ID
170 * @param string cache group
171 * @return mixed cached data or NULL on failure
174 function get($id, $group = 'default') {
178 if ($this->isCached($id, $group) && !$this->isExpired($id, $group))
179 return $this->load($id, $group);
185 * Stores the given data in the cache.
187 * @param string dataset ID used as cache identifier
188 * @param mixed data to cache
189 * @param integer lifetime of the cached data in seconds - 0 for endless
190 * @param string cache group
194 function save($id, $data, $expires = 0, $group = 'default') {
198 return $this->extSave($id, $data, '',$expires, $group);
202 * Stores a dataset without additional userdefined data.
204 * @param string dataset ID
205 * @param mixed data to store
206 * @param string additional userdefined data
207 * @param mixed userdefined expire date
208 * @param string cache group
210 * @throws Cache_Error
214 function extSave($id, $cachedata, $userdata, $expires = 0, $group = 'default') {
218 return $this->container->save($id, $cachedata, $expires, $group, $userdata);
219 } // end func extSave
222 * Loads the given ID from the cache.
224 * @param string dataset ID
225 * @param string cache group
226 * @return mixed cached data or NULL on failure
229 function load($id, $group = 'default') {
233 return $this->container->load($id, $group);
237 * Returns the userdata field of a cached data set.
239 * @param string dataset ID
240 * @param string cache group
241 * @return string userdata
245 function getUserdata($id, $group = 'default') {
249 return $this->container->getUserdata($id, $group);
250 } // end func getUserdata
253 * Removes the specified dataset from the cache.
255 * @param string dataset ID
256 * @param string cache group
260 function remove($id, $group = 'default') {
264 return $this->container->remove($id, $group);
268 * Flushes the cache - removes all data from it
270 * @param string cache group, if empty all groups will be flashed
271 * @return integer number of removed datasets
273 function flush($group = 'default') {
277 return $this->container->flush($group);
281 * Checks if a dataset exists.
283 * Note: this does not say that the cached data is not expired!
285 * @param string dataset ID
286 * @param string cache group
290 function isCached($id, $group = 'default') {
294 return $this->container->isCached($id, $group);
295 } // end func isCached
298 * Checks if a dataset is expired
300 * @param string dataset ID
301 * @param string cache group
302 * @param integer maximum age for the cached data in seconds - 0 for endless
303 * If the cached data is older but the given lifetime it will
304 * be removed from the cache. You don't have to provide this
305 * argument if you call isExpired(). Every dataset knows
306 * it's expire date and will be removed automatically. Use
307 * this only if you know what you're doing...
311 function isExpired($id, $group = 'default', $max_age = 0) {
315 return $this->container->isExpired($id, $group, $max_age);
316 } // end func isExpired
319 * Generates a "unique" ID for the given value
321 * This is a quick but dirty hack to get a "unique" ID for a any kind of variable.
322 * ID clashes might occur from time to time although they are extreme unlikely!
324 * @param mixed variable to generate a ID for
325 * @return string "unique" ID
328 function generateID($variable) {
329 // WARNING: ID clashes are possible although unlikely
330 return md5(serialize($variable));
334 * Calls the garbage collector of the storage object with a certain probability
336 * @param boolean Force a garbage collection run?
337 * @see $gc_probability, $gc_time
339 function garbageCollection($force = false) {
340 static $last_run = 0;
345 srand((double) microtime() * 1000000);
347 // time and probability based
348 if (($force) || ($last_run && $last_run < time() + $this->gc_time) || (rand(1, 100) < $this->gc_probability)) {
349 $this->container->garbageCollection($this->gc_maxlifetime);
352 } // end func garbageCollection