* @category Horde
* @copyright 1999-2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
*/
class Horde_Cache_Storage_File extends Horde_Cache_Storage_Base
{
/* Location of the garbage collection data file. */
const GC_FILE = 'horde_cache_gc';
/**
* List of key to filename mappings.
*
* @var array
*/
protected $_file = array();
/**
* Constructor.
*
* @param array $params Optional parameters:
*
* - dir: (string) The base directory to store the cache files in.
* DEFAULT: System default
* - no_gc: (boolean) If true, don't perform garbage collection.
* DEFAULT: false
* - prefix: (string) The filename prefix to use for the cache files.
* DEFAULT: 'cache_'
* - sub: (integer) If non-zero, the number of subdirectories to create
* to store the file (i.e. PHP's session.save_path).
* DEFAULT: 0
*
*/
public function __construct(array $params = array())
{
$params = array_merge(array(
'prefix' => 'cache_',
'sub' => 0
), $params);
if (!isset($params['dir']) || !@is_dir($params['dir'])) {
$params['dir'] = sys_get_temp_dir();
}
parent::__construct($params);
}
/**
* Destructor.
*/
public function __destruct()
{
$c_time = time();
/* Only do garbage collection 0.1% of the time we create an object. */
if (!empty($this->_params['no_gc']) ||
(intval(substr($c_time, -3)) !== 0)) {
return;
}
$filename = $this->_params['dir'] . '/' . self::GC_FILE;
$excepts = array();
if (is_readable($filename)) {
$gc_file = file($filename, FILE_IGNORE_NEW_LINES);
reset($gc_file);
next($gc_file);
while (list(,$data) = each($gc_file)) {
$parts = explode("\t", $data, 2);
$excepts[$parts[0]] = $parts[1];
}
}
foreach ($this->_getCacheFiles() as $fname => $pname) {
if (!empty($excepts[$fname]) &&
(($c_time - $excepts[$fname]) > filemtime($pname))) {
@unlink($pname);
unset($excepts[$fname]);
}
}
if ($fp = @fopen($filename, 'w')) {
foreach ($excepts as $key => $val) {
fwrite($fp, $key . "\t" . $val . "\n");
}
fclose($fp);
}
}
/**
*/
public function get($key, $lifetime = 0)
{
if (!$this->exists($key, $lifetime)) {
/* Nothing cached, return failure. */
return false;
}
$filename = $this->_keyToFile($key);
$size = filesize($filename);
return $size
? @file_get_contents($filename)
: '';
}
/**
*/
public function set($key, $data, $lifetime = 0)
{
$filename = $this->_keyToFile($key, true);
$tmp_file = Horde_Util::getTempFile('HordeCache', true, $this->_params['dir']);
if (isset($this->_params['umask'])) {
chmod($tmp_file, 0666 & ~$this->_params['umask']);
}
if (file_put_contents($tmp_file, $data) === false) {
throw new Horde_Cache_Exception('Cannot write to cache directory ' . $this->_params['dir']);
}
@rename($tmp_file, $filename);
if ($lifetime &&
($fp = @fopen($this->_params['dir'] . '/' . self::GC_FILE, 'a'))) {
// This may result in duplicate entries in GC_FILE, but we
// will take care of these whenever we do GC and this is quicker
// than having to check every time we access the file.
fwrite($fp, $filename . "\t" . (time() + $lifetime) . "\n");
fclose($fp);
}
}
/**
*/
public function exists($key, $lifetime = 0)
{
$filename = $this->_keyToFile($key);
/* Key exists in the cache */
if (file_exists($filename)) {
/* 0 means no expire.
* Also, If the file was been created after the supplied value,
* the data is valid (fresh). */
if (($lifetime == 0) ||
(time() - $lifetime <= filemtime($filename))) {
return true;
}
@unlink($filename);
}
return false;
}
/**
*/
public function expire($key)
{
return @unlink($this->_keyToFile($key));
}
/**
*/
public function clear()
{
foreach ($this->_getCacheFiles() as $val) {
@unlink($val);
}
@unlink($this->_params['dir'] . '/' . self::GC_FILE);
}
/**
* Return a list of cache files.
*
* @return array Pathnames to cache files.
*/
protected function _getCacheFiles()
{
$paths = array();
try {
$it = empty($this->_params['sub'])
? new DirectoryIterator($this->_params['dir'])
: new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->_params['dir']), RecursiveIteratorIterator::CHILD_FIRST);
} catch (UnexpectedValueException $e) {
return $paths;
}
foreach ($it as $val) {
if (!$val->isDir() &&
($fname = $val->getFilename()) &&
(strpos($fname, $this->_params['prefix']) === 0)) {
$paths[$fname] = $val->getPathname();
}
}
return $paths;
}
/**
* Map a cache key to a unique filename.
*
* @param string $key Cache key.
* @param string $create Create path if it doesn't exist?
*
* @return string Fully qualified filename.
*/
protected function _keyToFile($key, $create = false)
{
if ($create || !isset($this->_file[$key])) {
$dir = $this->_params['dir'] . '/';
$md5 = hash('md5', $key);
$sub = '';
if (!empty($this->_params['sub'])) {
$max = min($this->_params['sub'], strlen($md5));
for ($i = 0; $i < $max; $i++) {
$sub .= $md5[$i];
if ($create && !is_dir($dir . $sub)) {
if (!mkdir($dir . $sub)) {
$sub = '';
break;
}
}
$sub .= '/';
}
}
$this->_file[$key] = $dir . $sub . $this->_params['prefix'] . $md5;
}
return $this->_file[$key];
}
}
Horde_Cache-2.4.0/lib/Horde/Cache/Storage/Hashtable.php 0000644 0001750 0000764 00000004446 12270574403 022473 0 ustar slusarz slusarz
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
* @since 2.2.0
*/
class Horde_Cache_Storage_Hashtable extends Horde_Cache_Storage_Base
{
/**
* HashTable object.
*
* @var Horde_HashTable
*/
protected $_hash;
/**
* @param array $params Additional parameters:
*
* - hashtable: (Horde_HashTable) [REQUIRED] A Horde_HashTable object.
* - prefix: (string) The prefix to use for the cache keys.
* DEFAULT: ''
*
*/
public function __construct(array $params = array())
{
if (!isset($params['hashtable'])) {
throw new InvalidArgumentException('Missing hashtable parameter.');
}
if (isset($params['prefix'])) {
$params['hashtable'] = clone $params['hashtable'];
$params['hashtable']->prefix = $params['prefix'];
unset($params['prefix']);
}
parent::__construct($params);
}
/**
*/
protected function _initOb()
{
$this->_hash = $this->_params['hashtable'];
}
/**
* NOTE: This driver ignores the lifetime argument.
*/
public function get($key, $lifetime = 0)
{
return $this->_hash->get($key);
}
/**
*/
public function set($key, $data, $lifetime = 0)
{
$this->_hash->set($key, $data, array_filter(array(
'timeout' => $lifetime
)));
}
/**
* NOTE: This driver ignores the lifetime argument.
*/
public function exists($key, $lifetime = 0)
{
return $this->_hash->exists($key);
}
/**
*/
public function expire($key)
{
$this->_hash->delete($key);
}
/**
*/
public function clear()
{
$this->_hash->clear();
}
}
Horde_Cache-2.4.0/lib/Horde/Cache/Storage/Memcache.php 0000644 0001750 0000764 00000007001 12270574403 022270 0 ustar slusarz slusarz
* @author Michael Slusarz
* @category Horde
* @copyright 2006-2013 Horde LLC
* @deprecated Use HashTable driver instead.
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
*/
class Horde_Cache_Storage_Memcache extends Horde_Cache_Storage_Base
{
/**
* Cache results of expire() calls (since we will get the entire object
* on an expire() call anyway).
*
* @var array
*/
protected $_expirecache = array();
/**
* Memcache object.
*
* @var Horde_Memcache
*/
protected $_memcache;
/**
* Construct a new Horde_Cache_Memcache object.
*
* @param array $params Parameter array:
* - memcache: (Horde_Memcache) [REQUIRED] A Horde_Memcache object.
* - prefix: (string) The prefix to use for the cache keys.
* DEFAULT: ''
*/
public function __construct(array $params = array())
{
if (!isset($params['memcache'])) {
if (isset($params['hashtable'])) {
$params['memcache'] = $params['hashtable'];
} else {
throw new InvalidArgumentException('Missing memcache object');
}
}
parent::__construct(array_merge(array(
'prefix' => '',
), $params));
}
/**
*/
protected function _initOb()
{
$this->_memcache = $this->_params['memcache'];
}
/**
*/
public function get($key, $lifetime = 0)
{
$original_key = $key;
$key = $this->_params['prefix'] . $key;
if (isset($this->_expirecache[$key])) {
return $this->_expirecache[$key];
}
$key_list = array($key);
if (!empty($lifetime)) {
$key_list[] = $key . '_e';
}
$res = $this->_memcache->get($key_list);
if ($res === false) {
unset($this->_expirecache[$key]);
} else {
// If we can't find the expire time, assume we have exceeded it.
if (empty($lifetime) ||
(($res[$key . '_e'] !== false) && ($res[$key . '_e'] + $lifetime > time()))) {
$this->_expirecache[$key] = $res[$key];
} else {
$res[$key] = false;
$this->expire($original_key);
}
}
return $res[$key];
}
/**
*/
public function set($key, $data, $lifetime = 0)
{
$key = $this->_params['prefix'] . $key;
if ($this->_memcache->set($key . '_e', time(), $lifetime) !== false) {
$this->_memcache->set($key, $data, $lifetime);
}
}
/**
*/
public function exists($key, $lifetime = 0)
{
return ($this->get($key, $lifetime) !== false);
}
/**
*/
public function expire($key)
{
$key = $this->_params['prefix'] . $key;
unset($this->_expirecache[$key]);
$this->_memcache->delete($key . '_e');
return $this->_memcache->delete($key);
}
/**
*/
public function clear()
{
$this->_memcache->flush();
}
}
Horde_Cache-2.4.0/lib/Horde/Cache/Storage/Memoryoverlay.php 0000644 0001750 0000764 00000004461 12270574403 023447 0 ustar slusarz slusarz
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Cache
* @package Cache
*/
class Horde_Cache_Storage_Memoryoverlay extends Horde_Cache_Storage_Base
{
/**
* The memory cache.
*
* @var array
*/
private $_cache = array();
/**
* Constructor.
*
* @param array $params Parameters:
*
* - backend: (Horde_Cache_Storage_Base) [REQUIRED] The master storage
* backend.
*
*/
public function __construct(array $params = array())
{
if (!isset($params['backend'])) {
throw new InvalidArgumentException('Missing backend parameter.');
}
parent::__construct($params);
}
/**
*/
public function get($key, $lifetime = 0)
{
if (!isset($this->_cache[$key])) {
$this->_cache[$key] = $this->_params['backend']->get($key, $lifetime);
}
return $this->_cache[$key];
}
/**
*/
public function set($key, $data, $lifetime = 0)
{
$this->_cache[$key] = $data;
$this->_params['backend']->set($key, $data, $lifetime);
}
/**
*/
public function exists($key, $lifetime = 0)
{
return isset($this->_cache[$key])
? true
: $this->_params['backend']->exists($key, $lifetime);
}
/**
*/
public function expire($key)
{
unset($this->_cache[$key]);
$this->_params['backend']->expire($key);
}
/**
*/
public function clear()
{
$this->_cache = array();
$this->_params['backend']->clear();
}
}
Horde_Cache-2.4.0/lib/Horde/Cache/Storage/Mock.php 0000644 0001750 0000764 00000003254 12270574403 021465 0 ustar slusarz slusarz
* @category Horde
* @copyright 2010-2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Cache
* @package Cache
*/
class Horde_Cache_Storage_Mock extends Horde_Cache_Storage_Base
{
/**
* The storage location for this cache.
*
* @var array
*/
private $_cache = array();
/**
*/
public function __sleep()
{
throw new BadMethodCallException('Cannot serialize this object.');
}
/**
*/
public function get($key, $lifetime = 0)
{
return isset($this->_cache[$key])
? $this->_cache[$key]
: false;
}
/**
*/
public function set($key, $data, $lifetime = 0)
{
$this->_cache[$key] = $data;
}
/**
*/
public function exists($key, $lifetime = 0)
{
return isset($this->_cache[$key]);
}
/**
*/
public function expire($key)
{
unset($this->_cache[$key]);
}
/**
*/
public function clear()
{
$this->_cache = array();
}
}
Horde_Cache-2.4.0/lib/Horde/Cache/Storage/Mongo.php 0000644 0001750 0000764 00000014146 12270574403 021655 0 ustar slusarz slusarz
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
*/
class Horde_Cache_Storage_Mongo extends Horde_Cache_Storage_Base
{
/* Field names. */
const CID = 'cid';
const DATA = 'data';
const EXPIRE = 'expire';
const TIMESTAMP = 'ts';
/**
* The MongoDB Collection object for the cache data.
*
* @var MongoCollection
*/
protected $_db;
/**
* Constructor.
*
* @param array $params Parameters:
* - collection: (string) The collection name.
* - mongo_db: [REQUIRED] (Horde_Mongo_Client) A MongoDB client object.
*/
public function __construct(array $params = array())
{
if (!isset($params['mongo_db'])) {
throw new InvalidArgumentException('Missing mongo_db parameter.');
}
parent::__construct(array_merge(array(
'collection' => 'horde_cache'
), $params));
}
/**
* Destructor.
*/
public function __destruct()
{
/* Only do garbage collection 0.1% of the time we create an object. */
if (rand(0, 999) == 0) {
try {
$this->_db->remove(array(
self::EXPIRE => array(
'$exists' => true,
'$lt' => time()
)
));
} catch (MongoException $e) {
$this->_logger->log($e->getMessage(), 'DEBUG');
}
}
}
/**
*/
protected function _initOb()
{
$this->_db = $this->_params['mongo_db']->selectCollection(null, $this->_params['collection']);
}
/**
*/
public function get($key, $lifetime = 0)
{
$okey = $key;
$key = $this->_getCid($key);
/* Build SQL query. */
$query = array(
self::CID => $key
);
// 0 lifetime checks for objects which have no expiration
if ($lifetime != 0) {
$query[self::TIMESTAMP] = array('$gte' => time() - $lifetime);
}
try {
$result = $this->_db->findOne($query, array(self::DATA));
} catch (MongoException $e) {
$this->_logger->log($e->getMessage(), 'DEBUG');
return false;
}
if (empty($result)) {
/* No rows were found - cache miss */
if ($this->_logger) {
$this->_logger->log(sprintf('Cache miss: %s (cache ID %s)', $okey, $key), 'DEBUG');
}
return false;
}
if ($this->_logger) {
$this->_logger->log(sprintf('Cache hit: %s (cache ID %s)', $okey, $key), 'DEBUG');
}
return $result[self::DATA]->bin;
}
/**
*/
public function set($key, $data, $lifetime = 0)
{
$okey = $key;
$key = $this->_getCid($key);
$curr = time();
$data = array(
self::CID => $key,
self::DATA => new MongoBinData($data, MongoBinData::BYTE_ARRAY),
self::TIMESTAMP => $curr
);
// 0 lifetime indicates the object should not be GC'd.
if (!empty($lifetime)) {
$data[self::EXPIRE] = intval($lifetime) + $curr;
}
if ($this->_logger) {
$this->_logger->log(sprintf(
'Cache set: %s (id %s set at %s%s)',
$okey,
$key,
date('r', $curr),
(isset($data[self::EXPIRE]) ? ' expires at ' . date('r', $data[self::EXPIRE]) : '')
), 'DEBUG');
}
// Remove any old cache data and prevent duplicate keys
try {
$this->_db->update(array(
self::CID => $key
), array(
'$set' => $data
), array(
'upsert' => true,
'w' => 0
));
} catch (MongoException $e) {
$this->_logger->log($e->getMessage(), 'DEBUG');
return false;
}
}
/**
*/
public function exists($key, $lifetime = 0)
{
$okey = $key;
$key = $this->_getCid($key);
/* Build SQL query. */
$query = array(
self::CID => $key
);
// 0 lifetime checks for objects which have no expiration
if ($lifetime != 0) {
$query[self::TIMESTAMP] = array('$gte' => time() - $lifetime);
}
try {
$result = $this->_db->findOne($query);
} catch (MongoException $e) {
$this->_logger->log($e->getMessage(), 'DEBUG');
return false;
}
if (is_null($result)) {
if ($this->_logger) {
$this->_logger->log(sprintf('Cache exists() miss: %s (cache ID %s)', $okey, $key), 'DEBUG');
}
return false;
}
if ($this->_logger) {
$this->_logger->log(sprintf('Cache exists() hit: %s (cache ID %s)', $okey, $key), 'DEBUG');
}
return true;
}
/**
*/
public function expire($key)
{
$okey = $key;
$key = $this->_getCid($key);
try {
$this->_db->remove(array(
self::CID => $key
));
$this->_logger->log(sprintf('Cache expire: %s (cache ID %s)', $okey, $key), 'DEBUG');
return true;
} catch (MongoException $e) {
return false;
}
}
/**
*/
public function clear()
{
$this->_db->drop();
}
/**
* Gets the cache ID for a key.
*
* @param string $key The key.
*
* @return string The cache ID.
*/
protected function _getCid($key)
{
return hash('md5', $key);
}
}
Horde_Cache-2.4.0/lib/Horde/Cache/Storage/Null.php 0000644 0001750 0000764 00000002034 12270574403 021501 0 ustar slusarz slusarz
* @category Horde
* @copyright 2006-2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
*/
class Horde_Cache_Storage_Null extends Horde_Cache_Storage_Base
{
/**
*/
public function get($key, $lifetime = 0)
{
return false;
}
/**
*/
public function set($key, $data, $lifetime = 0)
{
}
/**
*/
public function exists($key, $lifetime = 0)
{
return false;
}
/**
*/
public function expire($key)
{
return false;
}
/**
*/
public function clear()
{
}
}
Horde_Cache-2.4.0/lib/Horde/Cache/Storage/Session.php 0000644 0001750 0000764 00000004704 12270574403 022220 0 ustar slusarz slusarz
* @category Horde
* @copyright 2010-2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
*/
class Horde_Cache_Storage_Session extends Horde_Cache_Storage_Base
{
/**
* Pointer to the session entry.
*
* @var array
*/
protected $_sess;
/**
* Constructor.
*
* @param array $params Optional parameters:
* - session: (string) Store session data in this entry.
* DEFAULT: 'horde_cache_session'
*/
public function __construct(array $params = array())
{
$params = array_merge(array(
'sess_name' => 'horde_cache_session'
), $params);
parent::__construct($params);
}
/**
* Do initialization tasks.
*/
protected function _initOb()
{
if (!isset($_SESSION[$this->_params['sess_name']])) {
$_SESSION[$this->_params['sess_name']] = array();
}
$this->_sess = &$_SESSION[$this->_params['sess_name']];
}
/**
*/
public function get($key, $lifetime = 0)
{
return $this->exists($key, $lifetime)
? $this->_sess[$key]['d']
: false;
}
/**
*/
public function set($key, $data, $lifetime = 0)
{
$this->_sess[$key] = array(
'd' => $data,
'l' => $lifetime
);
}
/**
*/
public function exists($key, $lifetime = 0)
{
if (isset($this->_sess[$key])) {
/* 0 means no expire. */
if (($lifetime == 0) ||
((time() - $lifetime) <= $this->_sess[$key]['l'])) {
return true;
}
unset($this->_sess[$key]);
}
return false;
}
/**
*/
public function expire($key)
{
if (isset($this->_sess[$key])) {
unset($this->_sess[$key]);
return true;
}
return false;
}
/**
*/
public function clear()
{
$this->_sess = array();
}
}
Horde_Cache-2.4.0/lib/Horde/Cache/Storage/Sql.php 0000644 0001750 0000764 00000014621 12270574403 021333 0 ustar slusarz slusarz
* CREATE TABLE horde_cache (
* cache_id VARCHAR(32) NOT NULL,
* cache_timestamp BIGINT NOT NULL,
* cache_data LONGBLOB,
* (Or on PostgreSQL:)
* cache_data TEXT,
* (Or on some other DBMS systems:)
* cache_data IMAGE,
*
* PRIMARY KEY (cache_id)
* );
*
*
* @author Ben Klang
* @author Michael Slusarz
* @category Horde
* @copyright 2007-2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
*/
class Horde_Cache_Storage_Sql extends Horde_Cache_Storage_Base
{
/**
* Handle for the current database connection.
*
* @var Horde_Db_Adapter
*/
protected $_db;
/**
* Constructor.
*
* @param array $params Parameters:
* - db: (Horde_Db_Adapter) [REQUIRED] The DB instance.
* - table: (string) The name of the cache table.
* DEFAULT: 'horde_cache'
*/
public function __construct($params = array())
{
if (!isset($params['db'])) {
throw new InvalidArgumentException('Missing db parameter.');
}
parent::__construct(array_merge(array(
'table' => 'horde_cache',
), $params));
}
/**
*/
protected function _initOb()
{
$this->_db = $this->_params['db'];
}
/**
* Destructor.
*/
public function __destruct()
{
/* Only do garbage collection 0.1% of the time we create an object. */
if (rand(0, 999) != 0) {
return;
}
$query = 'DELETE FROM ' . $this->_params['table'] .
' WHERE cache_expiration < ? AND cache_expiration <> 0';
$values = array(time());
try {
$this->_db->delete($query, $values);
} catch (Horde_Db_Exception $e) {}
}
/**
*/
public function get($key, $lifetime = 0)
{
$okey = $key;
$key = hash('md5', $key);
$timestamp = time();
$maxage = $timestamp - $lifetime;
/* Build SQL query. */
$query = 'SELECT cache_data FROM ' . $this->_params['table'] .
' WHERE cache_id = ?';
$values = array($key);
// 0 lifetime checks for objects which have no expiration
if ($lifetime != 0) {
$query .= ' AND cache_timestamp >= ?';
$values[] = $maxage;
}
try {
$result = $this->_db->selectValue($query, $values);
} catch (Horde_Db_Exception $e) {
return false;
}
if (!$result) {
/* No rows were found - cache miss */
if ($this->_logger) {
$this->_logger->log(sprintf('Cache miss: %s (Id %s newer than %d)', $okey, $key, $maxage), 'DEBUG');
}
return false;
}
if ($this->_logger) {
$this->_logger->log(sprintf('Cache hit: %s (Id %s newer than %d)', $okey, $key, $maxage), 'DEBUG');
}
return $result;
}
/**
*/
public function set($key, $data, $lifetime = 0)
{
$okey = $key;
$key = hash('md5', $key);
$timestamp = time();
// 0 lifetime indicates the object should not be GC'd.
$expiration = ($lifetime === 0)
? 0
: ($lifetime + $timestamp);
if ($this->_logger) {
$this->_logger->log(sprintf('Cache set: %s (Id %s set at %d expires at %d)', $okey, $key, $timestamp, $expiration), 'DEBUG');
}
// Remove any old cache data and prevent duplicate keys
$query = 'DELETE FROM ' . $this->_params['table'] . ' WHERE cache_id=?';
$values = array($key);
try {
$this->_db->delete($query, $values);
} catch (Horde_Db_Exception $e) {}
/* Build SQL query. */
$query = 'INSERT INTO ' . $this->_params['table'] .
' (cache_id, cache_timestamp, cache_expiration, cache_data)' .
' VALUES (?, ?, ?, ?)';
$values = array($key, $timestamp, $expiration, $data);
try {
$this->_db->insert($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Horde_Cache_Exception($e);
}
}
/**
*/
public function exists($key, $lifetime = 0)
{
$okey = $key;
$key = hash('md5', $key);
/* Build SQL query. */
$query = 'SELECT 1 FROM ' . $this->_params['table'] .
' WHERE cache_id = ?';
$values = array($key);
// 0 lifetime checks for objects which have no expiration
if ($lifetime != 0) {
$query .= ' AND cache_timestamp >= ?';
$values[] = time() - $lifetime;
}
try {
$result = $this->_db->selectValue($query, $values);
} catch (Horde_Db_Exception $e) {
return false;
}
$timestamp = time();
if (empty($result)) {
if ($this->_logger) {
$this->_logger->log(sprintf('Cache exists() miss: %s (Id %s newer than %d)', $okey, $key, $timestamp), 'DEBUG');
}
return false;
}
if ($this->_logger) {
$this->_logger->log(sprintf('Cache exists() hit: %s (Id %s newer than %d)', $okey, $key, $timestamp), 'DEBUG');
}
return true;
}
/**
*/
public function expire($key)
{
$key = hash('md5', $key);
$query = 'DELETE FROM ' . $this->_params['table'] .
' WHERE cache_id = ?';
$values = array($key);
try {
$this->_db->delete($query, $values);
} catch (Horde_Db_Exception $e) {
return false;
}
return true;
}
/**
*/
public function clear()
{
$query = 'DELETE FROM ' . $this->_params['table'];
try {
$this->_db->delete($query);
} catch (Horde_Db_Exception $e) {
throw new Horde_Cache_Exception($e);
}
}
}
Horde_Cache-2.4.0/lib/Horde/Cache/Storage/Stack.php 0000644 0001750 0000764 00000007055 12270574403 021644 0 ustar slusarz slusarz
* @category Horde
* @copyright 2010-2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
*/
class Horde_Cache_Storage_Stack extends Horde_Cache_Storage_Base
{
/**
* Stack of cache drivers.
*
* @var string
*/
protected $_stack = array();
/**
* Constructor.
*
* @param array $params Parameters:
* - stack: (array) [REQUIRED] An array of storage instances to loop
* through, in order of priority. The last entry is considered
* the 'master' driver, for purposes of writes.
*/
public function __construct(array $params = array())
{
if (!isset($params['stack'])) {
throw new InvalidArgumentException('Missing stack parameter.');
}
parent::__construct($params);
}
/**
*/
protected function _initOb()
{
$this->_stack = $this->_params['stack'];
}
/**
*/
public function get($key, $lifetime = 0)
{
foreach ($this->_stack as $val) {
$result = $val->get($key, $lifetime);
if ($result !== false) {
return $result;
}
}
return false;
}
/**
*/
public function set($key, $data, $lifetime = 0)
{
/* Do writes in *reverse* order - it is OK if a write to one of the
* non-master backends fails. */
$master = true;
foreach (array_reverse($this->_stack) as $val) {
$result = $val->set($key, $data, $lifetime);
if ($result === false) {
if ($master) {
return;
}
/* Attempt to invalidate cache if write failed. */
$val->expire($key);
}
$master = false;
}
}
/**
*/
public function exists($key, $lifetime = 0)
{
foreach ($this->_stack as $val) {
$result = $val->exists($key, $lifetime);
if ($result === true) {
break;
}
}
return $result;
}
/**
*/
public function expire($key)
{
/* Only report success from master. */
$master = $success = true;
foreach (array_reverse($this->_stack) as $val) {
$result = $val->expire($key);
if ($master && ($result === false)) {
$success = false;
}
$master = false;
}
return $success;
}
/**
*/
public function clear()
{
/* Only report errors from master. */
$exception = null;
$master = true;
foreach (array_reverse($this->_stack) as $val) {
try {
$val->clear();
} catch (Horde_Cache_Exception $e) {
if ($master) {
$exception = $e;
}
}
$master = false;
}
if ($exception) {
throw $exception;
}
}
}
Horde_Cache-2.4.0/lib/Horde/Cache/Storage/Xcache.php 0000644 0001750 0000764 00000005211 12270574403 021762 0 ustar slusarz slusarz
* @category Horde
* @copyright 2006-2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
*/
class Horde_Cache_Storage_Xcache extends Horde_Cache_Storage_Base
{
/**
* Constructor.
*
* @param array $params Optional parameters:
* - prefix: (string) The prefix to use for the cache keys.
* DEFAULT: ''
*/
public function __construct(array $params = array())
{
parent::__construct(array_merge(array(
'prefix' => '',
), $params));
}
/**
*/
public function get($key, $lifetime = 0)
{
$key = $this->_params['prefix'] . $key;
$this->_setExpire($key, $lifetime);
$result = xcache_get($key);
return empty($result)
? false
: $result;
}
/**
*/
public function set($key, $data, $lifetime = 0)
{
$key = $this->_params['prefix'] . $key;
if (xcache_set($key . '_expire', time(), $lifetime)) {
xcache_set($key, $data, $lifetime);
}
}
/**
*/
public function exists($key, $lifetime = 0)
{
$key = $this->_params['prefix'] . $key;
$this->_setExpire($key, $lifetime);
return xcache_isset($key);
}
/**
*/
public function expire($key)
{
$key = $this->_params['prefix'] . $key;
xcache_unset($key . '_expire');
return xcache_unset($key);
}
/**
*/
public function clear()
{
// xcache_clear_cache() won't work because it requires HTTP Auth.
throw new Horde_Cache_Exception('Not supported');
}
/**
* Set expire time on each call since xcache sets it on cache creation.
*
* @param string $key Cache key to expire.
* @param integer $lifetime Lifetime of the data in seconds.
*/
protected function _setExpire($key, $lifetime)
{
if ($lifetime == 0) {
// don't expire
return;
}
$expire = xcache_get($key . '_expire');
// set prune period
if ($expire + $lifetime < time()) {
// Expired
xcache_unset($key . '_expire');
xcache_unset($key);
}
}
}
Horde_Cache-2.4.0/lib/Horde/Cache/Exception.php 0000644 0001750 0000764 00000000651 12270574403 021124 0 ustar slusarz slusarz
* @category Horde
* @package Cache
*/
class Horde_Cache_Exception extends Horde_Exception_Wrapped
{
}
Horde_Cache-2.4.0/lib/Horde/Cache.php 0000644 0001750 0000764 00000011607 12270574403 017171 0 ustar slusarz slusarz
* @author Chuck Hagenbuch
* @author Michael Slusarz
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
*/
class Horde_Cache
{
/**
* Cache parameters.
*
* @var array
*/
protected $_params = array(
'compress' => false,
'lifetime' => 86400
);
/**
* Logger.
*
* @var Horde_Log_Logger
*/
protected $_logger;
/**
* Storage object.
*
* @var Horde_Cache_Storage
*/
protected $_storage;
/**
* Constructor.
*
* @param Horde_Cache_Storage $storage The storage object.
* @param array $params Parameter array:
* - compress: (boolean) Compress data (if possible)?
* DEFAULT: false
* - lifetime: (integer) Lifetime of data, in seconds.
* DEFAULT: 86400 seconds
* - logger: (Horde_Log_Logger) Log object to use for log/debug messages.
*/
public function __construct(Horde_Cache_Storage_Base $storage,
array $params = array())
{
if (isset($params['logger'])) {
$this->_logger = $params['logger'];
unset($params['logger']);
$storage->setLogger($this->_logger);
}
$this->_params = array_merge($this->_params, $params);
$this->_storage = $storage;
}
/**
* Attempts to directly output a cached object.
*
* @param string $key Object ID to query.
* @param integer $lifetime Lifetime of the object in seconds.
*
* @return boolean True if output or false if no object was found.
*/
public function output($key, $lifetime = 1)
{
$data = $this->get($key, $lifetime);
if ($data === false) {
return false;
}
echo $data;
return true;
}
/**
* 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.
*/
public function get($key, $lifetime = 1)
{
$res = $this->_storage->get($key, $lifetime);
if (empty($this->_params['compress']) || !is_string($res)) {
return $res;
}
$compress = new Horde_Compress_Fast();
return $compress->decompress($res);
}
/**
* 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.
*/
public function set($key, $data, $lifetime = null)
{
if (!empty($this->_params['compress'])) {
$compress = new Horde_Compress_Fast();
$data = $compress->compress($data);
}
$lifetime = is_null($lifetime)
? $this->_params['lifetime']
: $lifetime;
$this->_storage->set($key, $data, $lifetime);
}
/**
* Checks if a given key exists in the cache, valid for the given
* lifetime.
*
* @param string $key Cache key to check.
* @param integer $lifetime Lifetime of the key in seconds.
*
* @return boolean Existence.
*/
public function exists($key, $lifetime = 1)
{
return $this->_storage->exists($key, $lifetime);
}
/**
* Expire any existing data for the given key.
*
* @param string $key Cache key to expire.
*
* @return boolean Success or failure.
*/
public function expire($key)
{
return $this->_storage->expire($key);
}
/**
* Clears all data from the cache.
*
* @throws Horde_Cache_Exception
*/
public function clear()
{
return $this->_storage->clear();
}
/**
* Tests the driver for read/write access.
*
* @return boolean True if read/write is available.
*/
public function testReadWrite()
{
$key = '__horde_cache_testkey';
try {
$this->_storage->set($key, 1);
if ($this->_storage->exists($key)) {
$this->_storage->expire($key);
return true;
}
} catch (Exception $e) {}
return false;
}
}
Horde_Cache-2.4.0/migration/Horde/Cache/1_horde_cache_base_tables.php 0000644 0001750 0000764 00000001324 12270574403 025377 0 ustar slusarz slusarz tables())) {
$t = $this->createTable('horde_cache', array('autoincrementKey' => array('cache_id')));
$t->column('cache_id', 'string', array('limit' => 32, 'null' => false));
$t->column('cache_timestamp', 'bigint', array('null' => false));
$t->column('cache_expiration', 'bigint', array('null' => false));
$t->column('cache_data', 'binary');
$t->end();
}
}
public function down()
{
$this->dropTable('horde_cache');
}
}
Horde_Cache-2.4.0/migration/Horde/Cache/2_horde_cache_fix_blob_length.php 0000644 0001750 0000764 00000000331 12270574403 026256 0 ustar slusarz slusarz changeColumn('horde_cache', 'cache_data', 'binary');
}
public function down()
{
}
}