/** * Checks if the cache is up-to-date * * @param string Mailbox name * @param string Internal cache key * @return int Cache status: -3 = off, -2 = incomplete, -1 = dirty */ private function check_cache_status($mailbox, $cache_key) { if (!$this->caching_enabled) { return -3; } $cache_index = $this->get_message_cache_index($cache_key); $msg_count = $this->_messagecount($mailbox); $cache_count = count($cache_index); // empty mailbox if (!$msg_count) { return $cache_count ? -2 : 1; } // @TODO: We've got one big performance problem in cache status checking method // E.g. mailbox contains 1000 messages, in cache table we've got first 100 // of them. Now if we want to display only that 100 (which we've got) // check_cache_status returns 'incomplete' and messages are fetched // from IMAP instead of DB. if ($cache_count == $msg_count) { if ($this->skip_deleted) { $h_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, "1:*", 'UID', $this->skip_deleted); if (sizeof($h_index) == $cache_count) { $cache_index = array_flip($cache_index); foreach ($h_index as $idx => $uid) { unset($cache_index[$uid]); } if (empty($cache_index)) { return 1; } } return -2; } else { // get UID of message with highest index $uid = iil_C_ID2UID($this->conn, $mailbox, $msg_count); $cache_uid = array_pop($cache_index); // uids of highest message matches -> cache seems OK if ($cache_uid == $uid) { return 1; } } // cache is dirty return -1; } else { if (abs($msg_count - $cache_count) < $msg_count / 10) { return -1; } else { return -2; } } }
/** * @access private */ function sync_header_index($mailbox) { $cache_key = $mailbox . '.msg'; $cache_index = $this->get_message_cache_index($cache_key); $msg_count = $this->_messagecount($mailbox); // fetch complete message index $a_message_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, "1:{$msg_count}", 'UID'); foreach ($a_message_index as $id => $uid) { // message in cache at correct position if ($cache_index[$id] == $uid) { unset($cache_index[$id]); continue; } // message in cache but in wrong position if (in_array((string) $uid, $cache_index, TRUE)) { unset($cache_index[$id]); } // other message at this position if (isset($cache_index[$id])) { $this->remove_message_cache($cache_key, $id); unset($cache_index[$id]); } // fetch complete headers and add to cache $headers = iil_C_FetchHeader($this->conn, $mailbox, $id); $this->add_message_cache($cache_key, $headers->id, $headers); } // 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, $id); } } }