[commits] [Wiki] changed: CustomizingPreferencesH5
Wiki Guest
wikiguest at horde.org
Thu Mar 6 09:06:02 UTC 2014
guest [194.95.66.21] Thu, 06 Mar 2014 09:06:02 +0000
Modified page: http://wiki.horde.org/CustomizingPreferencesH5
New Revision: 5
Change log: add caching
@@ -63,7 +63,38 @@
Because the prefs_init hook is called each time an user logins, line
12 shall prevent that the value is set to default each time, probably
overwriting the user's choice. The empty string is the default value
of the {{search_sources}}.
Line 13 collects all address books from Turba, the user has access
to. Line 14 prepares the string for the preference. These lines are
very preference-specific.
-Line 15 and 16 permanently write the settings into the preference
storage of the user. The next time the user logins, {{$value}} is no
longer empty, hence, the {{if()}} block is skipped and any newly
available address books are **not** automatically enabled. If you want
to keep the preference unchanged once the user has change the setting
manually, remove these two lines.
+Line 15 and 16 permanently write the settings into the preference
storage of the user. The next time the user logins, {{$value}} is no
longer empty, hence, the {{if()}} block is skipped and any newly
available address books are **not** automatically enabled. If you want
to keep the preference changing until the user changes the setting
manually, remove these two lines.
+++ Cache user information between prefs_init() calls
+Because preference values are requested one after another, the
function prefs_init() is called for each preference you have set
{{hook = true}}. If you query large latency databases, it can make
sense to query all user data at once and cache the result for the next
call of prefs_init. You can use a Horde case for example like so:
+<code type="php">
+function my_userinfo($uid) {
+ global $injector;
+ $cache = $injector->getInstance('Horde_Cache');
+ $key = 'my_userinfo.u.'.$uid;
+ if($cache->exists($key)) { // saved??
+ return unserialize($cache->get($key));
+ }
+
+ $userinfo = false;
+
+ // prepare the userinfo array here
+
+ if(isset($userinfo)) {
+ $cache->set($key, $userinfo, 20 * 60);
+ return unserialize($userinfo);
+ }
+ return false;
+}
+</code>
+
+Save this function in a PHP file and use it in all hooks.php:
+<code type="php">
+ require_once(HORDE_BASE . '/lib/my_lib.php');
+ $info = my_userinfo($username);
+ if(!isset($info) || $info === false || $info == '') {
+ return $value; // error getting user information
+ }
+</code>
More information about the commits
mailing list