Use explicit insert() method to skip delete query when caching a newly created object (updated objects get new msguids and are de-facto new)

This commit is contained in:
Thomas Bruederli 2012-06-26 16:54:04 +02:00
parent 5fa593ad99
commit 290d0f113f
2 changed files with 49 additions and 32 deletions

View file

@ -202,50 +202,67 @@ class kolab_storage_cache
return; return;
} }
// write to cache // remove old entry
if ($this->ready) { if ($this->ready) {
// remove old entry
$this->db->query("DELETE FROM kolab_cache WHERE resource=? AND msguid=? AND type<>?", $this->db->query("DELETE FROM kolab_cache WHERE resource=? AND msguid=? AND type<>?",
$this->resource_uri, $msguid, 'lock'); $this->resource_uri, $msguid, 'lock');
}
// write new object data if not false (wich means deleted) if ($object) {
if ($object) { // insert new object data...
$sql_data = $this->_serialize($object); $this->insert($msguid, $object);
$objtype = $object['_type'] ? $object['_type'] : $this->folder->type; }
else {
// ...or set in-memory cache to false
$this->objects[$msguid] = $object;
}
}
$result = $this->db->query(
"INSERT INTO kolab_cache ".
" (resource, type, msguid, uid, created, changed, data, xml, dtstart, dtend, tags, words)".
" VALUES (?, ?, ?, ?, " . $this->db->now() . ", ?, ?, ?, ?, ?, ?, ?)",
$this->resource_uri,
$objtype,
$msguid,
$object['uid'],
$sql_data['changed'],
$sql_data['data'],
$sql_data['xml'],
$sql_data['dtstart'],
$sql_data['dtend'],
$sql_data['tags'],
$sql_data['words']
);
if (!$this->db->affected_rows($result)) { /**
rcube::raise_error(array( * Insert a cache entry
'code' => 900, 'type' => 'php', *
'message' => "Failed to write to kolab cache" * @param string Related IMAP message UID
), true); * @param mixed Hash array with object properties to save or false to delete the cache entry
} */
public function insert($msguid, $object)
{
// write to cache
if ($this->ready) {
$sql_data = $this->_serialize($object);
$objtype = $object['_type'] ? $object['_type'] : $this->folder->type;
$result = $this->db->query(
"INSERT INTO kolab_cache ".
" (resource, type, msguid, uid, created, changed, data, xml, dtstart, dtend, tags, words)".
" VALUES (?, ?, ?, ?, " . $this->db->now() . ", ?, ?, ?, ?, ?, ?, ?)",
$this->resource_uri,
$objtype,
$msguid,
$object['uid'],
$sql_data['changed'],
$sql_data['data'],
$sql_data['xml'],
$sql_data['dtstart'],
$sql_data['dtend'],
$sql_data['tags'],
$sql_data['words']
);
if (!$this->db->affected_rows($result)) {
rcube::raise_error(array(
'code' => 900, 'type' => 'php',
'message' => "Failed to write to kolab cache"
), true);
} }
} }
// keep a copy in memory for fast access // keep a copy in memory for fast access
$this->objects[$msguid] = $object; $this->objects[$msguid] = $object;
$this->uid2msg[$object['uid']] = $msguid;
if ($object)
$this->uid2msg[$object['uid']] = $msguid;
} }
/** /**
* Move an existing cache entry to a new resource * Move an existing cache entry to a new resource
* *

View file

@ -592,7 +592,7 @@ class kolab_storage_folder
// update cache with new UID // update cache with new UID
if ($result) { if ($result) {
$object['_msguid'] = $result; $object['_msguid'] = $result;
$this->cache->set($result, $object); $this->cache->insert($result, $object);
} }
} }