2 commits - plugins/libkolab
Thomas Brüderli
bruederli at kolabsys.com
Sat May 26 15:22:53 CEST 2012
plugins/libkolab/lib/kolab_storage_cache.php | 77 ++++++++++++++++++++++-----
1 file changed, 65 insertions(+), 12 deletions(-)
New commits:
commit b5b26e6800ee85222f1c95844bb3c692b0844241
Author: Thomas Bruederli <bruederli at kolabsys.com>
Date: Sat May 26 15:20:41 2012 +0200
Always add type parameter to cache queries (#799); remove last parameter from set()
diff --git a/plugins/libkolab/lib/kolab_storage_cache.php b/plugins/libkolab/lib/kolab_storage_cache.php
index 045dba4..5f13547 100644
--- a/plugins/libkolab/lib/kolab_storage_cache.php
+++ b/plugins/libkolab/lib/kolab_storage_cache.php
@@ -190,19 +190,19 @@ class kolab_storage_cache
* @param mixed Hash array with object properties to save or false to delete the cache entry
* @param string IMAP folder name the entry relates to
*/
- public function set($msguid, $object, $foldername = null, $mcache = true)
+ public function set($msguid, $object, $foldername = null)
{
// delegate to another cache instance
if ($foldername && $foldername != $this->folder->name) {
kolab_storage::get_folder($foldername)->cache->set($msguid, $object);
return;
}
-
+
// write to cache
if ($this->ready) {
// remove old entry
- $this->db->query("DELETE FROM kolab_cache WHERE resource=? AND msguid=?",
- $this->resource_uri, $msguid);
+ $this->db->query("DELETE FROM kolab_cache WHERE resource=? AND msguid=? AND type<>?",
+ $this->resource_uri, $msguid, 'lock');
// write new object data if not false (wich means deleted)
if ($object) {
@@ -235,8 +235,7 @@ class kolab_storage_cache
}
// keep a copy in memory for fast access
- if ($mcache)
- $this->objects[$msguid] = $object;
+ $this->objects[$msguid] = $object;
if ($object)
$this->uid2msg[$object['uid']] = $msguid;
@@ -257,11 +256,12 @@ class kolab_storage_cache
if ($new_msguid = $target->cache->uid2msguid($objuid)) {
$this->db->query(
"UPDATE kolab_cache SET resource=?, msguid=? ".
- "WHERE resource=? AND msguid=?",
+ "WHERE resource=? AND msguid=? AND type<>?",
$target->get_resource_uri(),
$new_msguid,
$this->resource_uri,
- $msguid
+ $msguid,
+ 'lock'
);
}
else {
@@ -420,7 +420,7 @@ class kolab_storage_cache
foreach ((array)$index as $msguid) {
if ($object = $this->folder->read_object($msguid, $type, $folder)) {
$results[] = $object;
- $this->set($msguid, $object, null, false);
+ $this->set($msguid, $object);
}
}
commit 27a4061c995bab7f772466273f3f562836c0c0e2
Author: Thomas Bruederli <bruederli at kolabsys.com>
Date: Sat May 26 15:15:17 2012 +0200
Optimize cache synchronization by using extended SQL inserts (#798)
diff --git a/plugins/libkolab/lib/kolab_storage_cache.php b/plugins/libkolab/lib/kolab_storage_cache.php
index b96c8be..045dba4 100644
--- a/plugins/libkolab/lib/kolab_storage_cache.php
+++ b/plugins/libkolab/lib/kolab_storage_cache.php
@@ -35,7 +35,7 @@ class kolab_storage_cache
private $synched = false;
private $synclock = false;
private $ready = false;
-
+ private $max_sql_packet = 1046576; // 1 MB - 2000 bytes
private $binary_cols = array('photo','pgppublickey','pkcs7publickey');
@@ -116,8 +116,12 @@ class kolab_storage_cache
}
// fetch new objects from imap
- $fetch_index = array_diff($this->index, $old_index);
- $this->_fetch($fetch_index, '*');
+ foreach (array_diff($this->index, $old_index) as $msguid) {
+ if ($object = $this->folder->read_object($msguid, '*')) {
+ $this->_extended_insert($msguid, $object);
+ }
+ }
+ $this->_extended_insert(0, null);
// delete invalid entries from local DB
$del_index = array_diff($old_index, $this->index);
@@ -499,6 +503,55 @@ class kolab_storage_cache
}
/**
+ * Write records into cache using extended inserts to reduce the number of queries to be executed
+ *
+ * @param int Message UID. Set 0 to commit buffered inserts
+ * @param array Kolab object to cache
+ */
+ private function _extended_insert($msguid, $object)
+ {
+ static $buffer = '';
+
+ $line = '';
+ if ($object) {
+ $sql_data = $this->_serialize($object);
+ $objtype = $object['_type'] ? $object['_type'] : $this->folder->type;
+
+ $values = array(
+ $this->db->quote($this->resource_uri),
+ $this->db->quote($objtype),
+ $this->db->quote($msguid),
+ $this->db->quote($object['uid']),
+ $this->db->quote($sql_data['data']),
+ $this->db->quote($sql_data['xml']),
+ $this->db->quote($sql_data['dtstart']),
+ $this->db->quote($sql_data['dtend']),
+ $this->db->quote($sql_data['tags']),
+ $this->db->quote($sql_data['words']),
+ );
+ $line = '(' . join(',', $values) . ')';
+ }
+
+ if ($buffer && (!$msguid || (strlen($buffer) + strlen($line) > $this->max_sql_packet))) {
+ $result = $this->db->query(
+ "INSERT INTO kolab_cache ".
+ " (resource, type, msguid, uid, data, xml, dtstart, dtend, tags, words)".
+ " VALUES $buffer"
+ );
+ if (!$this->db->affected_rows($result)) {
+ rcmail::raise_error(array(
+ 'code' => 900, 'type' => 'php',
+ 'message' => "Failed to write to kolab cache"
+ ), true);
+ }
+
+ $buffer = '';
+ }
+
+ $buffer .= ($buffer ? ',' : '') . $line;
+ }
+
+ /**
* Check lock record for this folder and wait if locked or set lock
*/
private function _sync_lock()
More information about the commits
mailing list