From ad84f9c06c14b70f6f764df1f77b964d65db1f99 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Mon, 22 Jun 2009 14:32:51 -0400
Subject: [PATCH] - performance improvements of messages caching

---
 program/include/rcube_imap.php |   70 +++++++++++++++++------------------
 1 files changed, 34 insertions(+), 36 deletions(-)

diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index 76aa853..87742ab 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -863,7 +863,6 @@
       }
 
     // fetch complete message index
-    $msg_count = $this->_messagecount($mailbox);
     if ($this->get_capability('sort') && ($a_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : '')))
       {
       if ($this->sort_order == 'DESC')
@@ -873,7 +872,7 @@
       }
     else
       {
-      $a_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, "1:$msg_count", $this->sort_field, $this->skip_deleted);
+      $a_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, "1:*", $this->sort_field, $this->skip_deleted);
 
       if ($this->sort_order=="ASC")
         asort($a_index);
@@ -919,25 +918,25 @@
       // other message at this position
       if (isset($cache_index[$id]))
         {
-        $this->remove_message_cache($cache_key, $cache_index[$id]);
+	$for_remove[] = $cache_index[$id];
         unset($cache_index[$id]);
         }
         
-	$toupdate[] = $id;
+	$for_update[] = $id;
       }
 
+    // clear messages at wrong positions and those deleted that are still in cache_index      
+    if (!empty($for_remove))
+      $cache_index = array_merge($cache_index, $for_remove);
+    
+    if (!empty($cache_index))
+      $this->remove_message_cache($cache_key, $cache_index);
+
     // fetch complete headers and add to cache
-    if (!empty($toupdate)) {
-      if ($headers = iil_C_FetchHeader($this->conn, $mailbox, join(',', $toupdate), false, $this->fetch_add_headers))
+    if (!empty($for_update)) {
+      if ($headers = iil_C_FetchHeader($this->conn, $mailbox, join(',', $for_update), false, $this->fetch_add_headers))
         foreach ($headers as $header)
           $this->add_message_cache($cache_key, $header->id, $header);
-      }
-    
-    // those ids that are still in cache_index have been deleted      
-    if (!empty($cache_index))
-      {
-      foreach ($cache_index as $id => $uid)
-        $this->remove_message_cache($cache_key, $uid);
       }
     }
 
@@ -963,7 +962,7 @@
 
     // try search with US-ASCII charset (should be supported by server)
     // only if UTF-8 search is not supported
-    if (empty($results) && !is_array($results) && !empty($charset) && $charset!='US-ASCII')
+    if (empty($results) && !is_array($results) && !empty($charset) && $charset != 'US-ASCII')
       {
 	// convert strings to US_ASCII
         if(preg_match_all('/\{([0-9]+)\}\r\n/', $str, $matches, PREG_OFFSET_CAPTURE))
@@ -984,7 +983,7 @@
 	else // strings for conversion not found
 	  $res = $str;
 	  
-	$results = $this->search($mbox_name, $res, 'US-ASCII', $sort_field);
+	$results = $this->search($mbox_name, $res, NULL, $sort_field);
       }
 
     $this->set_search_set($str, $results, $charset, $sort_field);
@@ -1549,9 +1548,7 @@
     $cache_key = $mailbox.'.msg';
     if ($this->caching_enabled)
       {
-      foreach ($uids as $uid)
-        if ($cached_headers = $this->get_cached_message($cache_key, $uid))
-          $this->remove_message_cache($cache_key, $uid);
+      $this->remove_message_cache($cache_key, $uids);
 
       // close and re-open connection
       // this prevents connection problems with Courier 
@@ -1660,7 +1657,8 @@
         }
 
       // clear cache from the lowest index on
-      $this->clear_message_cache($cache_key, $start_index);
+      if ($start_index < 100000)
+        $this->clear_message_cache($cache_key, $start_index);
       }
 
     return $moved;
@@ -1715,7 +1713,8 @@
         }
 
       // clear cache from the lowest index on
-      $this->clear_message_cache($cache_key, $start_index);
+      if ($start_index < 100000)
+        $this->clear_message_cache($cache_key, $start_index);
       }
 
     return $deleted;
@@ -1963,7 +1962,7 @@
   /**
    * Remove mailboxes from server
    *
-   * @param string Mailbox name
+   * @param string Mailbox name(s) string/array
    * @return boolean True on success
    */
   function delete_mailbox($mbox_name)
@@ -1987,9 +1986,11 @@
 
         // send delete command to server
         $result = iil_C_DeleteFolder($this->conn, $mailbox);
-        if ($result>=0)
+        if ($result >= 0) {
           $deleted = TRUE;
-
+          $this->clear_message_cache($mailbox.'.msg');
+	  }
+	  
         foreach ($all_mboxes as $c_mbox)
           {
           $regex = preg_quote($mailbox . $this->delimiter, '/');
@@ -1998,18 +1999,17 @@
             {
             iil_C_UnSubscribe($this->conn, $c_mbox);
             $result = iil_C_DeleteFolder($this->conn, $c_mbox);
-            if ($result>=0)
+            if ($result >= 0) {
               $deleted = TRUE;
-            }
+    	      $this->clear_message_cache($c_mbox.'.msg');
+              }
+	    }
           }
         }
 
     // clear mailboxlist cache
     if ($deleted)
-      {
-      $this->clear_message_cache($mailbox.'.msg');
       $this->clear_cache('mailboxes');
-      }
 
     return $deleted;
     }
@@ -2324,7 +2324,6 @@
         $_SESSION['user_id'],
         $key,
         $uid);
-
       if ($sql_arr = $this->db->fetch_assoc($sql_result))
         {
         $this->cache[$internal_key][$uid] = unserialize($sql_arr['headers']);
@@ -2431,19 +2430,18 @@
   /**
    * @access private
    */
-  function remove_message_cache($key, $uid)
+  function remove_message_cache($key, $uids)
     {
     if (!$this->caching_enabled)
       return;
     
     $this->db->query(
       "DELETE FROM ".get_table_name('messages')."
-       WHERE  user_id=?
-       AND    cache_key=?
-       AND    uid=?",
+      WHERE  user_id=?
+      AND    cache_key=?
+      AND    uid IN (".$this->db->array2list($uids, 'integer').")",
       $_SESSION['user_id'],
-      $key,
-      $uid);
+      $key);
     }
 
   /**
@@ -2689,7 +2687,7 @@
 
 
   /**
-   * Convert body charset to UTF-8 according to the ctype_parameters
+   * Convert body charset to RCMAIL_CHARSET according to the ctype_parameters
    *
    * @param string Part body to decode
    * @param string Charset to convert from

--
Gitblit v1.9.1