plugins/libkolab
Aleksander Machniak
machniak at kolabsys.com
Wed Nov 20 10:43:28 CET 2013
plugins/libkolab/lib/kolab_storage_cache.php | 49 ++++++++++++++------------
plugins/libkolab/lib/kolab_storage_folder.php | 27 +++++++-------
2 files changed, 43 insertions(+), 33 deletions(-)
New commits:
commit a25fc1a961517e0898f535e271cd9248c09050b8
Author: Aleksander Machniak <machniak at kolabsys.com>
Date: Wed Nov 20 10:43:17 2013 +0100
Implement cache object update with UPDATE query instead of DELETE + INSERT (Request #2351)
diff --git a/plugins/libkolab/lib/kolab_storage_cache.php b/plugins/libkolab/lib/kolab_storage_cache.php
index c6d1267..31200ac 100644
--- a/plugins/libkolab/lib/kolab_storage_cache.php
+++ b/plugins/libkolab/lib/kolab_storage_cache.php
@@ -274,41 +274,48 @@ class kolab_storage_cache
/**
- * Insert a cache entry
+ * Insert (or update) a cache entry
*
- * @param string Related IMAP message UID
+ * @param int Related IMAP message UID
* @param mixed Hash array with object properties to save or false to delete the cache entry
+ * @param int Optional old message UID (for update)
*/
- public function insert($msguid, $object)
+ public function save($msguid, $object, $olduid)
{
// write to cache
if ($this->ready) {
$this->_read_folder_data();
$sql_data = $this->_serialize($object);
+ $sql_data['folder_id'] = $this->folder_id;
+ $sql_data['msguid'] = $msguid;
+ $sql_data['uid'] = $object['uid'];
- $extra_cols = $this->extra_cols ? ', ' . join(', ', $this->extra_cols) : '';
- $extra_fields = $this->extra_cols ? str_repeat(', ?', count($this->extra_cols)) : '';
+ $args = array();
+ $cols = array('folder_id', 'msguid', 'uid', 'changed', 'data', 'xml', 'tags', 'words');
+ $cols = array_merge($cols, $this->extra_cols);
- $args = array(
- "INSERT INTO $this->cache_table ".
- " (folder_id, msguid, uid, created, changed, data, xml, tags, words $extra_cols)".
- " VALUES (?, ?, ?, " . $this->db->now() . ", ?, ?, ?, ?, ? $extra_fields)",
- $this->folder_id,
- $msguid,
- $object['uid'],
- $sql_data['changed'],
- $sql_data['data'],
- $sql_data['xml'],
- $sql_data['tags'],
- $sql_data['words'],
- );
+ foreach ($cols as $idx => $col) {
+ $cols[$idx] = $this->db->quote_identifier($col);
+ $args[] = $sql_data[$col];
+ }
- foreach ($this->extra_cols as $col) {
- $args[] = $sql_data[$col];
+ if ($olduid) {
+ foreach ($cols as $idx => $col) {
+ $cols[$idx] = "$col = ?";
+ }
+
+ $query = "UPDATE $this->cache_table SET " . implode(', ', $cols)
+ . " WHERE folder_id = ? AND msguid = ?";
+ $args[] = $this->folder_id;
+ $args[] = $olduid;
+ }
+ else {
+ $query = "INSERT INTO $this->cache_table (created, " . implode(', ', $cols)
+ . ") VALUES (" . $this->db->now() . str_repeat(', ?', count($cols)) . ")";
}
- $result = call_user_func_array(array($this->db, 'query'), $args);
+ $result = $this->db->query($query, $args);
if (!$this->db->affected_rows($result)) {
rcube::raise_error(array(
diff --git a/plugins/libkolab/lib/kolab_storage_folder.php b/plugins/libkolab/lib/kolab_storage_folder.php
index 1eda5be..1eb409b 100644
--- a/plugins/libkolab/lib/kolab_storage_folder.php
+++ b/plugins/libkolab/lib/kolab_storage_folder.php
@@ -753,24 +753,27 @@ class kolab_storage_folder
$result = $this->imap->save_message($this->name, $raw_msg, null, false, null, null, $binary);
- // delete old message
- if ($result && !empty($object['_msguid']) && !empty($object['_mailbox'])) {
- $this->cache->bypass(true);
- $this->imap->delete_message($object['_msguid'], $object['_mailbox']);
- $this->cache->bypass(false);
- $this->cache->set($object['_msguid'], false, $object['_mailbox']);
- }
-
// update cache with new UID
if ($result) {
+ $old_uid = $object['_msguid'];
+
$object['_msguid'] = $result;
$object['_mailbox'] = $this->name;
- $this->cache->insert($result, $object);
- // remove temp file
- if ($body_file) {
- @unlink($body_file);
+ if ($old_uid) {
+ // delete old message
+ $this->cache->bypass(true);
+ $this->imap->delete_message($old_uid, $object['_mailbox']);
+ $this->cache->bypass(false);
}
+
+ // insert/update message in cache
+ $this->cache->save($result, $object, $old_uid);
+ }
+
+ // remove temp file
+ if ($body_file) {
+ @unlink($body_file);
}
}
More information about the commits
mailing list