<?php
/**
- * The Horde_Cache:: class provides a common abstracted interface into
- * the various caching backends. It also provides functions for
- * checking in, retrieving, and flushing a cache.
+ * The Horde_Cache:: class provides the abstract class definition for
+ * Horde_Cache drivers.
*
* Copyright 1999-2010 The Horde Project (http://www.horde.org/)
*
*
* @author Anil Madhavapeddy <anil@recoil.org>
* @author Chuck Hagenbuch <chuck@horde.org>
+ * @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @package Cache
*/
-class Horde_Cache
+abstract class Horde_Cache
{
/**
- * Attempts to return a concrete instance based on $driver.
+ * Cache parameters.
*
- * @param string $driver The type of concrete subclass to
- * return. The class name is based on the storage
- * driver ($driver). The code is dynamically
- * included.
- * @param array $params A hash containing any additional configuration
- * or connection parameters a subclass might need.
+ * @var array
+ */
+ protected $_params = array(
+ 'lifetime' => 86400,
+ 'prefix' => '',
+ );
+
+ /**
+ * Logger.
*
- * @return Horde_Cache_Base The newly created concrete instance.
- * @throws Horde_Cache_Exception
+ * @var Horde_Log_Logger
*/
- static public function factory($driver, array $params = array())
+ protected $_logger;
+
+ /**
+ * Construct a new Horde_Cache object.
+ *
+ * @param array $params Parameter array:
+ * <pre>
+ * 'lifetime' - (integer) Lifetime of data, in seconds.
+ * DEFAULT: 86400 seconds
+ * 'logger' - (Horde_Log_Logger) Log object to use for log/debug messages.
+ * </pre>
+ */
+ public function __construct($params = array())
{
- $driver = ucfirst(basename($driver));
- $class = __CLASS__ . '_' . $driver;
+ if (isset($params['logger'])) {
+ $this->_logger = $params['logger'];
+ unset($params['logger']);
+ }
+
+ $this->_params = array_merge($this->_params, $params);
+ }
- if (!class_exists($class)) {
- $class = __CLASS__ . '_Null';
+ /**
+ * 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;
}
- return new $class($params);
+ echo $data;
+ 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.
+ */
+ abstract public function get($key, $lifetime = 1);
+
+ /**
+ * Attempts to 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.
+ * @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
+ */
+ abstract public function set($key, $data, $lifetime = null);
+
+ /**
+ * 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.
+ */
+ abstract public function exists($key, $lifetime = 1);
+
+ /**
+ * Expire any existing data for the given key.
+ *
+ * @param string $key Cache key to expire.
+ *
+ * @return boolean Success or failure.
+ */
+ abstract public function expire($key);
+
+ /**
+ * Determine the default lifetime for data.
+ *
+ * @param mixed $lifetime The lifetime to use or null for default.
+ *
+ * @return integer The lifetime, in seconds.
+ */
+ protected function _getLifetime($lifetime)
+ {
+ return is_null($lifetime) ? $this->_params['lifetime'] : $lifetime;
+ }
}
* @category Horde
* @package Cache
*/
-class Horde_Cache_Apc extends Horde_Cache_Base
+class Horde_Cache_Apc extends Horde_Cache
{
/**
* Attempts to retrieve a piece of cached data and return it to
apc_delete($key . '_expire');
}
}
-
}
+++ /dev/null
-<?php
-/**
- * The Horde_Cache_Base:: class provides the abstract class definition for
- * Horde_Cache drivers.
- *
- * Copyright 1999-2010 The Horde Project (http://www.horde.org/)
- *
- * See the enclosed file COPYING for license information (LGPL). If you
- * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
- *
- * @author Anil Madhavapeddy <anil@recoil.org>
- * @author Chuck Hagenbuch <chuck@horde.org>
- * @author Michael Slusarz <slusarz@horde.org>
- * @category Horde
- * @package Cache
- */
-abstract class Horde_Cache_Base
-{
- /**
- * Cache parameters.
- *
- * @var array
- */
- protected $_params = array(
- 'lifetime' => 86400,
- 'prefix' => '',
- );
-
- /**
- * Logger.
- *
- * @var Horde_Log_Logger
- */
- protected $_logger;
-
- /**
- * Construct a new Horde_Cache object.
- *
- * @param array $params Parameter array:
- * <pre>
- * 'lifetime' - (integer) Lifetime of data, in seconds.
- * DEFAULT: 86400 seconds
- * 'logger' - (Horde_Log_Logger) Log object to use for log/debug messages.
- * </pre>
- */
- public function __construct($params = array())
- {
- if (isset($params['logger'])) {
- $this->_logger = $params['logger'];
- unset($params['logger']);
- }
-
- $this->_params = array_merge($this->_params, $params);
- }
-
- /**
- * 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;
- }
-
- /**
- * 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.
- */
- abstract public function get($key, $lifetime = 1);
-
- /**
- * Attempts to 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.
- * @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
- */
- abstract public function set($key, $data, $lifetime = null);
-
- /**
- * 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.
- */
- abstract public function exists($key, $lifetime = 1);
-
- /**
- * Expire any existing data for the given key.
- *
- * @param string $key Cache key to expire.
- *
- * @return boolean Success or failure.
- */
- abstract public function expire($key);
-
- /**
- * Determine the default lifetime for data.
- *
- * @param mixed $lifetime The lifetime to use or null for default.
- *
- * @return integer The lifetime, in seconds.
- */
- protected function _getLifetime($lifetime)
- {
- return is_null($lifetime) ? $this->_params['lifetime'] : $lifetime;
- }
-
-}
* @category Horde
* @package Cache
*/
-class Horde_Cache_Eaccelerator extends Horde_Cache_Base
+class Horde_Cache_Eaccelerator extends Horde_Cache
{
- /**
- * Construct a new Horde_Cache object.
- *
- * @param array $params Parameter array.
- * @throws Horde_Cache_Exception
- */
+ /**
+ * Construct a new Horde_Cache object.
+ *
+ * @param array $params Parameter array.
+ * @throws Horde_Cache_Exception
+ */
public function __construct($params = array())
{
if (!function_exists('eaccelerator_gc')) {
eaccelerator_rm($key . '_expire');
}
}
-
}
* @category Horde
* @package Cache
*/
-class Horde_Cache_File extends Horde_Cache_Base
+class Horde_Cache_File extends Horde_Cache
{
/**
* The location of the temp directory.
}
$d->close();
}
-
}
-
* @category Horde
* @package Cache
*/
-class Horde_Cache_Memcache extends Horde_Cache_Base
+class Horde_Cache_Memcache extends Horde_Cache
{
/**
* Memcache object.
return $this->_memcache->delete($key);
}
-
}
* @license http://www.fsf.org/copyleft/lgpl.html LGPL
* @link http://pear.horde.org/index.php?package=Cache
*/
-class Horde_Cache_Mock extends Horde_Cache_Base
+class Horde_Cache_Mock extends Horde_Cache
{
/**
* The storage location for this cache.
* @category Horde
* @package Cache
*/
-class Horde_Cache_Null extends Horde_Cache_Base
+class Horde_Cache_Null extends Horde_Cache
{
/**
* Attempts to retrieve a piece of cached data and return it to
{
return false;
}
-
}
* @license http://www.fsf.org/copyleft/lgpl.html LGPL
* @package Cache
*/
-class Horde_Cache_Session extends Horde_Cache_Base
+class Horde_Cache_Session extends Horde_Cache
{
/**
* Pointer to the session entry.
return false;
}
-
}
* @category Horde
* @package Cache
*/
-class Horde_Cache_Sql extends Horde_Cache_Base
+class Horde_Cache_Sql extends Horde_Cache
{
/**
* Handle for the current database connection.
return true;
}
-
}
* @category Horde
* @package Cache
*/
-class Horde_Cache_Stack extends Horde_Cache_Base
+class Horde_Cache_Stack extends Horde_Cache
{
/**
* Stack of cache drivers.
*
* @param array $params Parameters:
* <pre>
- * 'stack' - (array) [REQUIRED] A list of cache drivers to loop
+ * 'stack' - (array) [REQUIRED] An array of Cache instances to loop
* through, in order of priority. The last entry is considered
* the 'master' driver, for purposes of writes.
- * Each value should contain an array with two keys: 'driver', a
- * string value with the Cache driver to use, and 'params',
- * containing any parameters needed by this driver.
* </pre>
*
* @throws InvalidArgumentException
if (!isset($params['stack'])) {
throw new InvalidArgumentException('Missing stack parameter.');
}
-
- foreach ($params['stack'] as $val) {
- $this->_stack[] = Horde_Cache::factory($val['driver'], $val['params']);
- }
+ $this->_stack[] = $params['stack'];
unset($params['stack']);
-
parent::__construct($params);
}
return $success;
}
-
}
* @category Horde
* @package Cache
*/
-class Horde_Cache_Xcache extends Horde_Cache_Base
+class Horde_Cache_Xcache extends Horde_Cache
{
/**
* Attempts to retrieve a piece of cached data and return it to the caller.
xcache_unset($key);
}
}
-
}
</stability>
<license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
<notes>* Added Horde_Cache_Session::.
- * Horde_Cache_Base::set() no longer returns a boolean result.
+ * Horde_Cache::set() no longer returns a boolean result.
* Added Horde_Cache_Exception::.
* Removed dependency on Horde Core.
* Initial Horde 4 package.</notes>
<dir name="Horde">
<dir name="Cache">
<file name="Apc.php" role="php" />
- <file name="Base.php" role="php" />
<file name="Eaccelerator.php" role="php" />
<file name="Exception.php" role="php" />
<file name="File.php" role="php" />
<phprelease>
<filelist>
<install name="lib/Horde/Cache/Apc.php" as="Horde/Cache/Apc.php" />
- <install name="lib/Horde/Cache/Base.php" as="Horde/Cache/Base.php" />
<install name="lib/Horde/Cache/Eaccelerator.php" as="Horde/Cache/Eaccelerator.php" />
<install name="lib/Horde/Cache/Exception.php" as="Horde/Cache/Exception.php" />
<install name="lib/Horde/Cache/File.php" as="Horde/Cache/File.php" />
+++ /dev/null
-<?php
-/**
- * @category Horde
- * @package Core
- */
-class Horde_Core_Binder_Cache implements Horde_Injector_Binder
-{
- public function create(Horde_Injector $injector)
- {
- $cache = new Horde_Core_Factory_Cache($injector);
- return $cache->getCache();
- }
-
- public function equals(Horde_Injector_Binder $binder)
- {
- return false;
- }
-
-}
+++ /dev/null
-<?php
-/**
- * @category Horde
- * @package Core
- */
-class Horde_Core_Binder_CacheFactory implements Horde_Injector_Binder
-{
- public function create(Horde_Injector $injector)
- {
- return new Horde_Core_Factory_Cache($injector);
- }
-
- public function equals(Horde_Injector_Binder $binder)
- {
- return false;
- }
-
-}
class Horde_Core_Factory_Cache
{
/**
- * The injector.
- *
- * @var Horde_Injector
- */
- private $_injector;
-
- /**
- * Singleton instances.
- *
- * @var array
- */
- private $_instances = array();
-
- /**
- * Constructor.
- *
- * @param Horde_Injector $injector The injector to use.
- */
- public function __construct(Horde_Injector $injector)
- {
- $this->_injector = $injector;
- }
-
- /**
* Return the Horde_Cache:: instance.
*
* @param array $opts Options:
* @return Horde_Cache_Base The singleton instance.
* @throws Horde_Cache_Exception
*/
- public function getCache(array $opts = array())
+ public function create(Horde_Injector $injector)
{
$driver = empty($GLOBALS['conf']['cache']['driver'])
? 'Null'
$driver = 'Null';
}
- if (($driver == 'Null') && !empty($opts['session'])) {
- $driver = 'Session';
+ $params = Horde::getDriverConfig('cache', $driver);
+ if (isset($GLOBALS['conf']['cache']['default_lifetime'])) {
+ $params['lifetime'] = $GLOBALS['conf']['cache']['default_lifetime'];
}
- if (!isset($this->_instances[$driver])) {
- $params = Horde::getDriverConfig('cache', $driver);
- if (isset($GLOBALS['conf']['cache']['default_lifetime'])) {
- $params['lifetime'] = $GLOBALS['conf']['cache']['default_lifetime'];
- }
+ $logger = $injector->getInstance('Horde_Log_Logger');
+ $params['logger'] = $logger;
- $logger = $this->_injector->getInstance('Horde_Log_Logger');
- $params['logger'] = $logger;
+ $base_params = $params;
- $base_params = $params;
+ if (strcasecmp($driver, 'Memcache') === 0) {
+ $params['memcache'] = $injector->getInstance('Horde_Memcache');
+ } elseif (strcasecmp($driver, 'Sql') === 0) {
+ $params['db'] = $injector->getInstance('Horde_Db')->getDb('horde', 'cache');
+ }
- if (strcasecmp($driver, 'Memcache') === 0) {
- $params['memcache'] = $this->_injector->getInstance('Horde_Memcache');
- } elseif (strcasecmp($driver, 'Sql') === 0) {
- $params['db'] = $this->_injector->getInstance('Horde_Db')->getDb('horde', 'cache');
+ if (!empty($GLOBALS['conf']['cache']['use_memorycache']) &&
+ ((strcasecmp($driver, 'Sql') === 0) ||
+ (strcasecmp($driver, 'File') === 0))) {
+ if (strcasecmp($GLOBALS['conf']['cache']['use_memorycache'], 'Memcache') === 0) {
+ $base_params['memcache'] = $injector->getInstance('Horde_Memcache');
}
- if (!empty($GLOBALS['conf']['cache']['use_memorycache']) &&
- ((strcasecmp($driver, 'Sql') === 0) ||
- (strcasecmp($driver, 'File') === 0))) {
- if (strcasecmp($GLOBALS['conf']['cache']['use_memorycache'], 'Memcache') === 0) {
- $base_params['memcache'] = $this->_injector->getInstance('Horde_Memcache');
- }
+ $class1 = $this->_driverToClassname($GLOBALS['conf']['cache']['use_memorycache']);
+ $class2 = $this->_driverToClassname($driver);
+ $params = array(
+ 'stack' => array(
+ new $class1($base_params),
+ new $class2($params),
+ )
+ );
+ $driver = 'Stack';
+ }
- $params = array(
- 'stack' => array(
- array(
- 'driver' => $GLOBALS['conf']['cache']['use_memorycache'],
- 'params' => $base_params
- ),
- array(
- 'driver' => $driver,
- 'params' => $params
- )
- )
- );
- $driver = 'Stack';
- }
+ $classname = $this->_driverToClassname($driver);
+ return new $classname($params);
+ }
- $this->_instances[$driver] = Horde_Cache::factory($driver, $params);
+ protected function _driverToClassname($driver)
+ {
+ $driver = ucfirst(basename($driver));
+ $classname = 'Horde_Cache_' . $driver;
+ if (!class_exists($classname)) {
+ $classname = 'Horde_Cache_Null';
}
- return $this->_instances[$driver];
+ return $classname;
}
}
'Horde_Alarm' => 'Horde_Core_Binder_Alarm',
'Horde_Auth_Factory' => 'Horde_Core_Binder_AuthFactory',
// 'Horde_Browser' - initialized below
- 'Horde_Cache_Factory' => 'Horde_Core_Binder_CacheFactory',
'Horde_Core_Auth_Signup' => 'Horde_Core_Binder_AuthSignup',
'Horde_Crypt' => 'Horde_Core_Binder_Crypt',
'Horde_Data' => 'Horde_Core_Binder_Data',
$factories = array(
'Horde_Cache' => array(
'Horde_Core_Factory_Cache',
- 'getCache',
+ 'create',
),
'Horde_Controller_Request' => array(
'Horde_Core_Factory_Request',
<file name="Alarm.php" role="php" />
<file name="AuthFactory.php" role="php" />
<file name="AuthSignup.php" role="php" />
- <file name="CacheFactory.php" role="php" />
<file name="Crypt.php" role="php" />
<file name="Data.php" role="php" />
<file name="Db.php" role="php" />
<install as="Horde/Core/Binder/Alarm.php" name="lib/Horde/Core/Binder/Alarm.php" />
<install as="Horde/Core/Binder/AuthFactory.php" name="lib/Horde/Core/Binder/AuthFactory.php" />
<install as="Horde/Core/Binder/AuthSignup.php" name="lib/Horde/Core/Binder/AuthSignup.php" />
- <install as="Horde/Core/Binder/CacheFactory.php" name="lib/Horde/Core/Binder/CacheFactory.php" />
<install as="Horde/Core/Binder/Crypt.php" name="lib/Horde/Core/Binder/Crypt.php" />
<install as="Horde/Core/Binder/Data.php" name="lib/Horde/Core/Binder/Data.php" />
<install as="Horde/Core/Binder/Db.php" name="lib/Horde/Core/Binder/Db.php" />
/* Since IMAP tree generation is so expensive/time-consuming,
* fallback to storing in the session even if no permanent cache
* backend is setup. */
- $cache = $injector->getInstance('Horde_Cache_Factory')->getCache(array('session' => true));
+ $cache = $injector->getInstance('Horde_Cache');
+ if ($cache instanceof Horde_Cache_Null) {
+ $cache = $injector->getInstance('Horde_Cache_Session');
+ }
try {
$instance = @unserialize($cache->get($_SESSION['imp']['cache']['tree'], 86400));
} catch (Exception $e) {
{
/* Only need to store the object if the tree has changed. */
if ($instance->changed) {
- $cache = $this->_injector->getInstance('Horde_Cache_Factory')->getCache(array('session' => true));
+ $cache = $this->_injector->getInstance('Horde_Cache');
+ if ($cache instanceof Horde_Cache_Null) {
+ $cache = $this->_injector->getInstance('Horde_Cache_Session');
+ }
$cache->set($_SESSION['imp']['cache']['tree'], serialize($instance), 86400);
}
}
{
return false;
}
-
}