* @var array
*/
protected $_params = array(
+ 'compress' => false,
'lifetime' => 86400,
'prefix' => '',
);
*
* @param array $params Parameter array:
* <pre>
+ * 'compress' - (boolean) Compress data? Requires the 'lzf' PECL
+ * extension.
+ * DEFAULT: false
* 'lifetime' - (integer) Lifetime of data, in seconds.
* DEFAULT: 86400 seconds
* 'logger' - (Horde_Log_Logger) Log object to use for log/debug messages.
unset($params['logger']);
}
+ if (!empty($params['compress']) && !extension_loaded('lzf')) {
+ unset($params['compress']);
+ }
+
$this->_params = array_merge($this->_params, $params);
}
}
/**
- * Attempts to retrieve a cached object and return it to the
- * caller.
+ * Retrieve cached data.
*
* @param string $key Object ID to query.
* @param integer $lifetime Lifetime of the object in seconds.
*
* @return mixed Cached data, or false if none was found.
*/
- abstract public function get($key, $lifetime = 1);
+ public function get($key, $lifetime = 1)
+ {
+ $res = $this->_get($key, $lifetime);
+
+ return ($this->_params['compress'] && ($res !== false))
+ // lzf_decompress() returns false on error
+ ? lzf_decompress($res)
+ : $res;
+ }
/**
- * Attempts to store an object in the cache.
+ * @see get()
+ */
+ abstract protected function _get($key, $lifetime);
+
+ /**
+ * Store an object in the cache.
*
* @param string $key Object ID used as the caching key.
* @param mixed $data Data to store in the cache.
*
* @throws Horde_Cache_Exception
*/
- abstract public function set($key, $data, $lifetime = null);
+ public function set($key, $data, $lifetime = null)
+ {
+ if (!is_string($data)) {
+ throw new Horde_Cache_Exception('Cache data must be a string.');
+ }
+
+ if ($this->_params['compress']) {
+ $data = lzf_compress($data);
+ }
+
+ $res = $this->_set($key, $data, $lifetime);
+ }
+
+ /**
+ * @see set()
+ */
+ abstract protected function _set($key, $data, $lifetime);
/**
* Checks if a given key exists in the cache, valid for the given
{
return is_null($lifetime) ? $this->_params['lifetime'] : $lifetime;
}
+
}
class Horde_Cache_Apc extends Horde_Cache
{
/**
- * Attempts to retrieve a piece of cached data and return it to
- * the caller.
- *
- * @param string $key Cache key to fetch.
- * @param integer $lifetime Lifetime of the key in seconds.
- *
- * @return mixed Cached data, or false if none was found.
*/
- public function get($key, $lifetime = 1)
+ protected function _get($key, $lifetime)
{
$key = $this->_params['prefix'] . $key;
$this->_setExpire($key, $lifetime);
}
/**
- * Attempts to store an object to the cache.
- *
- * @param string $key Cache key (identifier).
- * @param string $data Data to store in the cache.
- * @param integer $lifetime Data lifetime.
- *
- * @throws Horde_Cache_Exception
*/
- public function set($key, $data, $lifetime = null)
+ protected function _set($key, $data, $lifetime)
{
- if (!is_string($data)) {
- throw new Horde_Cache_Exception('Data must be a string.');
- }
$key = $this->_params['prefix'] . $key;
$lifetime = $this->_getLifetime($lifetime);
if (apc_store($key . '_expire', time(), $lifetime)) {
{
$key = $this->_params['prefix'] . $key;
$this->_setExpire($key, $lifetime);
- return (apc_fetch($key) === false) ? false : true;
+ return (apc_fetch($key) !== false);
}
/**
apc_delete($key . '_expire');
}
}
+
}
* Construct a new Horde_Cache object.
*
* @param array $params Parameter array.
+ *
* @throws Horde_Cache_Exception
*/
public function __construct($params = array())
}
/**
- * Attempts to retrieve a piece of cached data and return it to the caller.
- *
- * @param string $key Cache key to fetch.
- * @param integer $lifetime Lifetime of the key in seconds.
- *
- * @return mixed Cached data, or false if none was found.
*/
- public function get($key, $lifetime = 1)
+ protected function _get($key, $lifetime)
{
$key = $this->_params['prefix'] . $key;
$this->_setExpire($key, $lifetime);
}
/**
- * Attempts to store an object to the cache.
- *
- * @param string $key Cache key (identifier).
- * @param string $data Data to store in the cache.
- * @param integer $lifetime Data lifetime.
- *
- * @throws Horde_Cache_Exception
*/
- public function set($key, $data, $lifetime = null)
+ protected function _set($key, $data, $lifetime)
{
- if (!is_string($data)) {
- throw new Horde_Cache_Exception('Data must be a string.');
- }
$key = $this->_params['prefix'] . $key;
$lifetime = $this->_getLifetime($lifetime);
if (eaccelerator_put($key . '_expire', time(), $lifetime)) {
eaccelerator_rm($key . '_expire');
}
}
+
}
<?php
/**
- * Exception handler for the Horde_Cache package.
+ * Exception handler for the horde/Cache package.
*
* Copyright 2010 The Horde Project (http://www.horde.org/)
*
* @category Horde
* @package Cache
*/
-class Horde_Cache_Exception extends Horde_Exception_Prior
-{
-}
+class Horde_Cache_Exception extends Horde_Exception_Prior {}
}
/**
- * Attempts to retrieve cached data from the filesystem and return it to
- * the caller.
- *
- * @param string $key Cache key to fetch.
- * @param integer $lifetime Lifetime of the data in seconds.
- *
- * @return mixed Cached data, or false if none was found.
*/
- public function get($key, $lifetime = 1)
+ protected function _get($key, $lifetime)
{
if (!$this->exists($key, $lifetime)) {
/* Nothing cached, return failure. */
if (!$size) {
return '';
}
- $old_error = error_reporting(0);
- $data = file_get_contents($filename);
- error_reporting($old_error);
- return $data;
+ return @file_get_contents($filename);
}
/**
- * Attempts to store data to the filesystem.
- *
- * @param string $key Cache key.
- * @param string $data Data to store in the cache.
- * @param integer $lifetime Data lifetime.
- *
- * @throws Horde_Cache_Exception
*/
- public function set($key, $data, $lifetime = null)
+ protected function _set($key, $data, $lifetime)
{
- if (!is_string($data)) {
- throw new Horde_Cache_Exception('Data must be a string.');
- }
-
$filename = $this->_keyToFile($key, true);
$tmp_file = Horde_Util::getTempFile('HordeCache', true, $this->_dir);
if (isset($this->_params['umask'])) {
/* Key exists in the cache */
if (file_exists($filename)) {
- /* 0 means no expire. */
- if ($lifetime == 0) {
- return true;
- }
-
- /* If the file was been created after the supplied value,
+ /* 0 means no expire.
+ * Also, If the file was been created after the supplied value,
* the data is valid (fresh). */
- if (time() - $lifetime <= filemtime($filename)) {
+ if (($lifetime == 0) ||
+ (time() - $lifetime <= filemtime($filename))) {
return true;
- } else {
- @unlink($filename);
}
+
+ @unlink($filename);
}
return false;
}
$d->close();
}
+
}
}
/**
- * Attempts to retrieve cached data from the memcache and return it to
- * the caller.
- *
- * @param string $key Cache key to fetch.
- * @param integer $lifetime Lifetime of the data in seconds.
- *
- * @return mixed Cached data, or false if none was found.
*/
- public function get($key, $lifetime = 1)
+ protected function _get($key, $lifetime)
{
$key = $this->_params['prefix'] . $key;
if (isset($this->_expirecache[$key])) {
}
/**
- * Attempts to store data to the memcache.
- *
- * @param string $key Cache key.
- * @param mixed $data Data to store in the cache.
- * @param integer $lifetime Data lifetime.
- *
- * @throws Horde_Cache_Exception
*/
- public function set($key, $data, $lifetime = null)
+ protected function _set($key, $data, $lifetime)
{
- if (!is_string($data)) {
- throw new Horde_Cache_Exception('Data must be a string.');
- }
-
$key = $this->_params['prefix'] . $key;
$lifetime = $this->_getLifetime($lifetime);
return $this->_memcache->delete($key);
}
+
}
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
- * @category Horde
- * @package Cache
* @author Gunnar Wrobel <wrobel@pardus.de>
+ * @category Horde
* @license http://www.fsf.org/copyleft/lgpl.html LGPL
* @link http://pear.horde.org/index.php?package=Cache
+ * @package Cache
*/
class Horde_Cache_Mock extends Horde_Cache
{
}
/**
- * Attempts to retrieve a piece of cached data and return it to the caller.
- *
- * @param string $key Cache key to fetch.
- * @param integer $lifetime Lifetime of the key in seconds.
- *
- * @return mixed Cached data, or false if none was found.
*/
- public function get($key, $lifetime = 1)
+ protected function _get($key, $lifetime)
{
return isset($this->_cache[$key])
? $this->_cache[$key]
}
/**
- * Attempts to store an object to the cache.
- *
- * @param string $key Cache key (identifier).
- * @param string $data Data to store in the cache.
- * @param integer $lifetime Data lifetime.
- *
- * @throws Horde_Cache_Exception
*/
- public function set($key, $data, $lifetime = null)
+ protected function _set($key, $data, $lifetime)
{
- if (!is_string($data)) {
- throw new Horde_Cache_Exception('Data must be a string.');
- }
$this->_cache[$key] = $data;
}
{
unset($this->_cache[$key]);
}
+
}
class Horde_Cache_Null extends Horde_Cache
{
/**
- * Attempts to retrieve a piece of cached data and return it to
- * the caller.
- *
- * @param string $key Cache key to fetch.
- * @param integer $lifetime Lifetime of the key in seconds.
- *
- * @return mixed Cached data, or false if none was found.
*/
- public function get($key, $lifetime = 1)
+ protected function _get($key, $lifetime)
{
return false;
}
/**
- * Attempts to store an object to the cache.
- *
- * @param string $key Cache key (identifier).
- * @param string $data Data to store in the cache.
- * @param integer $lifetime Data lifetime.
- *
- * @throws Horde_Cache_Exception
*/
- public function set($key, $data, $lifetime = null)
+ protected function _set($key, $data, $lifetime)
{
- if (!is_string($data)) {
- throw new Horde_Cache_Exception('Data must be a string.');
- }
}
/**
{
return false;
}
+
}
}
/**
- * Attempts to retrieve a piece of cached data and return it to
- * the caller.
- *
- * @param string $key Cache key to fetch.
- * @param integer $lifetime Lifetime of the key in seconds.
- *
- * @return mixed Cached data, or false if none was found.
*/
- public function get($key, $lifetime = 1)
+ protected function _get($key, $lifetime)
{
- if ($this->exists($key, $lifetime)) {
- return $this->_sess[$key]['d'];
- }
-
- return false;
+ return $this->exists($key, $lifetime)
+ ? $this->_sess[$key]['d']
+ : false;
}
/**
- * Attempts to store an object to the cache.
- *
- * @param string $key Cache key (identifier).
- * @param string $data Data to store in the cache.
- * @param integer $lifetime Data lifetime.
- *
- * @throws Horde_Cache_Exception
*/
- public function set($key, $data, $lifetime = null)
+ protected function _set($key, $data, $lifetime)
{
- if (!is_string($data)) {
- throw new Horde_Cache_Exception('Data must be a string.');
- }
$this->_sess[$key] = array(
'd' => $data,
'l' => $this->_getLifetime($lifetime)
return false;
}
+
}
}
/**
- * Attempts to retrieve cached data.
- *
- * @param string $key Cache key to fetch.
- * @param integer $lifetime Maximum age of the data in seconds or
- * 0 for any object.
- *
- * @return mixed Cached data, or false if none was found.
*/
- public function get($key, $lifetime = 1)
+ protected function _get($key, $lifetime)
{
$okey = $key;
$key = hash('md5', $key);
}
/**
- * Attempts to store data.
- *
- * @param string $key Cache key.
- * @param string $data Data to store in the cache.
- * @param integer $lifetime Maximum data life span or 0 for a
- * non-expiring object.
- *
- * @throws Horde_Cache_Exception
*/
- public function set($key, $data, $lifetime = null)
+ protected function _set($key, $data, $lifetime)
{
- if (!is_string($data)) {
- throw new Horde_Cache_Exception('Data must be a string.');
- }
-
$okey = $key;
$key = hash('md5', $key);
return true;
}
+
}
}
/**
- * Attempts to retrieve a cached object and return it to the
- * caller.
- *
- * @param string $key Object ID to query.
- * @param integer $lifetime Lifetime of the object in seconds.
- *
- * @return mixed Cached data, or false if none was found.
*/
- public function get($key, $lifetime = 1)
+ protected function _get($key, $lifetime)
{
foreach ($this->_stack as $val) {
$result = $val->get($key, $lifetime);
if ($result !== false) {
- break;
+ return $result;
}
}
- return $result;
+ return false;
}
/**
- * Attempts to store an object in the cache.
- *
- * @param string $key Object ID used as the caching key.
- * @param string $data Data to store in the cache.
- * @param integer $lifetime Object lifetime - i.e. the time before the
- * data becomes available for garbage
- * collection. If null use the default Horde GC
- * time. If 0 will not be GC'd.
- *
- * @throws Horde_Cache_Exception
*/
- public function set($key, $data, $lifetime = null)
+ protected function _set($key, $data, $lifetime)
{
- if (!is_string($data)) {
- throw new Horde_Cache_Exception('Data must be a string.');
- }
-
/* Do writes in *reverse* order - it is OK if a write to one of the
* non-master backends fails. */
$master = true;
return $success;
}
+
}
class Horde_Cache_Xcache extends Horde_Cache
{
/**
- * Attempts to retrieve a piece of cached data and return it to the caller.
- *
- * @param string $key Cache key to fetch.
- * @param integer $lifetime Lifetime of the key in seconds.
- *
- * @return mixed Cached data, or false if none was found.
*/
- public function get($key, $lifetime = 1)
+ protected function _get($key, $lifetime)
{
$key = $this->_params['prefix'] . $key;
$this->_setExpire($key, $lifetime);
}
/**
- * Attempts to store an object to the cache.
- *
- * @param string $key Cache key (identifier).
- * @param string $data Data to store in the cache.
- * @param integer $lifetime Data lifetime.
- *
- * @throws Horde_Cache_Exception
*/
- public function set($key, $data, $lifetime = null)
+ protected function _set($key, $data, $lifetime)
{
- if (!is_string($data)) {
- throw new Horde_Cache_Exception('Data must be a string.');
- }
$key = $this->_params['prefix'] . $key;
$lifetime = $this->_getLifetime($lifetime);
if (xcache_set($key . '_expire', time(), $lifetime)) {
xcache_unset($key);
}
}
+
}
<api>beta</api>
</stability>
<license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Added Horde_Cache_Session::.
+ <notes>* Add option to transparently compress cache data using lzf.
+ * Added Horde_Cache_Session::.
* Horde_Cache::set() no longer returns a boolean result.
* Added Horde_Cache_Exception::.
* Removed dependency on Horde Core.
<name>Memcache</name>
<channel>pear.horde.org</channel>
</package>
+ <package>
+ <name>lzf</name>
+ <channel>pecl.php.net</channel>
+ <recommended>1.5.2</recommended>
+ </package>
<extension>
<name>apc</name>
</extension>