Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Using ON DUPLICATE KEY UPDATE instead of SELECT+UPDATE/INSERT style m…

…ethod (that does 2 trips to Database server and is less optimal).

exists() method is not needed anymore thus got removed
  • Loading branch information...
commit 66fd8c7518ab71466aca72d20fb7bcd5f374af26 1 parent fda9250
@Joe7 Joe7 authored bharat committed
Showing with 8 additions and 30 deletions.
  1. +8 −30 modules/gallery/libraries/drivers/Cache/Database.php
View
38 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 @@ public function set($items, $tags=null, $lifetime=null) {
$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;
Please sign in to comment.
Something went wrong with that request. Please try again.