From 3f60739828d2743e5bb76b2011c174ba05103ed0 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Fri, 15 Oct 2010 13:06:38 -0600 Subject: [PATCH] Add/use Horde_Session based prefs caching driver --- framework/Core/lib/Horde/Core/Factory/Prefs.php | 7 ++- .../Core/lib/Horde/Core/Prefs/Cache/Session.php | 73 ++++++++++++++++++++++ framework/Core/lib/Horde/Registry.php | 2 +- framework/Core/package.xml | 4 ++ 4 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 framework/Core/lib/Horde/Core/Prefs/Cache/Session.php diff --git a/framework/Core/lib/Horde/Core/Factory/Prefs.php b/framework/Core/lib/Horde/Core/Factory/Prefs.php index af1ccd285..b9bce66e1 100644 --- a/framework/Core/lib/Horde/Core/Factory/Prefs.php +++ b/framework/Core/lib/Horde/Core/Factory/Prefs.php @@ -84,7 +84,7 @@ class Horde_Core_Factory_Prefs } $opts = array_merge(array( - 'cache' => 'Horde_Prefs_Cache_Session', + 'cache' => 'Horde_Core_Prefs_Cache_Session', 'charset' => 'UTF-8', 'logger' => $this->_injector->getInstance('Horde_Log_Logger'), 'password' => '', @@ -93,6 +93,11 @@ class Horde_Core_Factory_Prefs ), $opts); ksort($opts); + /* Allow no caching to be specified as false-y value. */ + if (!$opts['cache']) { + unset($opts['cache']); + } + /* If $params['user_hook'] is defined, use it to retrieve the value to * use for the username. */ if (!empty($params['user_hook']) && diff --git a/framework/Core/lib/Horde/Core/Prefs/Cache/Session.php b/framework/Core/lib/Horde/Core/Prefs/Cache/Session.php new file mode 100644 index 000000000..2ae9f263c --- /dev/null +++ b/framework/Core/lib/Horde/Core/Prefs/Cache/Session.php @@ -0,0 +1,73 @@ + + * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @package Core + */ +class Horde_Core_Prefs_Cache_Session extends Horde_Prefs_Cache +{ + /** + * Session key. + * + * @var string + */ + protected $_key; + + /** + */ + public function __construct($user) + { + parent::__construct($user); + + $this->_key = 'horde:prefs_' . $this->_user . '/'; + } + + /** + */ + public function get($scope) + { + global $session; + + return isset($session[$this->_key . $scope]) + ? $session[$this->_key . $scope] + : false; + } + + /** + */ + public function update($scope, $prefs) + { + if (($cached = $this->get($scope)) === false) { + $cached = array(); + } + $cached = array_merge($cached, $prefs); + $GLOBALS['session'][$this->_key . $scope] = $cached; + } + + /** + */ + public function clear($scope = null, $pref = null) + { + global $session; + + if (is_null($scope)) { + unset($session[$this->_key]); + } elseif (is_null($pref)) { + unset($session[$this->_key . $scope]); + } elseif ((($cached = $this->get($scope)) !== false) && + isset($cached[$pref])) { + unset($cached[$pref]); + $session[$this->_key . $scope] = $cached; + } + } + +} diff --git a/framework/Core/lib/Horde/Registry.php b/framework/Core/lib/Horde/Registry.php index 7e59ede35..1e1e432c8 100644 --- a/framework/Core/lib/Horde/Registry.php +++ b/framework/Core/lib/Horde/Registry.php @@ -1345,7 +1345,7 @@ class Horde_Registry /* If there is no logged in user, return an empty Horde_Prefs:: * object with just default preferences. */ $opts = array( - 'cache' => false, + 'cache' => 'Horde_Prefs_Cache_Null', 'session' => true ); } diff --git a/framework/Core/package.xml b/framework/Core/package.xml index 01e3e0b4a..f781d4aae 100644 --- a/framework/Core/package.xml +++ b/framework/Core/package.xml @@ -184,6 +184,9 @@ Application Framework. + + + @@ -479,6 +482,7 @@ Application Framework. + -- 2.11.0