summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe7 <jozsef.rnagy@site.hu>2010-12-27 15:35:33 +0100
committerJoe7 <jozsef.rnagy@site.hu>2010-12-27 15:35:33 +0100
commitf4ecb939f5f61f16715df2ed5a88f01d85926eb6 (patch)
tree9fe9c58c3666ebba14ea9dfa19c6c3d558401154
parenta4100521d7a60a31e2f0c22d12fc0e371c201f8c (diff)
Using ON DUPLICATE KEY UPDATE instead of SELECT+UPDATE/INSERT style method (that does 2 trips to Database server and is less optimal).
exists() method is not needed anymore thus got removed
-rw-r--r--modules/gallery/libraries/drivers/Cache/Database.php38
1 files changed, 8 insertions, 30 deletions
diff --git a/modules/gallery/libraries/drivers/Cache/Database.php b/modules/gallery/libraries/drivers/Cache/Database.php
index 4f57b3da..7eda5b30 100644
--- a/modules/gallery/libraries/drivers/Cache/Database.php
+++ b/modules/gallery/libraries/drivers/Cache/Database.php
@@ -25,21 +25,6 @@ class Cache_Database_Driver extends Cache_Driver {
protected $db;
/**
- * Checks if a cache id is already set.
- *
- * @param string cache id
- * @return boolean
- */
- public function exists($id) {
- $count = db::build()
- ->where("key", "=", $id)
- ->where("expiration", ">=", time())
- ->limit("1")
- ->count_records("caches");
- return $count > 0;
- }
-
- /**
* Sets a cache item to the given data, tags, and lifetime.
*
* @param array assoc array of key => value pairs
@@ -60,22 +45,15 @@ class Cache_Database_Driver extends Cache_Driver {
$lifetime += time();
}
+ $db = Database::instance();
+ $tags = $db->escape($tags);
foreach ($items as $id => $data) {
- if ($this->exists($id)) {
- $status = db::build()
- ->update("caches")
- ->set("tags", $tags)
- ->set("expiration", $lifetime)
- ->set("cache", serialize($data))
- ->where("key", "=", $id)
- ->execute();
- } else {
- $status = db::build()
- ->insert("caches")
- ->columns("key", "tags", "expiration", "cache")
- ->values($id, $tags, $lifetime, serialize($data))
- ->execute();
- }
+ $id = $db->escape($id);
+ $data = $db->escape(serialize($data));
+ $db->query("INSERT INTO {caches} (`key`, `tags`, `expiration`, `cache`)
+ VALUES ('$id', '$tags', $lifetime, '$data')
+ ON DUPLICATE KEY UPDATE
+ `tags`=VALUES(tags), `expiration`=VALUES(expiration), `cache`=VALUES(cache)");
}
return true;