From: Michael M Slusarz Date: Sun, 31 Jan 2010 00:14:36 +0000 (-0700) Subject: Make Horde_Memcache injectable X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=6e4bd25a9b9a6219affbd3a752ca816713e3802e;p=horde.git Make Horde_Memcache injectable --- diff --git a/framework/Cache/lib/Horde/Cache.php b/framework/Cache/lib/Horde/Cache.php index f828e8fa7..2aeb30123 100644 --- a/framework/Cache/lib/Horde/Cache.php +++ b/framework/Cache/lib/Horde/Cache.php @@ -48,8 +48,11 @@ class Horde_Cache return new Horde_Cache_Null($params); } - $class = (empty($app) ? 'Horde' : $app) . '_Cache_' . ucfirst($driver); + if ($driver == 'memcache') { + $params['memcache'] = $GLOBALS['injector']->getInstance('Horde_Memcache'); + } + $class = (empty($app) ? 'Horde' : $app) . '_Cache_' . ucfirst($driver); if (class_exists($class)) { return new $class($params); } diff --git a/framework/Cache/lib/Horde/Cache/Memcache.php b/framework/Cache/lib/Horde/Cache/Memcache.php index 96002ec8d..2bf20ae71 100644 --- a/framework/Cache/lib/Horde/Cache/Memcache.php +++ b/framework/Cache/lib/Horde/Cache/Memcache.php @@ -3,6 +3,12 @@ * The Horde_Cache_Memcache:: class provides a memcached implementation of the * Horde caching system. * + * Addtional parameters: + * --------------------- + *
+ * 'memcache' - (Horde_Memcache) A Horde_Memcache object.
+ * 
+ * * Copyright 2006-2007 Duck * Copyright 2007-2010 The Horde Project (http://www.horde.org/) * @@ -17,7 +23,7 @@ class Horde_Cache_Memcache extends Horde_Cache_Base { /** - * Horde_memcache object. + * Memcache object. * * @var Horde_Memcache */ @@ -33,10 +39,16 @@ class Horde_Cache_Memcache extends Horde_Cache_Base * Construct a new Horde_Cache_Memcache object. * * @param array $params Parameter array. + * + * @throws InvalidArgumentException */ public function __construct($params = array()) { - $this->_memcache = Horde_Memcache::singleton(); + if (!isset($params['memcache'])) { + throw new InvalidArgumentException('Missing memcache object'); + } + + $this->_memcache = $params['memcache']; parent::__construct($params); } diff --git a/framework/Core/lib/Horde/Core/Binder/Cache.php b/framework/Core/lib/Horde/Core/Binder/Cache.php index bd56c6c9a..237035595 100644 --- a/framework/Core/lib/Horde/Core/Binder/Cache.php +++ b/framework/Core/lib/Horde/Core/Binder/Cache.php @@ -13,10 +13,14 @@ class Horde_Core_Binder_Cache implements Horde_Injector_Binder $driver = basename($driver); } - if (empty($driver) || $driver == 'none') { + if (empty($driver) || ($driver == 'none')) { return new Horde_Cache_Null($params); } + if ($driver == 'memcache') { + $params['memcache'] = $injector->getInstance('Horde_Memcache'); + } + $class = (empty($app) ? 'Horde' : $app) . '_Cache_' . ucfirst($driver); if (class_exists($class)) { return new $class($params); diff --git a/framework/Core/lib/Horde/Core/Binder/Memcache.php b/framework/Core/lib/Horde/Core/Binder/Memcache.php new file mode 100644 index 000000000..d10b95b33 --- /dev/null +++ b/framework/Core/lib/Horde/Core/Binder/Memcache.php @@ -0,0 +1,15 @@ +addBinder('Horde_Memcache', new Horde_Core_Binder_Memcache()); + /* Start a session. */ if ($session_flags & self::SESSION_NONE || (PHP_SAPI == 'cli') || @@ -286,17 +284,8 @@ class Horde_Registry Horde_Nls::setTextdomain('horde', HORDE_BASE . '/locale', Horde_Nls::getCharset()); Horde_String::setDefaultCharset(Horde_Nls::getCharset()); - /* Check for caching availability. Using cache while not authenticated - * isn't possible because, although storage is possible, retrieval - * isn't since there is no MD5 sum in the session to use to build - * the cache IDs. */ - if (Horde_Auth::getAuth()) { - try { - $this->_cacheob = Horde_Cache::singleton($conf['cache']['driver'], Horde::getDriverConfig('cache', $conf['cache']['driver'])); - } catch (Horde_Exception $e) { - // @TODO Log error - } - } + /* Initialize caching. */ + $GLOBALS['injector']->addBinder('Horde_Cache', new Horde_Core_Binder_Cache()); $this->_regmtime = max(filemtime(HORDE_BASE . '/config/registry.php'), filemtime(HORDE_BASE . '/config/registry.d')); @@ -319,9 +308,7 @@ class Horde_Registry throw new Horde_Exception(_("This system is currently deactivated.")); } - /* Set default bindings. */ - $GLOBALS['injector'] = new Horde_Injector(new Horde_Injector_TopLevel()); - $GLOBALS['injector']->addBinder('Horde_Cache', new Horde_Core_Binder_Cache()); + /* Set the rest of the default bindings. */ $GLOBALS['injector']->addBinder('Horde_Db_Adapter_Base', new Horde_Core_Binder_Db('reader')); $GLOBALS['injector']->addBinder('Horde_Log_Logger', new Horde_Core_Binder_Logger()); @@ -1376,18 +1363,25 @@ class Horde_Registry */ protected function _saveCacheVar($name, $expire = false) { - if ($this->_cacheob) { - if ($expire) { - if ($id = $this->_getCacheId($name)) { - $this->_cacheob->expire($id); - } - } else { - $data = serialize($this->_cache[$name]); - $_SESSION['_registry']['md5'][$name] = $md5sum = hash('md5', $data); - $id = $this->_getCacheId($name, false) . '|' . $md5sum; - if ($this->_cacheob->set($id, $data, 86400)) { - Horde::logMessage('Horde_Registry: stored ' . $name . ' with cache ID ' . $id, __FILE__, __LINE__, PEAR_LOG_DEBUG); - } + /* Using cache while not authenticated isn't possible because, + * although storage is possible, retrieval isn't since there is no + * MD5 sum in the session to use to build the cache IDs. */ + if (!Horde_Auth::getAuth()) { + return; + } + + $ob = $GLOBALS['injector']->getInstance('Horde_Cache'); + + if ($expire) { + if ($id = $this->_getCacheId($name)) { + $ob->expire($id); + } + } else { + $data = serialize($this->_cache[$name]); + $_SESSION['_registry']['md5'][$name] = $md5sum = hash('md5', $data); + $id = $this->_getCacheId($name, false) . '|' . $md5sum; + if ($ob->set($id, $data, 86400)) { + Horde::logMessage('Horde_Registry: stored ' . $name . ' with cache ID ' . $id, __FILE__, __LINE__, PEAR_LOG_DEBUG); } } } @@ -1405,9 +1399,12 @@ class Horde_Registry return true; } - if ($this->_cacheob && + /* Using cache while not authenticated isn't possible because, + * although storage is possible, retrieval isn't since there is no + * MD5 sum in the session to use to build the cache IDs. */ + if (Horde_Auth::getAuth() && ($id = $this->_getCacheId($name))) { - $result = $this->_cacheob->get($id, 86400); + $result = $GLOBALS['injector']->getInstance('Horde_Cache')->get($id, 86400); if ($result !== false) { $this->_cache[$name] = unserialize($result); Horde::logMessage('Horde_Registry: retrieved ' . $name . ' with cache ID ' . $id, __FILE__, __LINE__, PEAR_LOG_DEBUG); @@ -1482,15 +1479,11 @@ class Horde_Registry $calls['destroy'], $calls['gc']); } elseif ($type != 'none') { - $sh = Horde_SessionHandler::singleton($conf['sessionhandler']['type'], array_merge(Horde::getDriverConfig('sessionhandler', $conf['sessionhandler']['type']), array('memcache' => !empty($conf['sessionhandler']['memcache'])))); - ini_set('session.save_handler', 'user'); - session_set_save_handler(array($sh, 'open'), - array($sh, 'close'), - array($sh, 'read'), - array($sh, 'write'), - array($sh, 'destroy'), - array($sh, 'gc')); - $this->sessionHandler = $sh; + $sh_config = Horde::getDriverConfig('sessionhandler', $conf['sessionhandler']['type']); + if (!empty($conf['sessionhandler']['memcache'])) { + $sh_config['memcache'] = $GLOBALS['injector']->getInstance('Horde_Memcache'); + } + $this->sessionHandler = Horde_SessionHandler::factory($conf['sessionhandler']['type'], $sh_config); } } diff --git a/framework/Core/package.xml b/framework/Core/package.xml index 3f47c8ae8..ad0ff50e0 100644 --- a/framework/Core/package.xml +++ b/framework/Core/package.xml @@ -54,6 +54,7 @@ Application Framework. + @@ -133,6 +134,7 @@ Application Framework. + diff --git a/framework/Memcache/lib/Horde/Memcache.php b/framework/Memcache/lib/Horde/Memcache.php index 5b2daa5e3..9c589d217 100644 --- a/framework/Memcache/lib/Horde/Memcache.php +++ b/framework/Memcache/lib/Horde/Memcache.php @@ -53,13 +53,6 @@ class Horde_Memcache const MAX_SIZE = 1000000; /** - * The singleton instance. - * - * @var Horde_Memcache - */ - static protected $_instance = null; - - /** * Memcache object. * * @var Memcache @@ -94,25 +87,15 @@ class Horde_Memcache protected $_noexist = array(); /** - * Singleton. - */ - public static function singleton() - { - if (!self::$_instance) { - self::$_instance = new self(); - } - - return self::$_instance; - } - - /** * Constructor. * + * @param array $params TODO + * * @throws Horde_Exception */ - protected function __construct() + public function __construct($params = array()) { - $this->_params = array_merge($this->_params, $GLOBALS['conf']['memcache']); + $this->_params = array_merge($this->_params, $params); $this->_params['prefix'] = (empty($this->_params['prefix'])) ? 'horde' : $this->_params['prefix']; $this->_large = !empty($this->_params['large_items']); diff --git a/framework/Memcache/scripts/Horde/Memcache/stats.php b/framework/Memcache/scripts/Horde/Memcache/stats.php index f408bd376..f1cc5275e 100755 --- a/framework/Memcache/scripts/Horde/Memcache/stats.php +++ b/framework/Memcache/scripts/Horde/Memcache/stats.php @@ -36,7 +36,7 @@ if (PEAR::isError($options)) { } $all = $raw = $summary = false; -$memcache = &Horde_Memcache::singleton(); +$memcache = $injector->getInstance('Horde_Memcache'); foreach ($options[0] as $val) { switch ($val[0]) { diff --git a/framework/SessionHandler/lib/Horde/SessionHandler.php b/framework/SessionHandler/lib/Horde/SessionHandler.php index f37ebb460..2400fd3e9 100644 --- a/framework/SessionHandler/lib/Horde/SessionHandler.php +++ b/framework/SessionHandler/lib/Horde/SessionHandler.php @@ -4,7 +4,8 @@ * handlers. * * Optional parameters:
- *   'memcache' - (boolean) Use memcache to cache session data?
+ * 'memcache' - (Horde_Memcache) If set, uses memcache to cache session
+ *              data.
  * 
* * Copyright 2002-2010 The Horde Project (http://www.horde.org/) @@ -19,13 +20,6 @@ class Horde_SessionHandler { /** - * Singleton instances. - * - * @var array - */ - static protected $_instances = array(); - - /** * Hash containing connection parameters. * * @var array @@ -74,10 +68,15 @@ class Horde_SessionHandler // Trap for old driver name. $driver = 'memcache'; } elseif (($driver != 'memcache') && !empty($params['memcache'])) { - unset($params['memcache']); - $persistent_params = array('persistent_driver' => $driver, 'persistent_params' => $params); + $p_params = $params; + unset($p_params['memcache']); + + $persistent_params = array( + 'persistent_driver' => $driver, + 'persistent_params' => $p_params + ); + $driver = 'memcache'; - $params = array(); } $class = __CLASS__ . '_' . ucfirst($driver); @@ -93,32 +92,6 @@ class Horde_SessionHandler } /** - * Attempts to return a reference to a concrete Horde_SessionHandler - * instance based on $driver. It will only create a new instance - * if no Horde_SessionHandler instance with the same parameters - * currently exists. - * - * This method must be invoked as: - * $var = Horde_SessionHandler::singleton() - * - * @param string $driver See Horde_SessionHandler::factory(). - * @param array $params See Horde_SessionHandler::factory(). - * - * @return Horde_SessionHandler The singleton instance. - * @throws Horde_Exception - */ - static public function singleton($driver, $params = array()) - { - ksort($params); - $signature = hash('md5', serialize(array($driver, $params))); - if (empty(self::$_instances[$signature])) { - self::$_instances[$signature] = self::factory($driver, $params); - } - - return self::$_instances[$signature]; - } - - /** * Constructor. * * @param array $params A hash containing connection parameters. @@ -128,7 +101,18 @@ class Horde_SessionHandler protected function __construct($params = array()) { $this->_params = $params; + register_shutdown_function(array($this, 'shutdown')); + + ini_set('session.save_handler', 'user'); + session_set_save_handler( + array($this, 'open'), + array($this, 'close'), + array($this, 'read'), + array($this, 'write'), + array($this, 'destroy'), + array($this, 'gc') + ); } /** diff --git a/framework/SessionHandler/lib/Horde/SessionHandler/Memcache.php b/framework/SessionHandler/lib/Horde/SessionHandler/Memcache.php index c1331507e..5d98880fb 100644 --- a/framework/SessionHandler/lib/Horde/SessionHandler/Memcache.php +++ b/framework/SessionHandler/lib/Horde/SessionHandler/Memcache.php @@ -1,19 +1,19 @@ + * 'memcache' - (Horde_Memcache) A memcache object. + * * * Optional parameters:
- *   'persistent_driver' - (string) If set, uses this backend to store session
- *                         data persistently.
- *   'persistent_params' - (array) If using a persistent backend, the params
- *                         to use for the persistent backend.
- *   'track' - (boolean) Track active sessions?
- *   'track_lifetime' - (integer) The number of seconds after which tracked
- *                      sessions will be treated as expired.
+ * 'persistent_driver' - (string) If set, uses this backend to store session
+ *                       data persistently.
+ * 'persistent_params' - (array) If using a persistent backend, the params
+ *                       to use for the persistent backend.
+ * 'track' - (boolean) Track active sessions?
+ * 'track_lifetime' - (integer) The number of seconds after which tracked
+ *                    sessions will be treated as expired.
  * 
* * Copyright 2005-2010 The Horde Project (http://www.horde.org/) @@ -28,7 +28,7 @@ class Horde_SessionHandler_Memcache extends Horde_SessionHandler { /** - * Horde_Memcache object. + * Memcache object. * * @var Horde_Memcache */ @@ -68,9 +68,16 @@ class Horde_SessionHandler_Memcache extends Horde_SessionHandler * @param array $params A hash containing connection parameters. * * @throws Horde_Exception + * @throws InvalidArgumentException */ protected function __construct($params = array()) { + if (empty($params['memcache'])) { + throw InvalidArgumentException('Missing memcache object.'); + } + + $this->_memcache = $params['memcache']; + if (!empty($params['persistent_driver'])) { try { $this->_persistent = self::singleton($params['persistent_driver'], empty($params['persistent_params']) ? null : $params['persistent_params']); @@ -105,8 +112,6 @@ class Horde_SessionHandler_Memcache extends Horde_SessionHandler */ protected function _open($save_path = null, $session_name = null) { - $this->_memcache = Horde_Memcache::singleton(); - if (isset($this->_persistent)) { if (!$this->_persistent->open($save_path, $session_name)) { throw new Horde_Exception('Could not open persistent backend.'); diff --git a/kronolith/fb.php b/kronolith/fb.php index 2a4ff2d5f..fd26bf8f1 100644 --- a/kronolith/fb.php +++ b/kronolith/fb.php @@ -26,7 +26,7 @@ if (!empty($cal)) { $user = basename($pathInfo); } -$cache = Horde_Cache::factory($conf['cache']['driver'], Horde::getDriverConfig('cache', $conf['cache']['driver'])); +$cache = $injector->getInstance('Horde_Cache'); $key = 'kronolith.fb.' . ($user ? 'u.' . $user : 'c.' . $cal); $fb = $cache->get($key, 360); if (!$fb) {