summaryrefslogtreecommitdiff
path: root/pear/Cache.php
blob: 3bb28a28dcd8a1f9103183861819590bae96247c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
// +----------------------------------------------------------------------+
// | PHP version 4.0                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Ulf Wendel <ulf.wendel@phpdoc.de>                           |
// |          Sebastian Bergmann <sb@sebastian-bergmann.de>               |
// +----------------------------------------------------------------------+
//
// $Id$

require_once 'Cache/Error.php';

/**
* Cache is a base class for cache implementations.
*
* The pear cache module is a generic data cache which can be used to 
* cache script runs. The idea behind the cache is quite simple. If you have
* the same input parameters for whatever tasks/algorithm you use you'll
* usually get the same output. So why not caching templates, functions calls,
* graphic generation etc. Caching certain actions e.g. XSLT tranformations
* saves you lots of time. 
*
* The design of the cache reminds of PHPLibs session implementation. A 
* (PHPLib: session) controller uses storage container (PHPLib: ct_*.inc) to save 
* certain data (PHPLib: session data). In contrast to the session stuff it's up to 
* you to generate an ID for the data to cache. If you're using the output cache
* you might use the script name as a seed for cache::generateID(), if your using the
* function cache you'd use an array with all function parameters.
*
* Usage example of the generic data cache:
*
* require_once('Cache.php');
*
* $cache = new Cache('file', array('cache_dir' => 'cache/') );
* $id = $cache->generateID('testentry');
*
* if ($data = $cache->get($id)) {
*    print "Cache hit.<br>Data: $data";
*
* } else {
*   $data = 'data of any kind';
*   $cache->save($id, $data);
*   print 'Cache miss.<br>';
* }
*
* WARNING: No File/DB-Table-Row locking is implemented yet,
*          it's possible, that you get corrupted data-entries under
*          bad circumstances  (especially with the file container)
*
* @author   Ulf Wendel <ulf.wendel@phpdoc.de>
* @version  $Id$
* @package  Cache
* @access   public
*/
class Cache extends PEAR {

    /**
    * Enables / disables caching.
    *
    * TODO: Add explanation what this is good for.
    *
    * @var      boolean
    * @access   private
    */
    var $caching = true;

    /**
    * Garbage collection: probability in seconds
    *
    * If set to a value above 0 a garbage collection will
    * flush all cache entries older than the specified number
    * of seconds.
    *
    * @var      integer
    * @see      $gc_probability, $gc_maxlifetime
    * @access   public
    */
    var $gc_time  = 1;

    /**
    * Garbage collection: probability in percent
    *
    * TODO: Add an explanation.
    *
    * @var      integer     0 => never
    * @see      $gc_time, $gc_maxlifetime
    * @access   public
    */
    var $gc_probability = 1;
    
    /**
    * Garbage collection: delete all entries not use for n seconds.
    *
    * Default is one day, 60 * 60 * 24 = 86400 seconds.
    *
    * @var  integer
    * @see  $gc_probability, $gc_time
    */
    var $gc_maxlifetime = 86400;

    /**
    * Storage container object.
    *
    * @var  object Cache_Container
    */
    var $container;

    //
    // public methods
    //

    /**
    *
    * @param    string  Name of storage container class
    * @param    array   Array with storage class dependend config options
    */
    function Cache($storage_driver, $storage_options = '')
    {
        $this->PEAR();
        $storage_driver = strtolower($storage_driver);
        $storage_class = 'Cache_Container_' . $storage_driver;
        $storage_classfile = 'Cache/Container/' . $storage_driver . '.php';

        include_once $storage_classfile;
        $this->container = new $storage_class($storage_options);
    }

    //deconstructor
    function _Cache()
    {
        $this->garbageCollection();
    }

    /**
     * Returns the current caching state.
     *
     * @return  boolean     The current caching state.
     * @access  public
     */
    function getCaching()
    {
        return ($this->caching);
    }

    /**
     * Enables or disables caching.
     *
     * @param   boolean     The new caching state.
     * @access  public
     */
    function setCaching($state)
    {
        $this->caching = $state;
    }

    /**
    * Returns the requested dataset it if exists and is not expired
    *
    * @param    string  dataset ID
    * @param    string  cache group
    * @return   mixed   cached data or NULL on failure
    * @access   public
    */
    function get($id, $group = 'default') {
        if (!$this->caching)
            return '';

        if ($this->isCached($id, $group) && !$this->isExpired($id, $group))
            return $this->load($id, $group);

        return NULL;
    } // end func get

