From f8e2a4023700f06ff2354439cc215ec7c5091202 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Fri, 3 Sep 2010 12:41:04 -0600 Subject: [PATCH] Add Horde_Cache factory object Provides a way to default to the Session cache driver, instead of the Null driver. --- framework/Core/lib/Horde/Core/Binder/Cache.php | 48 +------- .../Core/lib/Horde/Core/Binder/CacheFactory.php | 18 +++ framework/Core/lib/Horde/Core/Factory/Cache.php | 125 +++++++++++++++++++++ framework/Core/lib/Horde/Registry.php | 1 + framework/Core/package.xml | 4 + 5 files changed, 150 insertions(+), 46 deletions(-) create mode 100644 framework/Core/lib/Horde/Core/Binder/CacheFactory.php create mode 100644 framework/Core/lib/Horde/Core/Factory/Cache.php diff --git a/framework/Core/lib/Horde/Core/Binder/Cache.php b/framework/Core/lib/Horde/Core/Binder/Cache.php index 19c02d98f..b8366d93f 100644 --- a/framework/Core/lib/Horde/Core/Binder/Cache.php +++ b/framework/Core/lib/Horde/Core/Binder/Cache.php @@ -7,52 +7,8 @@ class Horde_Core_Binder_Cache implements Horde_Injector_Binder { public function create(Horde_Injector $injector) { - $driver = empty($GLOBALS['conf']['cache']['driver']) - ? 'Null' - : $GLOBALS['conf']['cache']['driver']; - if (strcasecmp($driver, 'None') === 0) { - $driver = 'Null'; - } - - $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; - - $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 (!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'); - } - - $params = array( - 'stack' => array( - array( - 'driver' => $GLOBALS['conf']['cache']['use_memorycache'], - 'params' => $base_params - ), - array( - 'driver' => $driver, - 'params' => $params - ) - ) - ); - $driver = 'Stack'; - } - - return Horde_Cache::factory($driver, $params); + $cache = new Horde_Core_Factory_Cache($injector); + return $cache->getCache(); } public function equals(Horde_Injector_Binder $binder) diff --git a/framework/Core/lib/Horde/Core/Binder/CacheFactory.php b/framework/Core/lib/Horde/Core/Binder/CacheFactory.php new file mode 100644 index 000000000..53f7d442f --- /dev/null +++ b/framework/Core/lib/Horde/Core/Binder/CacheFactory.php @@ -0,0 +1,18 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Core + */ + +/** + * A Horde_Injector:: based Horde_Cache:: factory. + * + * Copyright 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. + * + * @category Horde + * @package Core + * @author Michael Slusarz + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Core + */ +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: + *
+     * 'session' - (boolean) Fallback to session driver, instead of null
+     *             driver, if no cache config is found.
+     *             DEFAULT: false
+     * 
+ * + * @return Horde_Cache_Base The singleton instance. + * @throws Horde_Cache_Exception + */ + public function getCache(array $opts = array()) + { + $driver = empty($GLOBALS['conf']['cache']['driver']) + ? 'Null' + : $GLOBALS['conf']['cache']['driver']; + if (strcasecmp($driver, 'None') === 0) { + $driver = 'Null'; + } + + if (($driver == 'Null') && !empty($opts['session'])) { + $driver = 'Session'; + } + + 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 = $this->_injector->getInstance('Horde_Log_Logger'); + $params['logger'] = $logger; + + $base_params = $params; + + 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'] = $this->_injector->getInstance('Horde_Memcache'); + } + + $params = array( + 'stack' => array( + array( + 'driver' => $GLOBALS['conf']['cache']['use_memorycache'], + 'params' => $base_params + ), + array( + 'driver' => $driver, + 'params' => $params + ) + ) + ); + $driver = 'Stack'; + } + + $this->_instances[$driver] = Horde_Cache::factory($driver, $params); + } + + return $this->_instances[$driver]; + } + +} diff --git a/framework/Core/lib/Horde/Registry.php b/framework/Core/lib/Horde/Registry.php index e9b912ae4..1e9265bf5 100644 --- a/framework/Core/lib/Horde/Registry.php +++ b/framework/Core/lib/Horde/Registry.php @@ -266,6 +266,7 @@ implements Horde_Interfaces_Registry_Auth 'Horde_Auth' => 'Horde_Core_Binder_Auth', // 'Horde_Browser' - initialized below 'Horde_Cache' => 'Horde_Core_Binder_Cache', + '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', diff --git a/framework/Core/package.xml b/framework/Core/package.xml index 6be6da223..ba2b3e823 100644 --- a/framework/Core/package.xml +++ b/framework/Core/package.xml @@ -112,6 +112,7 @@ Application Framework. + @@ -153,6 +154,7 @@ Application Framework. + @@ -437,6 +439,7 @@ Application Framework. + @@ -474,6 +477,7 @@ Application Framework. + -- 2.11.0