Aleksander Machniak
2013-10-07 aceb0149b8f7f6c57e8dd6ba6f69aa0d6cacc6bc
Add possibility to programmatically set cache mode, so it is possible
to cache only indexes/threads but not messages
2 files modified
57 ■■■■■ changed files
program/lib/Roundcube/rcube_imap.php 7 ●●●● patch | view | raw | blame | history
program/lib/Roundcube/rcube_imap_cache.php 50 ●●●●● patch | view | raw | blame | history
program/lib/Roundcube/rcube_imap.php
@@ -3761,11 +3761,16 @@
     * Enable or disable messages caching
     *
     * @param boolean $set Flag
     * @param int     $mode Cache mode
     */
    public function set_messages_caching($set)
    public function set_messages_caching($set, $mode = null)
    {
        if ($set) {
            $this->messages_caching = true;
            if ($mode && ($cache = $this->get_mcache_engine())) {
                $cache->set_mode($mode);
            }
        }
        else {
            if ($this->mcache) {
program/lib/Roundcube/rcube_imap_cache.php
@@ -27,6 +27,9 @@
 */
class rcube_imap_cache
{
    const MODE_INDEX   = 1;
    const MODE_MESSAGE = 2;
    /**
     * Instance of rcube_imap
     *
@@ -70,6 +73,7 @@
    private $icache = array();
    private $skip_deleted = false;
    private $mode;
    /**
     * List of known flags. Thanks to this we can handle flag changes
@@ -95,6 +99,7 @@
    );
    /**
     * Object constructor.
     *
@@ -117,6 +122,9 @@
        $this->skip_deleted = $skip_deleted;
        $this->ttl          = $ttl;
        $this->threshold    = $threshold;
        // cache all possible information by default
        $this->mode = self::MODE_INDEX | self::MODE_MESSAGE;
    }
@@ -127,6 +135,17 @@
    {
        $this->save_icache();
        $this->icache = null;
    }
    /**
     * Set cache mode
     *
     * @param int $mode Cache mode
     */
    public function set_mode($mode)
    {
        $this->mode = $mode;
    }
@@ -308,6 +327,10 @@
            return array();
        }
        $msgs   = array_flip($msgs);
        $result = array();
        if ($this->mode & self::MODE_MESSAGE) {
        // Fetch messages from cache
        $sql_result = $this->db->query(
            "SELECT uid, data, flags"
@@ -316,9 +339,6 @@
                ." AND mailbox = ?"
                ." AND uid IN (".$this->db->array2list($msgs, 'integer').")",
            $this->userid, $mailbox);
        $msgs   = array_flip($msgs);
        $result = array();
        while ($sql_arr = $this->db->fetch_assoc($sql_result)) {
            $uid          = intval($sql_arr['uid']);
@@ -331,6 +351,7 @@
                unset($msgs[$uid]);
            }
        }
        }
        // Fetch not found messages from IMAP server
        if (!empty($msgs)) {
@@ -339,7 +360,10 @@
            // Insert to DB and add to result list
            if (!empty($messages)) {
                foreach ($messages as $msg) {
                    if ($this->mode & self::MODE_MESSAGE) {
                    $this->add_message($mailbox, $msg, !array_key_exists($msg->uid, $result));
                    }
                    $result[$msg->uid] = $msg;
                }
            }
@@ -370,6 +394,7 @@
            return $this->icache['__message']['object'];
        }
        if ($this->mode & self::MODE_MESSAGE) {
        $sql_result = $this->db->query(
            "SELECT flags, data"
            ." FROM ".$this->db->table_name('cache_messages')
@@ -382,11 +407,16 @@
            $message = $this->build_message($sql_arr);
            $found   = true;
        }
        }
        // Get the message from IMAP server
        if (empty($message) && $update) {
            $message = $this->imap->get_message_headers($uid, $mailbox, true);
            // cache will be updated in close(), see below
        }
        if (!($this->mode & self::MODE_MESSAGE)) {
            return $message;
        }
        // Save the message in internal cache, will be written to DB in close()
@@ -421,6 +451,10 @@
    function add_message($mailbox, $message, $force = false)
    {
        if (!is_object($message) || empty($message->uid)) {
            return;
        }
        if (!($this->mode & self::MODE_MESSAGE)) {
            return;
        }
@@ -495,6 +529,10 @@
            return;
        }
        if (!($this->mode & self::MODE_MESSAGE)) {
            return;
        }
        $flag = strtoupper($flag);
        $idx  = (int) array_search($flag, $this->flags);
        $uids = (array) $uids;
@@ -535,6 +573,10 @@
     */
    function remove_message($mailbox = null, $uids = null)
    {
        if (!($this->mode & self::MODE_MESSAGE)) {
            return;
        }
        if (!strlen($mailbox)) {
            $this->db->query(
                "DELETE FROM ".$this->db->table_name('cache_messages')
@@ -1036,6 +1078,7 @@
        $removed = array();
        // Get known UIDs
        if ($this->mode & self::MODE_MESSAGE) {
        $sql_result = $this->db->query(
            "SELECT uid"
            ." FROM ".$this->db->table_name('cache_messages')
@@ -1046,6 +1089,7 @@
        while ($sql_arr = $this->db->fetch_assoc($sql_result)) {
            $uids[] = $sql_arr['uid'];
        }
        }
        // Synchronize messages data
        if (!empty($uids)) {