summaryrefslogtreecommitdiff
path: root/modules/gallery/libraries/drivers/Cache
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gallery/libraries/drivers/Cache')
-rw-r--r--modules/gallery/libraries/drivers/Cache/Database.php95
1 files changed, 51 insertions, 44 deletions
diff --git a/modules/gallery/libraries/drivers/Cache/Database.php b/modules/gallery/libraries/drivers/Cache/Database.php
index 7e2aeabc..d27bcc32 100644
--- a/modules/gallery/libraries/drivers/Cache/Database.php
+++ b/modules/gallery/libraries/drivers/Cache/Database.php
@@ -20,43 +20,33 @@
/*
* Based on the Cache_Sqlite_Driver developed by the Kohana Team
*/
-class Cache_Database_Driver implements Cache_Driver {
+class Cache_Database_Driver extends Cache_Driver {
// Kohana database instance
protected $db;
/**
- * Tests that the storage location is a directory and is writable.
- */
- public function __construct() {
- // Open up an instance of the database
- $this->db = Database::instance();
-
- if (!$this->db->table_exists("caches")) {
- throw new Exception("@todo Cache table is not defined");
- }
- }
-
- /**
* Checks if a cache id is already set.
*
* @param string cache id
* @return boolean
*/
public function exists($id) {
- $count = $this->db->count_records("caches", array("key" => $id, "expiration >=" => time()));
+ $count = db::build()
+ ->where("key", "=", $id)
+ ->where("expiration", ">=", "time()")
+ ->count_records("caches");
return $count > 0;
}
/**
* Sets a cache item to the given data, tags, and lifetime.
*
- * @param string cache id to set
- * @param string data in the cache
+ * @param array assoc array of key => value pairs
* @param array cache tags
* @param integer lifetime
* @return bool
*/
- public function set($id, $data, array $tags = NULL, $lifetime) {
+ public function set($items, $tags=null, $lifetime=null) {
if (!empty($tags)) {
// Escape the tags, adding brackets so the tag can be explicitly matched
$tags = "<" . implode(">,<", $tags) . ">";
@@ -69,46 +59,46 @@ class Cache_Database_Driver implements Cache_Driver {
$lifetime += time();
}
- if ($this->exists($id)) {
- $status = $this->db->update(
- "caches",
- array("tags" => $tags, "expiration" => $lifetime, "cache" => serialize($data)), array("key" => $id));
- } else {
- $status = $this->db->insert(
- "caches",
- array("key" => $id, "tags" => $tags, "expiration" => $lifetime, "cache" => serialize($data)));
+ foreach ($items as $id => $data) {
+ if ($this->exists($id)) {
+ $status = db::build()->update(
+ "caches",
+ array("tags" => $tags, "expiration" => $lifetime, "cache" => serialize($data)),
+ array("key", "=", $id));
+ } else {
+ $status = db::build()->insert(
+ "caches",
+ array("key" => $id, "tags" => $tags, "expiration" => $lifetime, "cache" => serialize($data)));
+ }
}
- return count($status) > 0;
+ return true;
}
/**
- * Finds an array of ids for a given tag.
- *
- * @param string tag name
- * @return array of ids that match the tag
+ * Get cache items by tag
+ * @param array cache tags
+ * @return array cached data
*/
- public function find($tag) {
- $db_result = $this->db->from("caches")
- ->like("tags", "<$tag>")
- ->get()
- ->result(true);
+ public function get_tag($tags) {
+ $db = db::build()->from("caches");
+ foreach ($tags as $tag) {
+ $db->where("tags", "like", "<$tag>");
+ }
+ $db_result = $db->execute()->as_array();
// An array will always be returned
$result = array();
+ // Disable notices for unserializing
+ $ER = error_reporting(~E_NOTICE);
if ($db_result->count() > 0) {
- // Disable notices for unserializing
- $ER = error_reporting(~E_NOTICE);
-
foreach ($db_result as $row) {
// Add each cache to the array
$result[$row->key] = unserialize($row->cache);
}
-
- // Turn notices back on
- error_reporting($ER);
}
+ error_reporting($ER);
return $result;
}
@@ -120,9 +110,13 @@ class Cache_Database_Driver implements Cache_Driver {
* @param string cache id
* @return mixed|NULL
*/
- public function get($id) {
+ public function get($keys, $single=false) {
$data = null;
- $result = $this->db->getwhere("caches", array("key" => $id));
+ $result = db::build()
+ ->from("caches")
+ ->where("key", "IN", $keys)
+ ->select()
+ ->execute();
if (count($result) > 0) {
$cache = $result->current();
@@ -169,6 +163,13 @@ class Cache_Database_Driver implements Cache_Driver {
}
/**
+ * Delete cache items by tag
+ */
+ public function delete_tag($tags) {
+ return $this->delete($tags, true);
+ }
+
+ /**
* Deletes all cache files that are older than the current time.
*/
public function delete_expired() {
@@ -180,4 +181,10 @@ class Cache_Database_Driver implements Cache_Driver {
return count($status) > 0;
}
-} // End Cache Database Driver \ No newline at end of file
+ /**
+ * Empty the cache
+ */
+ public function delete_all() {
+ db::build()->query("TRUNCATE {caches}");
+ }
+} \ No newline at end of file