[opw-web] Cache role information



commit b3bbd547c1398f1f784e4dcb4581ae3c1d229723
Author: Owen W. Taylor <otaylor fishsoup net>
Date:   Fri Apr 4 10:37:55 2014 -0400

    Cache role information
    
    Instead of looking up in the disk cache or database every time
    $user->get_role() is called, keep a local cache. (We look up the
    role for each template we parse - which might be many for a
    page.)

 classes/class_user.php |   37 +++++++++++++++++++++++--------------
 1 files changed, 23 insertions(+), 14 deletions(-)
---
diff --git a/classes/class_user.php b/classes/class_user.php
index 1a169c0..935b6c5 100644
--- a/classes/class_user.php
+++ b/classes/class_user.php
@@ -27,6 +27,7 @@ class user
         $this->is_admin = false;
         $this->is_logged_in = false;
         $this->max_age = time() - 24 * 60 * 60;
+        $this->_role_data = array();
 
         if (!isset($config->server_secret))
         {
@@ -523,20 +524,28 @@ class user
         if ($this->username === null)
             return;
 
-        $key = "role_{$this->username}_{$program_id}";
-        $role_data = $cache->get($key, 'roles');
+        // We frequently get the role from scratch for the same program ID;
+        // even if the disk cache isn't on, we want to make sure to do this
+        // only once per-session, so keep a local cache.
 
-        if (!$role_data)
-        {
-            $sql = "SELECT role, organization_id FROM {$db->prefix}roles " .
-                       "WHERE username = :username " .
-                       "AND program_id = :id";
-
-            $role_data = $db->query($sql,
-                                    array('username' => $this->username,
-                                          'id' => $program_id),
-                                    true);
-            $cache->put($key, $role_data, 'roles');
+        if (isset($this->_roledata[$program_id]))
+          $role_data = $this->_roledata[$program_id];
+        else {
+            $key = "role_{$this->username}_{$program_id}";
+            $role_data = $cache->get($key, 'roles');
+            if (!$role_data)
+            {
+                $sql = "SELECT role, organization_id FROM {$db->prefix}roles " .
+                           "WHERE username = :username " .
+                           "AND program_id = :id";
+
+                $role_data = $db->query($sql,
+                                        array('username' => $this->username,
+                                              'id' => $program_id),
+                                        true);
+                $cache->put($key, $role_data, 'roles');
+            }
+            $this->_roledata[$program_id] = $role_data;
         }
 
         // Check if we have a role


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]