Index: /trunk/roundcubemail/program/include/rcube_imap.php
===================================================================
--- /trunk/roundcubemail/program/include/rcube_imap.php	(revision 2675)
+++ /trunk/roundcubemail/program/include/rcube_imap.php	(revision 2676)
@@ -795,5 +795,6 @@
         // add message to cache
         if ($this->caching_enabled && $cache_index[$headers->id] != $headers->uid)
-          $this->add_message_cache($cache_key, $headers->id, $headers);
+          $this->add_message_cache($cache_key, $headers->id, $headers, NULL,
+		!in_array((string)$headers->uid, $cache_index, true));
 
         $a_msg_headers[$headers->uid] = $headers;
@@ -913,5 +914,5 @@
       if (in_array((string)$uid, $cache_index, TRUE))
         {
-        unset($cache_index[$id]);        
+        unset($cache_index[$id]);
         }
       
@@ -937,5 +938,6 @@
       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);
+          $this->add_message_cache($cache_key, $header->id, $header, NULL,
+		in_array((string)$header->uid, $for_remove, true));
       }
     }
@@ -1086,5 +1088,5 @@
         $this->uid_id_map[$mailbox][$headers->uid] = $headers->id;
 
-      $this->add_message_cache($mailbox.'.msg', $headers->id, $headers);
+      $this->add_message_cache($mailbox.'.msg', $headers->id, $headers, NULL, true);
       }
 
@@ -1104,5 +1106,5 @@
     {
     $cache_key = $this->mailbox.'.msg';
-    $headers = &$this->get_cached_message($cache_key, $uid, true);
+    $headers = &$this->get_cached_message($cache_key, $uid);
 
     // return cached message structure
@@ -2201,5 +2203,5 @@
    * @return int -3 = off, -2 = incomplete, -1 = dirty
    */
-  function check_cache_status($mailbox, $cache_key)
+  private function check_cache_status($mailbox, $cache_key)
   {
     if (!$this->caching_enabled)
@@ -2247,5 +2249,5 @@
    * @access private
    */
-  function get_message_cache($key, $from, $to, $sort_field, $sort_order)
+  private function get_message_cache($key, $from, $to, $sort_field, $sort_order)
     {
     $cache_key = "$key:$from:$to:$sort_field:$sort_order";
@@ -2286,5 +2288,5 @@
    * @access private
    */
-  function &get_cached_message($key, $uid, $struct=false)
+  private function &get_cached_message($key, $uid)
     {
     $internal_key = '__single_msg';
@@ -2293,7 +2295,6 @@
         ($struct && empty($this->cache[$internal_key][$uid]->structure))))
       {
-      $sql_select = "idx, uid, headers" . ($struct ? ", structure" : '');
       $sql_result = $this->db->query(
-        "SELECT $sql_select
+        "SELECT idx, headers, structure
          FROM ".get_table_name('messages')."
          WHERE  user_id=?
@@ -2303,6 +2304,8 @@
         $key,
         $uid);
+
       if ($sql_arr = $this->db->fetch_assoc($sql_result))
         {
+	$this->uid_id_map[preg_replace('/\.msg$/', '', $key)][$uid] = $sql_arr['idx'];
         $this->cache[$internal_key][$uid] = $this->db->decode(unserialize($sql_arr['headers']));
         if (is_object($this->cache[$internal_key][$uid]) && !empty($sql_arr['structure']))
@@ -2317,5 +2320,5 @@
    * @access private
    */  
-  function get_message_cache_index($key, $force=FALSE, $sort_field='idx', $sort_order='ASC')
+  private function get_message_cache_index($key, $force=FALSE, $sort_field='idx', $sort_order='ASC')
     {
     static $sa_message_index = array();
@@ -2347,5 +2350,5 @@
    * @access private
    */
-  private function add_message_cache($key, $index, $headers, $struct=null)
+  private function add_message_cache($key, $index, $headers, $struct=null, $force=false)
     {
     if (empty($key) || !is_object($headers) || empty($headers->uid))
@@ -2353,5 +2356,5 @@
 
     // add to internal (fast) cache
-    $this->cache['__single_msg'][$headers->uid] = $headers;
+    $this->cache['__single_msg'][$headers->uid] = clone $headers;
     $this->cache['__single_msg'][$headers->uid]->structure = $struct;
 
@@ -2361,5 +2364,6 @@
     
     // check for an existing record (probly headers are cached but structure not)
-    $sql_result = $this->db->query(
+    if (!$force) {
+      $sql_result = $this->db->query(
         "SELECT message_id
          FROM ".get_table_name('messages')."
@@ -2371,7 +2375,10 @@
         $key,
         $headers->uid);
+      if ($sql_arr = $this->db->fetch_assoc($sql_result))
+        $message_id = $sql_arr['message_id'];
+      }
 
     // update cache record
-    if ($sql_arr = $this->db->fetch_assoc($sql_result))
+    if ($message_id)
       {
       $this->db->query(
@@ -2382,5 +2389,5 @@
         serialize($this->db->encode(clone $headers)),
         is_object($struct) ? serialize($this->db->encode(clone $struct)) : NULL,
-        $sql_arr['message_id']
+        $message_id
         );
       }
@@ -2395,5 +2402,4 @@
         $index,
         $headers->uid,
-
         (string)rc_substr($this->db->encode($this->decode_header($headers->subject, TRUE)), 0, 128),
         (string)rc_substr($this->db->encode($this->decode_header($headers->from, TRUE)), 0, 128),
