~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Cache/Backend/Apc.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://framework.zend.com/license/new-bsd
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@zend.com so we can send you a copy immediately.
 
14
 *
 
15
 * @category   Zend
 
16
 * @package    Zend_Cache
 
17
 * @subpackage Zend_Cache_Backend
 
18
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
19
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
20
 */
 
21
 
 
22
 
 
23
/**
 
24
 * @see Zend_Cache_Backend_Interface
 
25
 */
 
26
require_once 'Zend/Cache/Backend/ExtendedInterface.php';
 
27
 
 
28
/**
 
29
 * @see Zend_Cache_Backend
 
30
 */
 
31
require_once 'Zend/Cache/Backend.php';
 
32
 
 
33
 
 
34
/**
 
35
 * @package    Zend_Cache
 
36
 * @subpackage Zend_Cache_Backend
 
37
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
38
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
39
 */
 
40
class Zend_Cache_Backend_Apc extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
 
41
{
 
42
    /**
 
43
     * Log message
 
44
     */
 
45
    const TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND = 'Zend_Cache_Backend_Apc::clean() : tags are unsupported by the Apc backend';
 
46
    const TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND =  'Zend_Cache_Backend_Apc::save() : tags are unsupported by the Apc backend';
 
47
 
 
48
    /**
 
49
     * Constructor
 
50
     *
 
51
     * @param  array $options associative array of options
 
52
     * @throws Zend_Cache_Exception
 
53
     * @return void
 
54
     */
 
55
    public function __construct(array $options = array())
 
56
    {
 
57
        if (!extension_loaded('apc')) {
 
58
            Zend_Cache::throwException('The apc extension must be loaded for using this backend !');
 
59
        }
 
60
        parent::__construct($options);
 
61
    }
 
62
 
 
63
    /**
 
64
     * Test if a cache is available for the given id and (if yes) return it (false else)
 
65
     *
 
66
     * WARNING $doNotTestCacheValidity=true is unsupported by the Apc backend
 
67
     *
 
68
     * @param  string  $id                     cache id
 
69
     * @param  boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
 
70
     * @return string cached datas (or false)
 
71
     */
 
72
    public function load($id, $doNotTestCacheValidity = false)
 
73
    {
 
74
        $tmp = apc_fetch($id);
 
75
        if (is_array($tmp)) {
 
76
            return $tmp[0];
 
77
        }
 
78
        return false;
 
79
    }
 
80
 
 
81
    /**
 
82
     * Test if a cache is available or not (for the given id)
 
83
     *
 
84
     * @param  string $id cache id
 
85
     * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
 
86
     */
 
87
    public function test($id)
 
88
    {
 
89
        $tmp = apc_fetch($id);
 
90
        if (is_array($tmp)) {
 
91
            return $tmp[1];
 
92
        }
 
93
        return false;
 
94
    }
 
95
 
 
96
    /**
 
97
     * Save some string datas into a cache record
 
98
     *
 
99
     * Note : $data is always "string" (serialization is done by the
 
100
     * core not by the backend)
 
101
     *
 
102
     * @param string $data datas to cache
 
103
     * @param string $id cache id
 
104
     * @param array $tags array of strings, the cache record will be tagged by each string entry
 
105
     * @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
 
106
     * @return boolean true if no problem
 
107
     */
 
108
    public function save($data, $id, $tags = array(), $specificLifetime = false)
 
109
    {
 
110
        $lifetime = $this->getLifetime($specificLifetime);
 
111
        $result = apc_store($id, array($data, time(), $lifetime), $lifetime);
 
112
        if (count($tags) > 0) {
 
113
            $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
 
114
        }
 
115
        return $result;
 
116
    }
 
117
 
 
118
    /**
 
119
     * Remove a cache record
 
120
     *
 
121
     * @param  string $id cache id
 
122
     * @return boolean true if no problem
 
123
     */
 
124
    public function remove($id)
 
125
    {
 
126
        return apc_delete($id);
 
127
    }
 
128
 
 
129
    /**
 
130
     * Clean some cache records
 
131
     *
 
132
     * Available modes are :
 
133
     * 'all' (default)  => remove all cache entries ($tags is not used)
 
134
     * 'old'            => unsupported
 
135
     * 'matchingTag'    => unsupported
 
136
     * 'notMatchingTag' => unsupported
 
137
     * 'matchingAnyTag' => unsupported
 
138
     *
 
139
     * @param  string $mode clean mode
 
140
     * @param  array  $tags array of tags
 
141
     * @throws Zend_Cache_Exception
 
142
     * @return boolean true if no problem
 
143
     */
 
144
    public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
 
145
    {
 
146
        switch ($mode) {
 
147
            case Zend_Cache::CLEANING_MODE_ALL:
 
148
                return apc_clear_cache('user');
 
149
                break;
 
150
            case Zend_Cache::CLEANING_MODE_OLD:
 
151
                $this->_log("Zend_Cache_Backend_Apc::clean() : CLEANING_MODE_OLD is unsupported by the Apc backend");
 
152
                break;
 
153
            case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
 
154
            case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
 
155
            case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
 
156
                $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND);
 
157
                break;
 
158
            default:
 
159
                Zend_Cache::throwException('Invalid mode for clean() method');
 
160
                break;
 
161
        }
 
162
    }
 
