From f410c902613e3e1f0d57c71b1065b69232ba92eb Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Tue, 13 Nov 2012 09:26:51 -0500
Subject: [PATCH] Cache identities data in memory for faster access when get_identity() is called more than once

---
 program/include/rcube_user.php |   25 +++++++++++++++++++++++--
 1 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/program/include/rcube_user.php b/program/include/rcube_user.php
index b92187a..72b03cd 100644
--- a/program/include/rcube_user.php
+++ b/program/include/rcube_user.php
@@ -47,6 +47,13 @@
      */
     private $rc;
 
+    /**
+     * Internal identities cache
+     *
+     * @var array
+     */
+    private $identities = array();
+
     const SEARCH_ADDRESSBOOK = 1;
     const SEARCH_MAIL = 2;
 
@@ -213,8 +220,14 @@
      */
     function get_identity($id = null)
     {
-        $result = $this->list_identities($id ? sprintf('AND identity_id = %d', $id) : '');
-        return $result[0];
+        $id = (int)$id;
+        // cache identities for better performance
+        if (!array_key_exists($id, $this->identities)) {
+            $result = $this->list_identities($id ? 'AND identity_id = ' . $id : '');
+            $this->identities[$id] = $result[0];
+        }
+
+        return $this->identities[$id];
     }
 
 
@@ -273,6 +286,8 @@
         call_user_func_array(array($this->db, 'query'),
             array_merge(array($sql), $query_params));
 
+        $this->identities = array();
+
         return $this->db->affected_rows();
     }
 
@@ -304,6 +319,8 @@
 
         call_user_func_array(array($this->db, 'query'),
             array_merge(array($sql), $insert_values));
+
+        $this->identities = array();
 
         return $this->db->insert_id('identities');
     }
@@ -339,6 +356,8 @@
             $this->ID,
             $iid);
 
+        $this->identities = array();
+
         return $this->db->affected_rows();
     }
 
@@ -359,6 +378,8 @@
                     " AND del <> 1",
                 $this->ID,
                 $iid);
+
+            unset($this->identities[0]);
         }
     }
 

--
Gitblit v1.9.1