    /**
    * Stores the given data in the cache.
    *
    * @param    string  dataset ID used as cache identifier
    * @param    mixed   data to cache
    * @param    integer lifetime of the cached data in seconds - 0 for endless
    * @param    string  cache group
    * @return   boolean
    * @access   public
    */
    function save($id, $data, $expires = 0, $group = 'default') {
        if (!$this->caching)
            return true;

        return $this->extSave($id, $data, '',$expires, $group);
    } // end func save

    /**
    * Stores a dataset without additional userdefined data.
    *
    * @param    string  dataset ID
    * @param    mixed   data to store
    * @param    string  additional userdefined data
    * @param    mixed   userdefined expire date
    * @param    string  cache group
    * @return   boolean
    * @throws   Cache_Error
    * @access   public
    * @see      getUserdata()
    */
    function extSave($id, $cachedata, $userdata, $expires = 0, $group = 'default') {
        if (!$this->caching)
            return true;

        return $this->container->save($id, $cachedata, $expires, $group, $userdata);
    } // end func extSave

    /**
    * Loads the given ID from the cache.
    *
    * @param    string  dataset ID
    * @param    string  cache group
    * @return   mixed   cached data or NULL on failure
    * @access   public
    */
    function load($id, $group = 'default') {
        if (!$this->caching)
            return '';

        return $this->container->load($id, $group);
    } // end func load

    /**
    * Returns the userdata field of a cached data set.
    *
    * @param    string  dataset ID
    * @param    string  cache group
    * @return   string  userdata
    * @access   public
    * @see      extSave()
    */
    function getUserdata($id, $group = 'default') {
        if (!$this->caching)
            return '';

        return $this->container->getUserdata($id, $group);
    } // end func getUserdata

    /**
    * Removes the specified dataset from the cache.
    *
    * @param    string  dataset ID
    * @param    string  cache group
    * @return   boolean
    * @access   public
    */
    function delete($id, $group = 'default') {
        if (!$this->caching)
            return true;

        return $this->container->delete($id, $group);
    } // end func delete

    /**
    * Flushes the cache - removes all data from it
    *
    * @param    string  cache group, if empty all groups will be flashed
    * @return   integer number of removed datasets
    */
    function flush($group = '') {
        if (!$this->caching)
            return true;

        return $this->container->flush($group);
    } // end func flush

    /**
    * Checks if a dataset exists.
    *
    * Note: this does not say that the cached data is not expired!
    *
    * @param    string  dataset ID
    * @param    string  cache group
    * @return   boolean
    * @access   public
    */
    function isCached($id, $group = 'default') {
        if (!$this->caching)
            return false;

        return $this->container->isCached($id, $group);
    } // end func isCached

    /**
    * Checks if a dataset is expired
    *
    * @param    string  dataset ID
    * @param    string  cache group
    * @param    integer maximum age for the cached data in seconds - 0 for endless
    *                   If the cached data is older but the given lifetime it will
    *                   be removed from the cache. You don't have to provide this
    *                   argument if you call isExpired(). Every dataset knows
    *                   it's expire date and will be removed automatically. Use
    *                   this only if you know what you're doing...
    * @return   boolean
    * @access   public
    */
    function isExpired($id, $group = 'default', $max_age = 0) {
        if (!$this->caching)
            return true;

        return $this->container->isExpired($id, $group, $max_age);
    } // end func isExpired

    /**
    * Generates a "unique" ID for the given value
    *
    * This is a quick but dirty hack to get a "unique" ID for a any kind of variable.
    * ID clashes might occur from time to time although they are extreme unlikely!
    *
    * @param    mixed   variable to generate a ID for
    * @return   string  "unique" ID
    * @access   public
    */
    function generateID($variable) {
        // WARNING: ID clashes are possible although unlikely
        return md5(serialize($variable));
    }

    /**
    * Calls the garbage collector of the storage object with a certain probability
    *
    * @param    boolean Force a garbage collection run?
    * @see  $gc_probability, $gc_time
    */
    function garbageCollection($force = false) {
        static $last_run = 0;

        if (!$this->caching)
            return;

        srand((double) microtime() * 1000000);

        // time and probability based
        if (($force) || ($last_run && $last_run < time() + $this->gc_time) || (rand(1, 100) < $this->gc_probability)) {
            $this->container->garbageCollection($this->gc_maxlifetime);
            $last_run = time();
        }
    } // end func garbageCollection

} // end class cache
?>