From d342f8f0315e535e71555ba908267b30af8daf88 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Thu, 06 Oct 2011 11:49:33 -0400
Subject: [PATCH] - Improve performance by storing sorted mailbox list in the cache

---
 program/include/rcube_imap.php |   55 +++++++++++++++++++++++++++++--------------------------
 1 files changed, 29 insertions(+), 26 deletions(-)

diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index 859c089..8dfffe8 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -2920,22 +2920,47 @@
      * @param   string  $rights    Optional ACL requirements
      * @param   bool    $skip_sort Enable to return unsorted list (for better performance)
      *
-     * @return  array   List of mailboxes/folders
+     * @return  array   List of folders
      * @access  public
      */
     function list_mailboxes($root='', $name='*', $filter=null, $rights=null, $skip_sort=false)
     {
+        $cache_key = $root.':'.$name;
+        if (!empty($filter)) {
+            $cache_key .= ':'.(is_string($filter) ? $filter : serialize($filter));
+        }
+        $cache_key .= ':'.$rights;
+        $cache_key = 'mailboxes.'.md5($cache_key);
+
+        // get cached folder list
+        $a_mboxes = $this->get_cache($cache_key);
+        if (is_array($a_mboxes)) {
+            return $a_mboxes;
+        }
+
         $a_mboxes = $this->_list_mailboxes($root, $name, $filter, $rights);
+
+        if (!is_array($a_mboxes)) {
+            return array();
+        }
+
+        // filter folders list according to rights requirements
+        if ($rights && $this->get_capability('ACL')) {
+            $a_mboxes = $this->filter_rights($a_mboxes, $rights);
+        }
 
         // INBOX should always be available
         if ((!$filter || $filter == 'mail') && !in_array('INBOX', $a_mboxes)) {
             array_unshift($a_mboxes, 'INBOX');
         }
 
-        // sort mailboxes
-        if (!$skip_sort) {
+        // sort mailboxes (always sort for cache)
+        if (!$skip_sort || $this->cache) {
             $a_mboxes = $this->_sort_mailbox_list($a_mboxes);
         }
+
+        // write mailboxlist to cache
+        $this->update_cache($cache_key, $a_mboxes);
 
         return $a_mboxes;
     }
@@ -2955,20 +2980,6 @@
      */
     private function _list_mailboxes($root='', $name='*', $filter=null, $rights=null)
     {
-        $cache_key = $root.':'.$name;
-        if (!empty($filter)) {
-            $cache_key .= ':'.(is_string($filter) ? $filter : serialize($filter));
-        }
-        $cache_key .= ':'.$rights;
-
-        $cache_key = 'mailboxes.'.md5($cache_key);
-
-        // get cached folder list
-        $a_mboxes = $this->get_cache($cache_key);
-        if (is_array($a_mboxes)) {
-            return $a_mboxes;
-        }
-
         $a_defaults = $a_out = array();
 
         // Give plugins a chance to provide a list of mailboxes
@@ -2979,7 +2990,7 @@
             $a_folders = $data['folders'];
         }
         else if (!$this->conn->connected()) {
-           return array();
+           return null;
         }
         else {
             // Server supports LIST-EXTENDED, we can use selection options
@@ -3026,14 +3037,6 @@
         if (!is_array($a_folders) || !sizeof($a_folders)) {
             $a_folders = array();
         }
-
-        // filter folders list according to rights requirements
-        if ($rights && $this->get_capability('ACL')) {
-            $a_folders = $this->filter_rights($a_folders, $rights);
-        }
-
-        // write mailboxlist to cache
-        $this->update_cache($cache_key, $a_folders);
 
         return $a_folders;
     }

--
Gitblit v1.9.1