From 2d38370ec4c7f7d68ed60e78a3a118ae20fcf540 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sat, 27 Jun 2009 23:34:07 -0700 Subject: The rest of the caching driver implementation that i somehow forgot. --- .../gallery/libraries/drivers/Cache/Database.php | 181 +++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 modules/gallery/libraries/drivers/Cache/Database.php (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 new file mode 100644 index 00000000..e008f473 --- /dev/null +++ b/modules/gallery/libraries/drivers/Cache/Database.php @@ -0,0 +1,181 @@ +db = Database::instance(); + + if (!$this->db->table_exists("caches")) { + throw new Kohana_Exception('cache.driver_error', "Cache table is not defined"); + } + Kohana::log('debug', 'Cache Database Driver Initialized'); + } + + /** + * 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('id' => $id, "expiration >=" => time())); + 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 cache tags + * @param integer lifetime + * @return bool + */ + public function set($id, $data, array $tags = NULL, $lifetime) { + if (!empty($tags)) { + // Escape the tags, adding brackets so the tag can be explicitly matched + $tags = '<' . implode('>,<', $tags) . '>'; + } + + // Cache Database driver expects unix timestamp + if ($lifetime !== 0) { + $lifetime += time(); + } + + $data = serialize($data); + if ($this->exists($id)) { + $status = $this->db->update("caches", + array("tags" => $tags, "expiration" => $lifetime, "cache" => $data), array("id" => $id)); + } else { + $status = $this->db->insert("caches", + array("id" => $id, "tags" => $tags, "expiration" => $lifetime, "cache" => $data)); + } + + return count($status) > 0; + } + + /** + * Finds an array of ids for a given tag. + * + * @param string tag name + * @return array of ids that match the tag + */ + public function find($tag) { + $db_result = $this->db->from("caches") + ->like("tags", "<$tag>") + ->get() + ->result(true); + + // An array will always be returned + $result = array(); + + 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->id] = unserialize($row->cache); + } + + // Turn notices back on + error_reporting($ER); + } + + return $result; + } + + /** + * Fetches a cache item. This will delete the item if it is expired or if + * the hash does not match the stored hash. + * + * @param string cache id + * @return mixed|NULL + */ + public function get($id) { + $data = null; + $result = $this->db->getwhere("caches", array('id' => $id)); + + if (count($result) > 0) { + $cache = $result->current(); + // Make sure the expiration is valid and that the hash matches + if ($cache->expiration != 0 && $cache->expiration <= time()) { + // Cache is not valid, delete it now + $this->delete($cache->id); + } else { + // Disable notices for unserializing + $ER = error_reporting(~E_NOTICE); + + // Return the valid cache data + $data = unserialize($cache->cache); + + // Turn notices back on + error_reporting($ER); + } + } + + return $data; + } + + /** + * Deletes a cache item by id or tag + * + * @param string cache id or tag, or true for "all items" + * @param bool delete a tag + * @return bool + */ + public function delete($id, $tag = false) { + $this->db->from("caches"); + if ($id === true) { + $this->db->where(1); + // Delete all caches + } else if ($tag === true) { + $this->db->like("tags", "<$id>"); + } else { + $this->db->where("id", $id); + } + + $status = $this->db->delete(); + + return count($status) > 0; + } + + /** + * Deletes all cache files that are older than the current time. + */ + public function delete_expired() { + // Delete all expired caches + $status = $this->db->from("caches") + ->where(array("expiration !=" => 0, "expiration <=" => time())) + ->delete(); + + return count($status) > 0; + } + +} // End Cache Database Driver \ No newline at end of file -- cgit v1.2.3 From a0c07d4b549f10dcd954777ae7d846a9b81246d8 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sun, 28 Jun 2009 07:49:35 -0700 Subject: Clean up code (i.e. preamble, tabs) from the caching implementation so the unit tests pass --- modules/gallery/config/cache.php | 31 +++++++++++++++++----- .../gallery/libraries/drivers/Cache/Database.php | 12 ++++----- modules/gallery/tests/Gallery_Installer_Test.php | 3 --- 3 files changed, 30 insertions(+), 16 deletions(-) (limited to 'modules/gallery/libraries/drivers/Cache/Database.php') diff --git a/modules/gallery/config/cache.php b/modules/gallery/config/cache.php index 5f2cd6de..cc3ac87d 100644 --- a/modules/gallery/config/cache.php +++ b/modules/gallery/config/cache.php @@ -1,5 +1,23 @@ - 'database', - 'params' => null, - 'lifetime' => 84600, - 'requests' => 1000 +$config["default"] = array ( + "driver" => "database", + "params" => null, + "lifetime" => 84600, + "requests" => 1000 ); diff --git a/modules/gallery/libraries/drivers/Cache/Database.php b/modules/gallery/libraries/drivers/Cache/Database.php index e008f473..70235e05 100644 --- a/modules/gallery/libraries/drivers/Cache/Database.php +++ b/modules/gallery/libraries/drivers/Cache/Database.php @@ -1,4 +1,4 @@ -db = Database::instance(); if (!$this->db->table_exists("caches")) { - throw new Kohana_Exception('cache.driver_error', "Cache table is not defined"); + throw new Kohana_Exception("cache.driver_error", "Cache table is not defined"); } - Kohana::log('debug', 'Cache Database Driver Initialized'); + Kohana::log("debug", "Cache Database Driver Initialized"); } /** @@ -44,7 +44,7 @@ class Cache_Database_Driver implements Cache_Driver { * @return boolean */ public function exists($id) { - $count = $this->db->count_records("caches", array('id' => $id, "expiration >=" => time())); + $count = $this->db->count_records("caches", array("id" => $id, "expiration >=" => time())); return $count > 0; } @@ -60,7 +60,7 @@ class Cache_Database_Driver implements Cache_Driver { public function set($id, $data, array $tags = NULL, $lifetime) { if (!empty($tags)) { // Escape the tags, adding brackets so the tag can be explicitly matched - $tags = '<' . implode('>,<', $tags) . '>'; + $tags = "<" . implode(">,<", $tags) . ">"; } // Cache Database driver expects unix timestamp @@ -120,7 +120,7 @@ class Cache_Database_Driver implements Cache_Driver { */ public function get($id) { $data = null; - $result = $this->db->getwhere("caches", array('id' => $id)); + $result = $this->db->getwhere("caches", array("id" => $id)); if (count($result) > 0) { $cache = $result->current(); diff --git a/modules/gallery/tests/Gallery_Installer_Test.php b/modules/gallery/tests/Gallery_Installer_Test.php index 001b7d26..27157d6e 100644 --- a/modules/gallery/tests/Gallery_Installer_Test.php +++ b/modules/gallery/tests/Gallery_Installer_Test.php @@ -31,9 +31,6 @@ class Gallery_Installer_Test extends Unit_Test_Case { public function install_registers_gallery_module_test() { $gallery = ORM::factory("module")->where("name", "gallery")->find(); $this->assert_equal("gallery", $gallery->name); - - // This is probably too volatile to keep for long - $this->assert_equal(2, $gallery->version); } public function install_creates_root_item_test() { -- cgit v1.2.3 From 7a3310e91b50f37b09a1c9d10173409244015653 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sun, 28 Jun 2009 13:14:47 -0700 Subject: Change the cache column of the caches table to a large blob. This fixes ticket #485 and gives us the extra adavantage of not having to serialize the data (as the database driver handles that for us) --- modules/gallery/helpers/gallery_installer.php | 9 +++++++-- modules/gallery/libraries/drivers/Cache/Database.php | 5 ++--- modules/gallery/module.info | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) (limited to 'modules/gallery/libraries/drivers/Cache/Database.php') diff --git a/modules/gallery/helpers/gallery_installer.php b/modules/gallery/helpers/gallery_installer.php index 92fc662d..8ccbd51b 100644 --- a/modules/gallery/helpers/gallery_installer.php +++ b/modules/gallery/helpers/gallery_installer.php @@ -185,7 +185,7 @@ class gallery_installer { `id` varchar(255) NOT NULL, `tags` varchar(255), `expiration` int(9) NOT NULL, - `cache` text, + `cache` longblob, PRIMARY KEY (`id`), KEY (`tags`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); @@ -258,7 +258,7 @@ class gallery_installer { module::set_var("gallery", "show_credits", 1); // @todo this string needs to be picked up by l10n_scanner module::set_var("gallery", "credits", "Powered by Gallery %version"); - module::set_version("gallery", 4); + module::set_version("gallery", 5); } static function upgrade($version) { @@ -287,6 +287,11 @@ class gallery_installer { ENGINE=InnoDB DEFAULT CHARSET=utf8;"); module::set_version("gallery", $version = 4); } + if ($version == 4) { + Cache::instance()->delete_all(); + $db->query("ALTER TABLE {caches} modify column cache LONGBLOB"); + module::set_version("gallery", $version = 5); + } } static function uninstall() { diff --git a/modules/gallery/libraries/drivers/Cache/Database.php b/modules/gallery/libraries/drivers/Cache/Database.php index 70235e05..158f7b3a 100644 --- a/modules/gallery/libraries/drivers/Cache/Database.php +++ b/modules/gallery/libraries/drivers/Cache/Database.php @@ -68,7 +68,6 @@ class Cache_Database_Driver implements Cache_Driver { $lifetime += time(); } - $data = serialize($data); if ($this->exists($id)) { $status = $this->db->update("caches", array("tags" => $tags, "expiration" => $lifetime, "cache" => $data), array("id" => $id)); @@ -101,7 +100,7 @@ class Cache_Database_Driver implements Cache_Driver { foreach ($db_result as $row) { // Add each cache to the array - $result[$row->id] = unserialize($row->cache); + $result[$row->id] = $row->cache; } // Turn notices back on @@ -133,7 +132,7 @@ class Cache_Database_Driver implements Cache_Driver { $ER = error_reporting(~E_NOTICE); // Return the valid cache data - $data = unserialize($cache->cache); + $data = $cache->cache; // Turn notices back on error_reporting($ER); diff --git a/modules/gallery/module.info b/modules/gallery/module.info index 1a44ce51..817fdddd 100644 --- a/modules/gallery/module.info +++ b/modules/gallery/module.info @@ -1,3 +1,3 @@ name = Gallery 3 description = Gallery core application -version = 4 +version = 5 -- cgit v1.2.3 From fcc57a418245b6cac410f70506f6f4b687c8a98a Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 28 Jun 2009 16:48:29 -0700 Subject: Modify the cache table to make id the primary key for consistency with other gallery 3 tables. Update the driver to match, add more upgrader code, update the installer block and change the gallery module version to 6. --- modules/gallery/helpers/gallery_installer.php | 14 ++++++++++++-- modules/gallery/libraries/drivers/Cache/Database.php | 13 ++++++------- modules/gallery/module.info | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) (limited to 'modules/gallery/libraries/drivers/Cache/Database.php') diff --git a/modules/gallery/helpers/gallery_installer.php b/modules/gallery/helpers/gallery_installer.php index d0bfa629..c3c3543c 100644 --- a/modules/gallery/helpers/gallery_installer.php +++ b/modules/gallery/helpers/gallery_installer.php @@ -33,7 +33,8 @@ class gallery_installer { ENGINE=InnoDB DEFAULT CHARSET=utf8;"); $db->query("CREATE TABLE {caches} ( - `id` varchar(255) NOT NULL, + `id` int(9) NOT NULL auto_increment, + `key` varchar(255) NOT NULL, `tags` varchar(255), `expiration` int(9) NOT NULL, `cache` longblob, @@ -287,11 +288,20 @@ class gallery_installer { ENGINE=InnoDB DEFAULT CHARSET=utf8;"); module::set_version("gallery", $version = 4); } + if ($version == 4) { Cache::instance()->delete_all(); - $db->query("ALTER TABLE {caches} modify column cache LONGBLOB"); + $db->query("ALTER TABLE {caches} MODIFY COLUMN `cache` LONGBLOB"); module::set_version("gallery", $version = 5); } + + if ($version == 5) { + Cache::instance()->delete_all(); + $db->query("ALTER TABLE {caches} DROP COLUMN `id`"); + $db->query("ALTER TABLE {caches} ADD COLUMN `key` varchar(255) NOT NULL"); + $db->query("ALTER TABLE {caches} ADD COLUMN `id` int(9) NOT NULL auto_increment PRIMARY KEY"); + module::set_version("gallery", $version = 6); + } } static function uninstall() { diff --git a/modules/gallery/libraries/drivers/Cache/Database.php b/modules/gallery/libraries/drivers/Cache/Database.php index 158f7b3a..f3a1eb02 100644 --- a/modules/gallery/libraries/drivers/Cache/Database.php +++ b/modules/gallery/libraries/drivers/Cache/Database.php @@ -32,9 +32,8 @@ class Cache_Database_Driver implements Cache_Driver { $this->db = Database::instance(); if (!$this->db->table_exists("caches")) { - throw new Kohana_Exception("cache.driver_error", "Cache table is not defined"); + throw new Exception("@todo Cache table is not defined"); } - Kohana::log("debug", "Cache Database Driver Initialized"); } /** @@ -44,7 +43,7 @@ class Cache_Database_Driver implements Cache_Driver { * @return boolean */ public function exists($id) { - $count = $this->db->count_records("caches", array("id" => $id, "expiration >=" => time())); + $count = $this->db->count_records("caches", array("key" => $id, "expiration >=" => time())); return $count > 0; } @@ -70,10 +69,10 @@ class Cache_Database_Driver implements Cache_Driver { if ($this->exists($id)) { $status = $this->db->update("caches", - array("tags" => $tags, "expiration" => $lifetime, "cache" => $data), array("id" => $id)); + array("tags" => $tags, "expiration" => $lifetime, "cache" => $data), array("key" => $id)); } else { $status = $this->db->insert("caches", - array("id" => $id, "tags" => $tags, "expiration" => $lifetime, "cache" => $data)); + array("key" => $id, "tags" => $tags, "expiration" => $lifetime, "cache" => $data)); } return count($status) > 0; @@ -119,7 +118,7 @@ class Cache_Database_Driver implements Cache_Driver { */ public function get($id) { $data = null; - $result = $this->db->getwhere("caches", array("id" => $id)); + $result = $this->db->getwhere("caches", array("key" => $id)); if (count($result) > 0) { $cache = $result->current(); @@ -157,7 +156,7 @@ class Cache_Database_Driver implements Cache_Driver { } else if ($tag === true) { $this->db->like("tags", "<$id>"); } else { - $this->db->where("id", $id); + $this->db->where("key", $id); } $status = $this->db->delete(); diff --git a/modules/gallery/module.info b/modules/gallery/module.info index 817fdddd..c184aba7 100644 --- a/modules/gallery/module.info +++ b/modules/gallery/module.info @@ -1,3 +1,3 @@ name = Gallery 3 description = Gallery core application -version = 5 +version = 6 -- cgit v1.2.3