}
$opts = array_merge(array(
- 'cache' => true,
+ 'cache' => 'Horde_Prefs_Cache_Session',
'charset' => 'UTF-8',
'logger' => $this->_injector->getInstance('Horde_Log_Logger'),
'password' => '',
* @var array
*/
protected $_opts = array(
- 'cache' => false,
+ 'cache' => 'Horde_Prefs_Cache_Null',
'logger' => null,
'password' => '',
'sizecallback' => null,
);
/**
- * Array to cache in. Usually a reference to an array in $_SESSION, but
- * could be overridden by a subclass for testing or other users.
+ * Caching object.
*
- * @var array
+ * @var Horde_Prefs_Cache
*/
- protected $_cache = array();
+ protected $_cache;
/**
* Hash holding preferences with hook functions defined.
* charset - (string) Default charset.
* OPTIONAL:
* ---------
- * cache - (boolean) Should caching be used?
- * DEFAULT: false
+ * cache - (string) The class name defining the cache driver to use.
+ * DEFAULT: Caching is not used
* logger - (Horde_Log_Logger) Logging object.
* DEFAULT: NONE
* password - (string) The password associated with 'user'.
$this->_params = $params;
$this->_scope = $scope;
+ $this->_cache = new $this->_opts['cache']($this->getUser());
$this->_dict = isset($this->_opts['translation'])
? $this->_opts['translation']
: new Horde_Translation_Gettext('Horde_Prefs', dirname(__FILE__) . '/../../locale');
- // Create a unique key that's safe to use for caching even if we want
- // another user's preferences later, then register the cache array in
- // $_SESSION.
- if ($this->_opts['cache']) {
- $cacheKey = 'horde_prefs_' . $this->getUser();
-
- // Store a reference to the $_SESSION array.
- $this->_cache = &$_SESSION[$cacheKey];
- }
-
register_shutdown_function(array($this, 'store'));
}
*/
public function remove($pref)
{
- // FIXME not updated yet.
+ // FIXME not updated yet - not removed from backend.
$scope = $this->_getPreferenceScope($pref);
unset($this->_prefs[$pref]);
- unset($this->_cache[$scope][$pref]);
+ $this->_cache->clear($scope, $pref);
}
/**
if ($result && $this->isDirty($pref)) {
$scope = $this->_getPreferenceScope($pref);
- $this->_cacheUpdate($scope, array($pref));
+ $this->_cache->update($scope, array(
+ $pref => $this->_scopes[$scope][$pref]
+ ));
/* If this preference has a change hook, call it now. */
try {
$this->_setDefaults($scope);
// Now check the prefs cache for existing values.
- if ($this->_cacheLookup($scope)) {
+ if (($cached = $this->_cache->get($scope)) !== false) {
+ $this->_scopes[$scope] = $cached;
return;
}
$this->_retrieve($scope);
$this->_callHooks($scope);
- /* Update the session cache. */
- $this->_cacheUpdate($scope, array_keys($this->_scopes[$scope]));
+ /* Update the cache. */
+ $this->_cache->update($scope, $this->_scopes[$scope]);
}
/**
$this->_prefs = array();
/* Destroy the contents of the preferences cache. */
- unset($this->_cache);
+ $this->_cache->clear();
} else {
- /* Remove this scope from the preferences cache, if it exists. */
- unset($this->_cache[$this->getScope()]);
+ /* Remove this scope from the preferences cache. */
+ $this->_cache->clear($this->getScope());
}
}
}
/**
- * Updates the session-based preferences cache (if available).
- *
- * @param string $scope The scope of the prefs being updated.
- * @param array $prefs The preferences to update.
- */
- protected function _cacheUpdate($scope, $prefs)
- {
- if ($this->_opts['cache'] && isset($this->_cache)) {
- /* Place each preference in the cache according to its
- * scope. */
- foreach ($prefs as $name) {
- if (isset($this->_scopes[$scope][$name])) {
- $this->_cache[$scope][$name] = $this->_scopes[$scope][$name];
- }
- }
- }
- }
-
- /**
- * Tries to find the requested preferences in the cache. If they
- * exist, update the $_scopes hash with the cached values.
- *
- * @return boolean True on success, false on failure.
- */
- protected function _cacheLookup($scope)
- {
- if ($this->_opts['cache'] && isset($this->_cache[$scope])) {
- $this->_scopes[$scope] = $this->_cache[$scope];
- return true;
- }
-
- return false;
- }
-
- /**
* Populates the $_scopes hash with new entries and externally
* defined default values.
*
--- /dev/null
+<?php
+/**
+ * Cache driver for the preferences system.
+ *
+ * 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.
+ *
+ * @author Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @package Prefs
+ */
+abstract class Horde_Prefs_Cache
+{
+ /**
+ * The username.
+ *
+ * @var string
+ */
+ protected $_user;
+
+ /**
+ * Constructor.
+ *
+ * @param string $user The current username.
+ */
+ public function __construct($user)
+ {
+ $this->_user = $user;
+ }
+
+ /**
+ * Gets a cache entry.
+ *
+ * @param string $scope The scope of the prefs to get.
+ *
+ * @return mixed Prefs array on success, false on failure.
+ */
+ abstract public function get($scope);
+
+ /**
+ * Updates a cache entry.
+ *
+ * @param string $scope The scope of the prefs being updated.
+ * @param array $prefs The preferences to update.
+ */
+ abstract public function update($scope, $prefs);
+
+ /**
+ * Clear cache entries.
+ *
+ * @param string $scope The scope of the prefs to clear. If null, clears
+ * entire cache.
+ * @param string $scope The pref to clear. If null, clears the entire
+ * scope.
+ */
+ abstract public function clear($scope = null, $pref = null);
+
+}
--- /dev/null
+<?php
+/**
+ * Null cache driver for the preferences system.
+ *
+ * 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.
+ *
+ * @author Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @package Prefs
+ */
+class Horde_Prefs_Cache_Null extends Horde_Prefs_Cache
+{
+ /**
+ */
+ public function get($scope)
+ {
+ return false;
+ }
+
+ /**
+ */
+ public function update($scope, $prefs)
+ {
+ }
+
+ /**
+ */
+ public function clear($scope = null, $pref = null)
+ {
+ }
+
+}
--- /dev/null
+<?php
+/**
+ * Session storage cache driver for the preferences system.
+ *
+ * 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.
+ *
+ * @author Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @package Prefs
+ */
+class Horde_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)
+ {
+ return isset($_SESSION[$this->_key][$scope])
+ ? $_SESSION[$this->_key][$scope]
+ : false;
+ }
+
+ /**
+ */
+ public function update($scope, $prefs)
+ {
+ $_SESSION[$this->_key][$scope] = isset($_SESSION[$this->_key][$scope])
+ ? array_merge($_SESSION[$this->_key][$scope], $prefs)
+ : array();
+ }
+
+ /**
+ */
+ public function clear($scope = null, $pref = null)
+ {
+ if (is_null($scope)) {
+ unset($_SESSION[$this->_key]);
+ } elseif (is_null($pref)) {
+ unset($_SESSION[$this->_key][$scope]);
+ } else {
+ unset($_SESSION[$this->_key][$scope][$pref]);
+ }
+ }
+
+}
$this->_connect();
foreach ($dirty_prefs as $scope => $prefs) {
+ $updated = array();
+
foreach ($prefs as $name => $pref) {
// Don't store locked preferences.
if ($this->_scopes[$scope][$name]['m'] & self::LOCKED) {
// Clean the pref since it was just saved.
$this->_scopes[$scope][$name]['m'] &= ~self::DIRTY;
+
+ $updated[$name] = $this->_scopes[$scope][$name];
}
// Update the cache for this scope.
- $this->_cacheUpdate($scope, array_keys($prefs));
+ $this->_cache->update($scope, $updated);
}
}
}
// Update the cache for this scope.
- $this->_cacheUpdate($scope, array_keys($prefs));
+ $this->_cache->update($scope, $prefs);
}
}
}
// Update the cache for this scope.
- $this->_cacheUpdate($scope, array_keys($prefs));
+ $this->_cache->update($scope, $prefs);
}
}
// For each preference, check for an existing table row and
// update it if it's there, or create a new one if it's not.
foreach ($dirty_prefs as $scope => $prefs) {
+ $updated = array();
+
foreach ($prefs as $name => $pref) {
// Don't store locked preferences.
if ($this->_scopes[$scope][$name]['m'] & self::LOCKED) {
// Clean the pref since it was just saved.
$this->_scopes[$scope][$name]['m'] &= ~self::DIRTY;
+
+ $updated[$name] = $this->_scopes[$scope][$name];
}
// Update the cache for this scope.
- $this->_cacheUpdate($scope, array_keys($prefs));
+ $this->_cache->update($scope, $updated);
}
}
<api>beta</api>
</stability>
<license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Add array access API to Horde_Prefs.
+ <notes>* Abstract caching code into Horde_Prefs_Cache.
+ * Add array access API to Horde_Prefs.
* Remove dependency on horde/Core.
* Use Horde_Db as backend for Sql driver.
* Moved UI code to horde/Core.
<dir name="lib">
<dir name="Horde">
<dir name="Prefs">
+ <dir name="Cache">
+ <file name="Null.php" role="php" />
+ <file name="Session.php" role="php" />
+ </dir> <!-- /lib/Horde/Prefs/Cache -->
+ <file name="Cache.php" role="php" />
<file name="CategoryManager.php" role="php" />
<file name="Exception.php" role="php" />
<file name="File.php" role="php" />
</dependencies>
<phprelease>
<filelist>
+ <install name="lib/Horde/Prefs/Cache/Null.php" as="Horde/Prefs/Cache/Null.php" />
+ <install name="lib/Horde/Prefs/Cache/Session.php" as="Horde/Prefs/Cache/Session.php" />
+ <install name="lib/Horde/Prefs/Cache.php" as="Horde/Prefs/Cache.php" />
<install name="lib/Horde/Prefs/CategoryManager.php" as="Horde/Prefs/CategoryManager.php" />
<install name="lib/Horde/Prefs/Exception.php" as="Horde/Prefs/Exception.php" />
<install name="lib/Horde/Prefs/File.php" as="Horde/Prefs/File.php" />