From 66fd8c7518ab71466aca72d20fb7bcd5f374af26 Mon Sep 17 00:00:00 2001 From: Joe7 Date: Mon, 27 Dec 2010 15:35:33 +0100 Subject: 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 --- .../gallery/libraries/drivers/Cache/Database.php | 38 +++++----------------- 1 file changed, 8 insertions(+), 30 deletions(-) (limited to 'modules/gallery/libraries/drivers/Cache/Database.php') 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 @@ -24,21 +24,6 @@ class Cache_Database_Driver extends Cache_Driver { // Kohana database instance 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. * @@ -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; -- cgit v1.2.3