163
 
 
164
    /**
 
165
     * Return true if the automatic cleaning is available for the backend
 
166
     *
 
167
     * DEPRECATED : use getCapabilities() instead
 
168
     * 
 
169
     * @deprecated 
 
170
     * @return boolean
 
171
     */
 
172
    public function isAutomaticCleaningAvailable()
 
173
    {
 
174
        return false;
 
175
    }
 
176
    
 
177
    /**
 
178
     * Return the filling percentage of the backend storage
 
179
     * 
 
180
     * @throws Zend_Cache_Exception
 
181
     * @return int integer between 0 and 100
 
182
     */
 
183
    public function getFillingPercentage()
 
184
    {
 
185
        $mem = apc_sma_info(true);
 
186
        $memSize    = $mem['num_seg'] * $mem['seg_size'];
 
187
        $memAvailable= $mem['avail_mem'];
 
188
        $memUsed = $memSize - $memAvailable;
 
189
        if ($memSize == 0) {
 
190
            Zend_Cache::throwException('can\'t get apc memory size');
 
191
        }
 
192
        if ($memUsed > $memSize) {
 
193
            return 100;
 
194
        }
 
195
        return ((int) (100. * ($memUsed / $memSize)));
 
196
    }
 
197
    
 
198
    /**
 
199
     * Return an array of stored tags
 
200
     *
 
201
     * @return array array of stored tags (string)
 
202
     */
 
203
    public function getTags()
 
204
    {   
 
205
        $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
 
206
        return array();
 
207
    }
 
208
    
 
209
    /**
 
210
     * Return an array of stored cache ids which match given tags
 
211
     * 
 
212
     * In case of multiple tags, a logical AND is made between tags
 
213
     *
 
214
     * @param array $tags array of tags
 
215
     * @return array array of matching cache ids (string)
 
216
     */
 
217
    public function getIdsMatchingTags($tags = array())
 
218
    {
 
219
        $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
 
220
        return array();               
 
221
    }
 
222
 
 
223
    /**
 
224
     * Return an array of stored cache ids which don't match given tags
 
225
     * 
 
226
     * In case of multiple tags, a logical OR is made between tags
 
227
     *
 
228
     * @param array $tags array of tags
 
229
     * @return array array of not matching cache ids (string)
 
230
     */    
 
231
    public function getIdsNotMatchingTags($tags = array())
 
232
    {
 
233
        $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
 
234
        return array();         
 
235
    }
 
236
    
 
237
    /**
 
238
     * Return an array of stored cache ids which match any given tags
 
239
     * 
 
240
     * In case of multiple tags, a logical AND is made between tags
 
241
     *
 
242
     * @param array $tags array of tags
 
243
     * @return array array of any matching cache ids (string)
 
244
     */
 
