From 42a5bd20a550a9edb5ec84625037b9d85889ff63 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Fri, 26 Jun 2009 07:51:29 -0700 Subject: 1) Move the generation of script tags to gallery_theme::head and gallery_theme::admin_head. This allows us to potentially manage the scripts like we do in g2 (single file and compressed) 2) Change Theme_View::_call to always call the gallery_theme::$function first. --- modules/gallery/libraries/Theme_View.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index 7b2ca840..5235fabb 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -68,9 +68,9 @@ class Theme_View_Core extends View { return module::get_var("gallery", "thumb_size", 200) / 200; } - public function url($path, $absolute_url=false) { + public function url($path, $absolute_url=false, $no_root=false) { $arg = "themes/{$this->theme_name}/$path"; - return $absolute_url ? url::abs_file($arg) : url::file($arg); + return $absolute_url ? url::abs_file($arg) : $no_root ? $arg : url::file($arg); } public function item() { @@ -192,7 +192,28 @@ class Theme_View_Core extends View { case "thumb_info": case "thumb_top": $blocks = array(); + if (method_exists("gallery_theme", $function)) { + switch (count($args)) { + case 0: + $blocks[] = gallery_theme::$function($this); + break; + case 1: + $blocks[] = gallery_theme::$function($this, $args[0]); + break; + case 2: + $blocks[] = gallery_theme::$function($this, $args[0], $args[1]); + break; + default: + $blocks[] = call_user_func_array( + array("gallery_theme", $function), + array_merge(array($this), $args)); + } + + } foreach (module::active() as $module) { + if ($module->name == "gallery") { + continue; + } $helper_class = "{$module->name}_theme"; if (method_exists($helper_class, $function)) { $blocks[] = call_user_func_array( -- cgit v1.2.3 From 4cec0201637d21f8e88c264bb1befff4e891ce3e Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Fri, 26 Jun 2009 14:37:15 -0700 Subject: Create a theme_view function script which allows modules in the head or admin_head functions to specify javascript files that are required for this page. In this commit, these script files are expressed at the end of the head or admin_head calls and appended to the beginning of the block stack. In a future commit these will be combined and gzipped for download. --- modules/comment/helpers/comment_theme.php | 4 +-- modules/gallery/helpers/gallery_theme.php | 50 +++++++++++++++---------------- modules/gallery/libraries/Theme_View.php | 19 ++++++++++++ 3 files changed, 46 insertions(+), 27 deletions(-) (limited to 'modules/gallery/libraries') diff --git a/modules/comment/helpers/comment_theme.php b/modules/comment/helpers/comment_theme.php index d9f1acf4..89b2f57c 100644 --- a/modules/comment/helpers/comment_theme.php +++ b/modules/comment/helpers/comment_theme.php @@ -19,8 +19,8 @@ */ class comment_theme_Core { static function head($theme) { - $url = url::file("modules/comment/js/comment.js"); - return "\n"; + $theme->script("modules/comment/js/comment.js"); + return ""; } static function photo_bottom($theme) { diff --git a/modules/gallery/helpers/gallery_theme.php b/modules/gallery/helpers/gallery_theme.php index 0679087e..b6b24b27 100644 --- a/modules/gallery/helpers/gallery_theme.php +++ b/modules/gallery/helpers/gallery_theme.php @@ -21,22 +21,22 @@ class gallery_theme_Core { static function head($theme) { $session = Session::instance(); $buf = ""; - $buf .= html::script("lib/jquery.js"); - $buf .= html::script("lib/jquery.form.js"); - $buf .= html::script("lib/jquery-ui.js"); - $buf .= html::script("lib/gallery.common.js"); - $buf .= html::script("lib/gallery.dialog.js"); - $buf .= html::script("lib/gallery.form.js"); - $buf .= html::script("lib/superfish/js/superfish.js"); + $theme->script("lib/jquery.js"); + $theme->script("lib/jquery.form.js"); + $theme->script("lib/jquery-ui.js"); + $theme->script("lib/gallery.common.js"); + $theme->script("lib/gallery.dialog.js"); + $theme->script("lib/gallery.form.js"); + $theme->script("lib/superfish/js/superfish.js"); if ($theme->page_type == 'photo') { - $buf .= html::script("lib/jquery.scrollTo.js"); - $buf .= html::script("lib/jquery.localscroll.js"); - $buf .= html::script("lib/gallery.show_full_size.js"); + $theme->script("lib/jquery.scrollTo.js"); + $theme->script("lib/jquery.localscroll.js"); + $theme->script("lib/gallery.show_full_size.js"); } if ($theme->page_type == 'movie') { - $buf .= html::script("lib/flowplayer.js"); + $theme->script("lib/flowplayer.js"); } - $buf .= html::script($theme->url("js/ui.init.js", false, true)); + $theme->script($theme->url("js/ui.init.js", false, true)); if ($session->get("debug")) { $buf .= ""; @@ -45,7 +45,7 @@ class gallery_theme_Core { && access::can("edit", $theme->item())) { $buf .= ""; - $buf .= html::script("modules/gallery/js/quick.js"); + $theme->script("modules/gallery/js/quick.js"); } if (module::is_active("rss")) { @@ -59,8 +59,8 @@ class gallery_theme_Core { if ($session->get("l10n_mode", false)) { $buf .= ""; - $buf .= html::script("lib/jquery.cookie.js"); - $buf .= html::script("modules/gallery/js/l10n_client.js"); + $theme->script("lib/jquery.cookie.js"); + $theme->script("modules/gallery/js/l10n_client.js"); } return $buf; @@ -95,14 +95,14 @@ class gallery_theme_Core { static function admin_head($theme) { $session = Session::instance(); $buf = ""; - $buf .= html::script("lib/jquery.js"); - $buf .= html::script("lib/jquery.form.js"); - $buf .= html::script("lib/jquery-ui.js"); - $buf .= html::script("lib/gallery.common.js"); - $buf .= html::script("lib/gallery.dialog.js"); - $buf .= html::script("lib/superfish/js/superfish.js"); - $buf .= html::script($theme->url("js/jquery.dropshadow.js", false, true)); - $buf .= html::script($theme->url("js/ui.init.js", false, true)); + $theme->script("lib/jquery.js"); + $theme->script("lib/jquery.form.js"); + $theme->script("lib/jquery-ui.js"); + $theme->script("lib/gallery.common.js"); + $theme->script("lib/gallery.dialog.js"); + $theme->script("lib/superfish/js/superfish.js"); + $theme->script($theme->url("js/jquery.dropshadow.js", false, true)); + $theme->script($theme->url("js/ui.init.js", false, true)); if ($session->get("debug")) { $buf .= ""; @@ -111,8 +111,8 @@ class gallery_theme_Core { if ($session->get("l10n_mode", false)) { $buf .= ""; - $buf .= html::script("lib/jquery.cookie.js"); - $buf .= html::script("modules/gallery/js/l10n_client.js"); + $theme->script("lib/jquery.cookie.js"); + $theme->script("modules/gallery/js/l10n_client.js"); } return $buf; diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index 5235fabb..7696f3b0 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -19,6 +19,7 @@ */ class Theme_View_Core extends View { private $theme_name = null; + private $scripts = array(); /** * Attempts to load a view and pre-load view data. @@ -163,6 +164,19 @@ class Theme_View_Core extends View { return message::get(); } + public function script($file) { + $this->scripts[$file] = 1; + } + + private function _combine_script() { + $links = array(); + Kohana::log("error", Kohana::debug($this->scripts)); + foreach (array_keys($this->scripts) as $file) { + $links[] = html::script($file); + } + return empty($links) ? "" : implode("\n", $links); + } + /** * Handle all theme functions that insert module content. */ @@ -221,6 +235,11 @@ class Theme_View_Core extends View { array_merge(array($this), $args)); } } + + if ($function == "head" || $function == "admin_head") { + array_unshift($blocks, $this->_combine_script()); + } + if (Session::instance()->get("debug")) { if ($function != "head") { array_unshift( -- cgit v1.2.3 From 11f08ee4399c62cc1d2c36457a214c6db693db06 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Fri, 26 Jun 2009 21:42:02 -0700 Subject: Implement the combined javascript controller. --- modules/gallery/controllers/javascript.php | 64 ++++++++++++++++++++++++++++++ modules/gallery/libraries/Theme_View.php | 29 ++++++++++++-- 2 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 modules/gallery/controllers/javascript.php (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/controllers/javascript.php b/modules/gallery/controllers/javascript.php new file mode 100644 index 00000000..d3c0ded5 --- /dev/null +++ b/modules/gallery/controllers/javascript.php @@ -0,0 +1,64 @@ +scripts)); + $key = ""; foreach (array_keys($this->scripts) as $file) { - $links[] = html::script($file); + $path = DOCROOT . $file; + if (file_exists($path)) { + $stats = stat($path); + $links[] = $path; + // 7 == size, 9 == mtime, see http://php.net/stat + $key = "{$key}$file $stats[7] $stats[9],"; + } else { + Kohana::log("warn", "Javascript file missing: " . $file); + } + } + + $key = md5($key); + $file = "tmp/CombinedJavascript_$key"; + if (!file_exists(VARPATH . $file)) { + $contents = ''; + foreach ($links as $link) { + $contents .= file_get_contents($link); + } + file_put_contents(VARPATH . $file, $contents); + if (function_exists("gzencode")) { + file_put_contents(VARPATH . "{$file}_gzip", gzencode($contents, 9, FORCE_GZIP)); + } } - return empty($links) ? "" : implode("\n", $links); + + return ""; } /** -- cgit v1.2.3 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. --- modules/gallery/config/cache.php | 32 ++++ .../gallery/libraries/drivers/Cache/Database.php | 181 +++++++++++++++++++++ 2 files changed, 213 insertions(+) create mode 100644 modules/gallery/config/cache.php create mode 100644 modules/gallery/libraries/drivers/Cache/Database.php (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/config/cache.php b/modules/gallery/config/cache.php new file mode 100644 index 00000000..5f2cd6de --- /dev/null +++ b/modules/gallery/config/cache.php @@ -0,0 +1,32 @@ + File cache is fast and reliable, but requires many filesystem lookups. + * > Database cache can be used to cache items remotely, but is slower. + * > Memcache is very high performance, but prevents cache tags from being used. + * + * params - Driver parameters, specific to each driver. + * + * lifetime - Default lifetime of caches in seconds. By default caches are stored for + * thirty minutes. Specific lifetime can also be set when creating a new cache. + * Setting this to 0 will never automatically delete caches. + * + * requests - Average number of cache requests that will processed before all expired + * caches are deleted. This is commonly referred to as "garbage collection". + * Setting this to 0 or a negative number will disable automatic garbage collection. + */ +$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 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') 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') 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 bf26ca727e615e61a8f61a2bbf1a83572cfa5c20 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sun, 28 Jun 2009 15:30:13 -0700 Subject: Change the combined javascript to use the new caching functionality and respect the HTTP_IF_MODIFIED_SINCE header request. --- modules/gallery/controllers/javascript.php | 37 +++++++++++++++++------------- modules/gallery/libraries/Theme_View.php | 11 +++++---- 2 files changed, 28 insertions(+), 20 deletions(-) (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/controllers/javascript.php b/modules/gallery/controllers/javascript.php index d3c0ded5..bc231e0a 100644 --- a/modules/gallery/controllers/javascript.php +++ b/modules/gallery/controllers/javascript.php @@ -20,30 +20,35 @@ class Javascript_Controller extends Controller { public function combined($key) { if (preg_match('/[^0-9a-f]/', $key)) { - /* The key can't contain non-hex, so just terminate early */ + // The key can't contain non-hex, so just terminate early Kohana::show_404(); } // We don't need to save the session for this request Session::abort_save(); - Kohana::log("error", Kohana::debug($_SERVER)); // Dump out the javascript file - $ext = strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false ? "_gzip" : ""; - $file = VARPATH . "tmp/CombinedJavascript_$key{$ext}"; + $cache = Cache::instance(); - if (!file_exists($file)) { - Kohana::show_404(); + $modified = $cache->get("{$key}_modified"); + if (!empty($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && !empty($modified)) { + header('HTTP/1.0 304 Not Modified'); + return; } - $stats = stat($file); - if (!empty($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && - $stats[9] <= $_SERVER["HTTP_IF_MODIFIED_SINCE"]) { - header("HTTP/1.0 304 Not Modified"); - return; + $content = ""; + if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false ) { + $content = $cache->get("{$key}_gz"); + } + + if (empty($content)) { + $content = $cache->get($key); + } + + if (empty($content)) { + Kohana::show_404(); } - Kohana::log("error", Kohana::debug($_SERVER)); if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false) { header("Content-Encoding: gzip"); header("Cache-Control: private, x-gzip-ok=\"public\""); @@ -52,13 +57,13 @@ class Javascript_Controller extends Controller { header("Content-Type: text/javascript; charset=UTF-8"); header("Expires: " . gmdate(21474383647)); - header("Last-Modified: " . gmdate($stats[9])); + header("Last-Modified: " . $modified); Kohana::close_buffers(false); - $fd = fopen($file, "rb"); - fpassthru($fd); - fclose($fd); + $handle = fopen("php://output", "wb"); + fwrite($handle, $content); + fclose($handle); } } diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index 167f8a8d..f55cb55a 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -188,18 +188,21 @@ class Theme_View_Core extends View { } $key = md5($key); - $file = "tmp/CombinedJavascript_$key"; - if (!file_exists(VARPATH . $file)) { + $contents = Cache::instance()->get($key); + if (empty($contents)) { $contents = ''; foreach ($links as $link) { $contents .= file_get_contents($link); } - file_put_contents(VARPATH . $file, $contents); + Cache::instance()->set($key, $contents, array("javascript"), 84600); if (function_exists("gzencode")) { - file_put_contents(VARPATH . "{$file}_gzip", gzencode($contents, 9, FORCE_GZIP)); + Cache::instance()->set("{$key}_gz", gzencode($contents, 9, FORCE_GZIP), + array("javascript", "gzip"), 84600); } + Cache::instance()->set("{$key}_modified", time(), array("javascript", "modified"), 84600); } + // Handcraft the script link because html::script will add a .js extenstion return ""; } -- cgit v1.2.3 From aa31e1f0090522c3cfb3a44b947ad8c33a275979 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 28 Jun 2009 16:24:51 -0700 Subject: Tweak the cache implementation 1) Drop the *_modified key, we don't really need it. The modification date is not relevant to our browser caching strategy. 2) Fix multiple issues with the Expires header and just hardcode it to the biggest possibly value for code clarity. 3) print the $content out directly instead of using fwrite 4) Minor cleanups in the installer. --- modules/gallery/controllers/javascript.php | 21 ++++++------------ modules/gallery/helpers/gallery_installer.php | 32 +++++++++++++-------------- modules/gallery/libraries/Theme_View.php | 16 +++++++++----- 3 files changed, 33 insertions(+), 36 deletions(-) (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/controllers/javascript.php b/modules/gallery/controllers/javascript.php index bc231e0a..ba5cbf4b 100644 --- a/modules/gallery/controllers/javascript.php +++ b/modules/gallery/controllers/javascript.php @@ -27,16 +27,13 @@ class Javascript_Controller extends Controller { // We don't need to save the session for this request Session::abort_save(); - // Dump out the javascript file - $cache = Cache::instance(); - - $modified = $cache->get("{$key}_modified"); - if (!empty($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && !empty($modified)) { + // Our data is immutable, so if they already have a copy then it needs no updating. + if (!empty($_SERVER["HTTP_IF_MODIFIED_SINCE"])) { header('HTTP/1.0 304 Not Modified'); return; } - $content = ""; + $cache = Cache::instance(); if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false ) { $content = $cache->get("{$key}_gz"); } @@ -51,19 +48,15 @@ class Javascript_Controller extends Controller { if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false) { header("Content-Encoding: gzip"); - header("Cache-Control: private, x-gzip-ok=\"public\""); + header("Cache-Control: public"); } header("Content-Type: text/javascript; charset=UTF-8"); - - header("Expires: " . gmdate(21474383647)); - header("Last-Modified: " . $modified); + header("Expires: Tue, 19 Jan 2038 00:00:00 GMT"); + header("Last-Modified: " . gmdate("D, d M Y H:i:s T", time())); Kohana::close_buffers(false); - - $handle = fopen("php://output", "wb"); - fwrite($handle, $content); - fclose($handle); + print $content; } } diff --git a/modules/gallery/helpers/gallery_installer.php b/modules/gallery/helpers/gallery_installer.php index 8ccbd51b..d0bfa629 100644 --- a/modules/gallery/helpers/gallery_installer.php +++ b/modules/gallery/helpers/gallery_installer.php @@ -32,6 +32,15 @@ class gallery_installer { PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + $db->query("CREATE TABLE {caches} ( + `id` varchar(255) NOT NULL, + `tags` varchar(255), + `expiration` int(9) NOT NULL, + `cache` longblob, + PRIMARY KEY (`id`), + KEY (`tags`)) + ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + $db->query("CREATE TABLE {graphics_rules} ( `id` int(9) NOT NULL auto_increment, `active` BOOLEAN default 0, @@ -181,15 +190,6 @@ class gallery_installer { UNIQUE KEY(`module_name`, `name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); - $db->query("CREATE TABLE {caches} ( - `id` varchar(255) NOT NULL, - `tags` varchar(255), - `expiration` int(9) NOT NULL, - `cache` longblob, - PRIMARY KEY (`id`), - KEY (`tags`)) - ENGINE=InnoDB DEFAULT CHARSET=utf8;"); - foreach (array("albums", "logs", "modules", "resizes", "thumbs", "tmp", "uploads") as $dir) { @mkdir(VARPATH . $dir); } @@ -278,13 +278,13 @@ class gallery_installer { if ($version == 3) { $db->query("CREATE TABLE {caches} ( - `id` varchar(255) NOT NULL, - `tags` varchar(255), - `expiration` int(9) NOT NULL, - `cache` text, - PRIMARY KEY (`id`), - KEY (`tags`)) - ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + `id` varchar(255) NOT NULL, + `tags` varchar(255), + `expiration` int(9) NOT NULL, + `cache` text, + PRIMARY KEY (`id`), + KEY (`tags`)) + ENGINE=InnoDB DEFAULT CHARSET=utf8;"); module::set_version("gallery", $version = 4); } if ($version == 4) { diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index f55cb55a..c3acfeb3 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -172,6 +172,10 @@ class Theme_View_Core extends View { $this->scripts[$file] = 1; } + /** + * Combine a series of Javascript files into a single one and cache it in the database, then + * return a single