summaryrefslogtreecommitdiff
path: root/program/lib/Roundcube/rcube_imap_cache.php
diff options
context:
space:
mode:
authorAleksander Machniak <alec@alec.pl>2013-10-07 09:26:37 +0200
committerAleksander Machniak <alec@alec.pl>2013-10-07 09:26:37 +0200
commitaceb0149b8f7f6c57e8dd6ba6f69aa0d6cacc6bc (patch)
tree521486fa51c44aecde04b782af32c975a5067d69 /program/lib/Roundcube/rcube_imap_cache.php
parent4daaa09438bc05f9d5d6cf339cc0b60b511057e9 (diff)
Add possibility to programmatically set cache mode, so it is possible
to cache only indexes/threads but not messages
Diffstat (limited to 'program/lib/Roundcube/rcube_imap_cache.php')
-rw-r--r--program/lib/Roundcube/rcube_imap_cache.php114
1 files changed, 79 insertions, 35 deletions
diff --git a/program/lib/Roundcube/rcube_imap_cache.php b/program/lib/Roundcube/rcube_imap_cache.php
index d72bfe0ab..445d46360 100644
--- a/program/lib/Roundcube/rcube_imap_cache.php
+++ b/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 @@ class rcube_imap_cache
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 @@ class rcube_imap_cache
);
+
/**
* Object constructor.
*
@@ -117,6 +122,9 @@ class rcube_imap_cache
$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;
}
@@ -131,6 +139,17 @@ class rcube_imap_cache
/**
+ * Set cache mode
+ *
+ * @param int $mode Cache mode
+ */
+ public function set_mode($mode)
+ {
+ $this->mode = $mode;
+ }
+
+
+ /**
* Return (sorted) messages index (UIDs).
* If index doesn't exist or is invalid, will be updated.
*
@@ -308,27 +327,29 @@ class rcube_imap_cache
return array();
}
- // Fetch messages from cache
- $sql_result = $this->db->query(
- "SELECT uid, data, flags"
- ." FROM ".$this->db->table_name('cache_messages')
- ." WHERE user_id = ?"
- ." 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']);
- $result[$uid] = $this->build_message($sql_arr);
+ if ($this->mode & self::MODE_MESSAGE) {
+ // Fetch messages from cache
+ $sql_result = $this->db->query(
+ "SELECT uid, data, flags"
+ ." FROM ".$this->db->table_name('cache_messages')
+ ." WHERE user_id = ?"
+ ." AND mailbox = ?"
+ ." AND uid IN (".$this->db->array2list($msgs, 'integer').")",
+ $this->userid, $mailbox);
+
+ while ($sql_arr = $this->db->fetch_assoc($sql_result)) {
+ $uid = intval($sql_arr['uid']);
+ $result[$uid] = $this->build_message($sql_arr);
- if (!empty($result[$uid])) {
- // save memory, we don't need message body here (?)
- $result[$uid]->body = null;
+ if (!empty($result[$uid])) {
+ // save memory, we don't need message body here (?)
+ $result[$uid]->body = null;
- unset($msgs[$uid]);
+ unset($msgs[$uid]);
+ }
}
}
@@ -339,7 +360,10 @@ class rcube_imap_cache
// Insert to DB and add to result list
if (!empty($messages)) {
foreach ($messages as $msg) {
- $this->add_message($mailbox, $msg, !array_key_exists($msg->uid, $result));
+ if ($this->mode & self::MODE_MESSAGE) {
+ $this->add_message($mailbox, $msg, !array_key_exists($msg->uid, $result));
+ }
+
$result[$msg->uid] = $msg;
}
}
@@ -370,17 +394,19 @@ class rcube_imap_cache
return $this->icache['__message']['object'];
}
- $sql_result = $this->db->query(
- "SELECT flags, data"
- ." FROM ".$this->db->table_name('cache_messages')
- ." WHERE user_id = ?"
- ." AND mailbox = ?"
- ." AND uid = ?",
- $this->userid, $mailbox, (int)$uid);
+ if ($this->mode & self::MODE_MESSAGE) {
+ $sql_result = $this->db->query(
+ "SELECT flags, data"
+ ." FROM ".$this->db->table_name('cache_messages')
+ ." WHERE user_id = ?"
+ ." AND mailbox = ?"
+ ." AND uid = ?",
+ $this->userid, $mailbox, (int)$uid);
- if ($sql_arr = $this->db->fetch_assoc($sql_result)) {
- $message = $this->build_message($sql_arr);
- $found = true;
+ if ($sql_arr = $this->db->fetch_assoc($sql_result)) {
+ $message = $this->build_message($sql_arr);
+ $found = true;
+ }
}
// Get the message from IMAP server
@@ -389,6 +415,10 @@ class rcube_imap_cache
// 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()
// Common scenario: user opens unseen message
// - get message (SELECT)
@@ -424,6 +454,10 @@ class rcube_imap_cache
return;
}
+ if (!($this->mode & self::MODE_MESSAGE)) {
+ return;
+ }
+
$flags = 0;
$msg = clone $message;
@@ -495,6 +529,10 @@ class rcube_imap_cache
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 @@ class rcube_imap_cache
*/
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,15 +1078,17 @@ class rcube_imap_cache
$removed = array();
// Get known UIDs
- $sql_result = $this->db->query(
- "SELECT uid"
- ." FROM ".$this->db->table_name('cache_messages')
- ." WHERE user_id = ?"
- ." AND mailbox = ?",
- $this->userid, $mailbox);
+ if ($this->mode & self::MODE_MESSAGE) {
+ $sql_result = $this->db->query(
+ "SELECT uid"
+ ." FROM ".$this->db->table_name('cache_messages')
+ ." WHERE user_id = ?"
+ ." AND mailbox = ?",
+ $this->userid, $mailbox);
- while ($sql_arr = $this->db->fetch_assoc($sql_result)) {
- $uids[] = $sql_arr['uid'];
+ while ($sql_arr = $this->db->fetch_assoc($sql_result)) {
+ $uids[] = $sql_arr['uid'];
+ }
}
// Synchronize messages data