From: Chuck Hagenbuch Date: Sun, 3 Oct 2010 01:59:23 +0000 (-0400) Subject: Simplify Horde_Cache binder to a simple injector factory X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=30684fe5577cf38ea3304004c068034b89ea4470;p=horde.git Simplify Horde_Cache binder to a simple injector factory --- diff --git a/framework/Cache/lib/Horde/Cache.php b/framework/Cache/lib/Horde/Cache.php index 5141a4cca..6cbc88908 100644 --- a/framework/Cache/lib/Horde/Cache.php +++ b/framework/Cache/lib/Horde/Cache.php @@ -1,8 +1,7 @@ * @author Chuck Hagenbuch + * @author Michael Slusarz * @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: + *
+     * '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($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; + } } diff --git a/framework/Cache/lib/Horde/Cache/Apc.php b/framework/Cache/lib/Horde/Cache/Apc.php index 4c7828d95..84b41e3d9 100644 --- a/framework/Cache/lib/Horde/Cache/Apc.php +++ b/framework/Cache/lib/Horde/Cache/Apc.php @@ -12,7 +12,7 @@ * @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 @@ -104,5 +104,4 @@ class Horde_Cache_Apc extends Horde_Cache_Base apc_delete($key . '_expire'); } } - } diff --git a/framework/Cache/lib/Horde/Cache/Base.php b/framework/Cache/lib/Horde/Cache/Base.php deleted file mode 100644 index 2067a1f48..000000000 --- a/framework/Cache/lib/Horde/Cache/Base.php +++ /dev/null @@ -1,132 +0,0 @@ - - * @author Chuck Hagenbuch - * @author Michael Slusarz - * @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: - *
-     * '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($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; - } - -} diff --git a/framework/Cache/lib/Horde/Cache/Eaccelerator.php b/framework/Cache/lib/Horde/Cache/Eaccelerator.php index c91965420..6e1501efb 100644 --- a/framework/Cache/lib/Horde/Cache/Eaccelerator.php +++ b/framework/Cache/lib/Horde/Cache/Eaccelerator.php @@ -12,14 +12,14 @@ * @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')) { @@ -119,5 +119,4 @@ class Horde_Cache_Eaccelerator extends Horde_Cache_Base eaccelerator_rm($key . '_expire'); } } - } diff --git a/framework/Cache/lib/Horde/Cache/File.php b/framework/Cache/lib/Horde/Cache/File.php index d4cf07cea..b7022a5b3 100644 --- a/framework/Cache/lib/Horde/Cache/File.php +++ b/framework/Cache/lib/Horde/Cache/File.php @@ -13,7 +13,7 @@ * @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. @@ -299,6 +299,4 @@ class Horde_Cache_File extends Horde_Cache_Base } $d->close(); } - } - diff --git a/framework/Cache/lib/Horde/Cache/Memcache.php b/framework/Cache/lib/Horde/Cache/Memcache.php index e9073ebbe..198fc2f86 100644 --- a/framework/Cache/lib/Horde/Cache/Memcache.php +++ b/framework/Cache/lib/Horde/Cache/Memcache.php @@ -14,7 +14,7 @@ * @category Horde * @package Cache */ -class Horde_Cache_Memcache extends Horde_Cache_Base +class Horde_Cache_Memcache extends Horde_Cache { /** * Memcache object. @@ -151,5 +151,4 @@ class Horde_Cache_Memcache extends Horde_Cache_Base return $this->_memcache->delete($key); } - } diff --git a/framework/Cache/lib/Horde/Cache/Mock.php b/framework/Cache/lib/Horde/Cache/Mock.php index 872ba375e..8d256ab7f 100644 --- a/framework/Cache/lib/Horde/Cache/Mock.php +++ b/framework/Cache/lib/Horde/Cache/Mock.php @@ -15,7 +15,7 @@ * @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. diff --git a/framework/Cache/lib/Horde/Cache/Null.php b/framework/Cache/lib/Horde/Cache/Null.php index 27267021b..93af1647c 100644 --- a/framework/Cache/lib/Horde/Cache/Null.php +++ b/framework/Cache/lib/Horde/Cache/Null.php @@ -12,7 +12,7 @@ * @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 @@ -69,5 +69,4 @@ class Horde_Cache_Null extends Horde_Cache_Base { return false; } - } diff --git a/framework/Cache/lib/Horde/Cache/Session.php b/framework/Cache/lib/Horde/Cache/Session.php index 260b56fc5..ea8c8663f 100644 --- a/framework/Cache/lib/Horde/Cache/Session.php +++ b/framework/Cache/lib/Horde/Cache/Session.php @@ -13,7 +13,7 @@ * @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. @@ -123,5 +123,4 @@ class Horde_Cache_Session extends Horde_Cache_Base return false; } - } diff --git a/framework/Cache/lib/Horde/Cache/Sql.php b/framework/Cache/lib/Horde/Cache/Sql.php index e5ab16550..2846f8e37 100644 --- a/framework/Cache/lib/Horde/Cache/Sql.php +++ b/framework/Cache/lib/Horde/Cache/Sql.php @@ -28,7 +28,7 @@ * @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. @@ -252,5 +252,4 @@ class Horde_Cache_Sql extends Horde_Cache_Base return true; } - } diff --git a/framework/Cache/lib/Horde/Cache/Stack.php b/framework/Cache/lib/Horde/Cache/Stack.php index d075b9c58..2385a96c3 100644 --- a/framework/Cache/lib/Horde/Cache/Stack.php +++ b/framework/Cache/lib/Horde/Cache/Stack.php @@ -13,7 +13,7 @@ * @category Horde * @package Cache */ -class Horde_Cache_Stack extends Horde_Cache_Base +class Horde_Cache_Stack extends Horde_Cache { /** * Stack of cache drivers. @@ -27,12 +27,9 @@ class Horde_Cache_Stack extends Horde_Cache_Base * * @param array $params Parameters: *
-     * '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.
      * 
* * @throws InvalidArgumentException @@ -42,13 +39,9 @@ class Horde_Cache_Stack extends Horde_Cache_Base 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); } @@ -152,5 +145,4 @@ class Horde_Cache_Stack extends Horde_Cache_Base return $success; } - } diff --git a/framework/Cache/lib/Horde/Cache/Xcache.php b/framework/Cache/lib/Horde/Cache/Xcache.php index bd4306167..b4635c916 100644 --- a/framework/Cache/lib/Horde/Cache/Xcache.php +++ b/framework/Cache/lib/Horde/Cache/Xcache.php @@ -12,7 +12,7 @@ * @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. @@ -106,5 +106,4 @@ class Horde_Cache_Xcache extends Horde_Cache_Base xcache_unset($key); } } - } diff --git a/framework/Cache/package.xml b/framework/Cache/package.xml index 90c5a627a..7b622dfc4 100644 --- a/framework/Cache/package.xml +++ b/framework/Cache/package.xml @@ -34,7 +34,7 @@ Performance Suite's content cache), memcached, or an SQL table. LGPL * 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. @@ -47,7 +47,6 @@ Performance Suite's content cache), memcached, or an SQL table. - @@ -105,7 +104,6 @@ Performance Suite's content cache), memcached, or an SQL table. - diff --git a/framework/Core/lib/Horde/Core/Binder/Cache.php b/framework/Core/lib/Horde/Core/Binder/Cache.php deleted file mode 100644 index b8366d93f..000000000 --- a/framework/Core/lib/Horde/Core/Binder/Cache.php +++ /dev/null @@ -1,19 +0,0 @@ -getCache(); - } - - public function equals(Horde_Injector_Binder $binder) - { - return false; - } - -} diff --git a/framework/Core/lib/Horde/Core/Binder/CacheFactory.php b/framework/Core/lib/Horde/Core/Binder/CacheFactory.php deleted file mode 100644 index 53f7d442f..000000000 --- a/framework/Core/lib/Horde/Core/Binder/CacheFactory.php +++ /dev/null @@ -1,18 +0,0 @@ -_injector = $injector; - } - - /** * Return the Horde_Cache:: instance. * * @param array $opts Options: @@ -64,7 +40,7 @@ class Horde_Core_Factory_Cache * @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' @@ -73,52 +49,52 @@ class Horde_Core_Factory_Cache $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; } } diff --git a/framework/Core/lib/Horde/Registry.php b/framework/Core/lib/Horde/Registry.php index 08ee18c72..b814d5029 100644 --- a/framework/Core/lib/Horde/Registry.php +++ b/framework/Core/lib/Horde/Registry.php @@ -267,7 +267,6 @@ class Horde_Registry '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', @@ -308,7 +307,7 @@ class Horde_Registry $factories = array( 'Horde_Cache' => array( 'Horde_Core_Factory_Cache', - 'getCache', + 'create', ), 'Horde_Controller_Request' => array( 'Horde_Core_Factory_Request', diff --git a/framework/Core/package.xml b/framework/Core/package.xml index ead3bcda8..7c4380abf 100644 --- a/framework/Core/package.xml +++ b/framework/Core/package.xml @@ -112,7 +112,6 @@ Application Framework. - @@ -434,7 +433,6 @@ Application Framework. - diff --git a/imp/lib/Injector/Binder/Imaptree.php b/imp/lib/Injector/Binder/Imaptree.php index 004ce197d..7ee2bf96f 100644 --- a/imp/lib/Injector/Binder/Imaptree.php +++ b/imp/lib/Injector/Binder/Imaptree.php @@ -37,7 +37,10 @@ class IMP_Injector_Binder_Imaptree implements Horde_Injector_Binder /* 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) { @@ -61,7 +64,10 @@ class IMP_Injector_Binder_Imaptree implements Horde_Injector_Binder { /* 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); } } @@ -70,5 +76,4 @@ class IMP_Injector_Binder_Imaptree implements Horde_Injector_Binder { return false; } - }