3
* This class provides the API interface to the cache storage drivers.
5
* Copyright 1999-2014 Horde LLC (http://www.horde.org/)
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.
10
* @author Anil Madhavapeddy <anil@recoil.org>
11
* @author Chuck Hagenbuch <chuck@horde.org>
12
* @author Michael Slusarz <slusarz@horde.org>
14
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
24
protected $_params = array(
32
* @var Horde_Log_Logger
39
* @var Horde_Cache_Storage
46
* @param Horde_Cache_Storage $storage The storage object.
47
* @param array $params Parameter array:
49
* - compress: (boolean) Compress data (if possible)?
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.
56
public function __construct(Horde_Cache_Storage_Base $storage,
57
array $params = array())
59
if (isset($params['logger'])) {
60
$this->_logger = $params['logger'];
61
unset($params['logger']);
63
$storage->setLogger($this->_logger);
66
$this->_params = array_merge($this->_params, $params);
67
$this->_storage = $storage;
71
* Attempts to directly output a cached object.
73
* @todo Change default lifetime to 0
75
* @param string $key Object ID to query.
76
* @param integer $lifetime Lifetime of the object in seconds.
78
* @return boolean True if output or false if no object was found.
80
public function output($key, $lifetime = 1)
82
$data = $this->get($key, $lifetime);
83
if ($data === false) {
92
* Retrieve cached data.
94
* @todo Change default lifetime to 0
96
* @param string $key Object ID to query.
97
* @param integer $lifetime Lifetime of the object in seconds.
99
* @return mixed Cached data, or false if none was found.
101
public function get($key, $lifetime = 1)
103
$res = $this->_storage->get($key, $lifetime);
105
if (empty($this->_params['compress']) || !is_string($res)) {
109
$compress = new Horde_Compress_Fast();
110
return $compress->decompress($res);
114
* Store an object in the cache.
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.
123
public function set($key, $data, $lifetime = null)
125
if (!empty($this->_params['compress'])) {
126
$compress = new Horde_Compress_Fast();
127
$data = $compress->compress($data);
130
$lifetime = is_null($lifetime)
131
? $this->_params['lifetime']
134
$this->_storage->set($key, $data, $lifetime);
138
* Checks if a given key exists in the cache, valid for the given
141
* @todo Change default lifetime to 0
143
* @param string $key Cache key to check.
144
* @param integer $lifetime Lifetime of the key in seconds.
146
* @return boolean Existence.
148
public function exists($key, $lifetime = 1)
150
return $this->_storage->exists($key, $lifetime);
154
* Expire any existing data for the given key.
156
* @param string $key Cache key to expire.
158
* @return boolean Success or failure.
160
public function expire($key)
162
return $this->_storage->expire($key);
166
* Clears all data from the cache.
168
* @throws Horde_Cache_Exception
170
public function clear()
172
return $this->_storage->clear();
176
* Tests the driver for read/write access.
178
* @return boolean True if read/write is available.
180
public function testReadWrite()
182
$key = '__horde_cache_testkey';
185
$this->_storage->set($key, 1);
186
if ($this->_storage->exists($key)) {
187
$this->_storage->expire($key);
190
} catch (Exception $e) {}