~ubuntu-branches/ubuntu/wily/php-horde-cache/wily

« back to all changes in this revision

Viewing changes to Horde_Cache-2.4.2/lib/Horde/Cache.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2014-04-13 09:11:33 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20140413091133-um8xxmlwuawkhbi5
Tags: 2.4.2-1
New upstream version 2.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * This class provides the API interface to the cache storage drivers.
 
4
 *
 
5
 * Copyright 1999-2014 Horde LLC (http://www.horde.org/)
 
6
 *
 
7
 * See the enclosed file COPYING for license information (LGPL). If you
 
8
 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 
9
 *
 
10
 * @author   Anil Madhavapeddy <anil@recoil.org>
 
11
 * @author   Chuck Hagenbuch <chuck@horde.org>
 
12
 * @author   Michael Slusarz <slusarz@horde.org>
 
13
 * @category Horde
 
14
 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 
15
 * @package  Cache
 
16
 */
 
17
class Horde_Cache
 
18
{
 
19
    /**
 
20
     * Cache parameters.
 
21
     *
 
22
     * @var array
 
23
     */
 
24
    protected $_params = array(
 
25
        'compress' => false,
 
26
        'lifetime' => 86400
 
27
    );
 
28
 
 
29
    /**
 
30
     * Logger.
 
31
     *
 
32
     * @var Horde_Log_Logger
 
33
     */
 
34
    protected $_logger;
 
35
 
 
36
    /**
 
37
     * Storage object.
 
38
     *
 
39
     * @var Horde_Cache_Storage
 
40
     */
 
41
    protected $_storage;
 
42
 
 
43
    /**
 
44
     * Constructor.
 
45
     *
 
46
     * @param Horde_Cache_Storage $storage  The storage object.
 
47
     * @param array $params                 Parameter array:
 
48
     * <pre>
 
49
     *   - compress: (boolean) Compress data (if possible)?
 
50
     *               DEFAULT: false
 
51
     *   - lifetime: (integer) Lifetime of data, in seconds.
 
52
     *               DEFAULT: 86400 seconds
 
53
     *   - logger: (Horde_Log_Logger) Log object to use for log/debug messages.
 
54
     * </pre>
 
55
     */
 
56
    public function __construct(Horde_Cache_Storage_Base $storage,
 
57
                                array $params = array())
 
58
    {
 
59
        if (isset($params['logger'])) {
 
60
            $this->_logger = $params['logger'];
 
61
            unset($params['logger']);
 
62
 
 
63
            $storage->setLogger($this->_logger);
 
64
        }
 
65
 
 
66
        $this->_params = array_merge($this->_params, $params);
 
67
        $this->_storage = $storage;
 
68
    }
 
69
 
 
70
    /**
 
71
     * Attempts to directly output a cached object.
 
72
     *
 
73
     * @todo Change default lifetime to 0
 
74
     *
 
75
     * @param string $key        Object ID to query.
 
76
     * @param integer $lifetime  Lifetime of the object in seconds.
 
77
     *
 
78
     * @return boolean  True if output or false if no object was found.
 
79
     */
 
80
    public function output($key, $lifetime = 1)
 
81
    {
 
82
        $data = $this->get($key, $lifetime);
 
83
        if ($data === false) {
 
84
            return false;
 
85
        }
 
86
 
 
87
        echo $data;
 
88
        return true;
 
89
    }
 
90
 
 
91
    /**
 
92
     * Retrieve cached data.
 
93
     *
 
94
     * @todo Change default lifetime to 0
 
95
     *
 
96
     * @param string $key        Object ID to query.
 
97
     * @param integer $lifetime  Lifetime of the object in seconds.
 
98
     *
 
99
     * @return mixed  Cached data, or false if none was found.
 
100
     */
 
101
    public function get($key, $lifetime = 1)
 
102
    {
 
103
        $res = $this->_storage->get($key, $lifetime);
 
104
 
 
105
        if (empty($this->_params['compress']) || !is_string($res)) {
 
106
            return $res;
 
107
        }
 
108
 
 
109
        $compress = new Horde_Compress_Fast();
 
110
        return $compress->decompress($res);
 
111
    }
 
112
 
 
113
    /**
 
114
     * Store an object in the cache.
 
115
     *
 
116
     * @param string $key        Object ID used as the caching key.
 
117
     * @param string $data       Data to store in the cache.
 
118
     * @param integer $lifetime  Object lifetime - i.e. the time before the
 
119
     *                           data becomes available for garbage
 
120
     *                           collection.  If null use the default Horde GC
 
121
     *                           time.  If 0 will not be GC'd.
 
122
     */
 
123
    public function set($key, $data, $lifetime = null)
 
124
    {
 
125
        if (!empty($this->_params['compress'])) {
 
126
            $compress = new Horde_Compress_Fast();
 
127
            $data = $compress->compress($data);
 
128
        }
 
129
 
 
130
        $lifetime = is_null($lifetime)
 
131
            ? $this->_params['lifetime']
 
132
            : $lifetime;
 
133
 
 
134
        $this->_storage->set($key, $data, $lifetime);
 
135
    }
 
136
 
 
137
    /**
 
138
     * Checks if a given key exists in the cache, valid for the given
 
139
     * lifetime.
 
140
     *
 
141
     * @todo Change default lifetime to 0
 
142
     *
 
143
     * @param string $key        Cache key to check.
 
144
     * @param integer $lifetime  Lifetime of the key in seconds.
 
145
     *
 
146
     * @return boolean  Existence.
 
147
     */
 
148
    public function exists($key, $lifetime = 1)
 
149
    {
 
150
        return $this->_storage->exists($key, $lifetime);
 
151
    }
 
152
 
 
153
    /**
 
154
     * Expire any existing data for the given key.
 
155
     *
 
156
     * @param string $key  Cache key to expire.
 
157
     *
 
158
     * @return boolean  Success or failure.
 
159
     */
 
160
    public function expire($key)
 
161
    {
 
162
        return $this->_storage->expire($key);
 
163
    }
 
164
 
 
165
    /**
 
166
     * Clears all data from the cache.
 
167
     *
 
168
     * @throws Horde_Cache_Exception
 
169
     */
 
170
    public function clear()
 
171
    {
 
172
        return $this->_storage->clear();
 
173
    }
 
174
 
 
175
    /**
 
176
     * Tests the driver for read/write access.
 
177
     *
 
178
     * @return boolean  True if read/write is available.
 
179
     */
 
180
    public function testReadWrite()
 
181
    {
 
182
        $key = '__horde_cache_testkey';
 
183
 
 
184
        try {
 
185
            $this->_storage->set($key, 1);
 
186
            if ($this->_storage->exists($key)) {
 
187
                $this->_storage->expire($key);
 
188
                return true;
 
189
            }
 
190
        } catch (Exception $e) {}
 
191
 
 
192
        return false;
 
193
    }
 
194
 
 
195
}