245
    public function getIdsMatchingAnyTags($tags = array())
 
246
    {
 
247
        $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
 
248
        return array();         
 
249
    }
 
250
    
 
251
    /**
 
252
     * Return an array of stored cache ids
 
253
     * 
 
254
     * @return array array of stored cache ids (string)
 
255
     */
 
256
    public function getIds()
 
257
    {
 
258
        $res = array();
 
259
        $array = apc_cache_info('user', false);
 
260
        $records = $array['cache_list'];
 
261
        foreach ($records as $record) {
 
262
            $res[] = $record['info'];
 
263
        }
 
264
        return $res;
 
265
    }
 
266
    
 
267
    /**
 
268
     * Return an array of metadatas for the given cache id
 
269
     *
 
270
     * The array must include these keys :
 
271
     * - expire : the expire timestamp
 
272
     * - tags : a string array of tags
 
273
     * - mtime : timestamp of last modification time
 
274
     * 
 
275
     * @param string $id cache id
 
276
     * @return array array of metadatas (false if the cache id is not found)
 
277
     */
 
278
    public function getMetadatas($id)
 
279
    {
 
280
        $tmp = apc_fetch($id);
 
281
        if (is_array($tmp)) {
 
282
            $data = $tmp[0];
 
283
            $mtime = $tmp[1];
 
284
            if (!isset($tmp[2])) {
 
285
                // because this record is only with 1.7 release
 
286
                // if old cache records are still there...
 
287
                return false;
 
288
            }
 
289
            $lifetime = $tmp[2];
 
290
            return array(
 
291
                'expire' => $mtime + $lifetime,
 
292
                'tags' => array(),
 
293
                'mtime' => $mtime
 
294
            );
 
295
        }
 
296
        return false;  
 
297
    }
 
298
    
 
299
    /**
 
300
     * Give (if possible) an extra lifetime to the given cache id
 
301
     *
 
302
     * @param string $id cache id
 
303
     * @param int $extraLifetime
 
304
     * @return boolean true if ok
 
305
     */
 
306
    public function touch($id, $extraLifetime)
 
307
    {
 
308
        $tmp = apc_fetch($id);
 
309
        if (is_array($tmp)) {
 
310
            $data = $tmp[0];
 
311
            $mtime = $tmp[1];
 
312
            if (!isset($tmp[2])) {
 
313
                // because this record is only with 1.7 release
 
314
                // if old cache records are still there...
 
315
                return false;
 
316
            }
 
317
            $lifetime = $tmp[2];
 
318
            $newLifetime = $lifetime - (time() - $mtime) + $extraLifetime;
 
319
            if ($newLifetime <=0) {
 
320
                return false; 
 
321
            }
 
322
            apc_store($id, array($data, time(), $newLifetime), $newLifetime);
 
323
            return true;
 
324
        }
 
325
        return false;
 
326
    }
 
327
    
 
328
    /**
 
329
     * Return an associative array of capabilities (booleans) of the backend
 
330
     * 
 
331
     * The array must include these keys :
 
332
     * - automatic_cleaning (is automating cleaning necessary)
 
333
     * - tags (are tags supported)
 
334
     * - expired_read (is it possible to read expired cache records
 
335
     *                 (for doNotTestCacheValidity option for example))
 
336
     * - priority does the backend deal with priority when saving
 
337
     * - infinite_lifetime (is infinite lifetime can work with this backend)
 
338
     * - get_list (is it possible to get the list of cache ids and the complete list of tags)
 
339
     * 
 
340
     * @return array associative of with capabilities
 
341
     */
 
342
    public function getCapabilities()
 
343
    {
 
344
        return array(
 
345
            'automatic_cleaning' => false,
 
346
            'tags' => false,
 
347
            'expired_read' => false,
 
348
            'priority' => false,
 
349
            'infinite_lifetime' => false,
 
350
            'get_list' => true
 
351
        );
 
352
    }
 
353
 
 
354
}