summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2009-06-28 16:24:51 -0700
committerBharat Mediratta <bharat@menalto.com>2009-06-28 16:24:51 -0700
commitaa31e1f0090522c3cfb3a44b947ad8c33a275979 (patch)
tree0438427a2864b69228a48ff6670671d53ee7cf17
parentf3f8b5eed6bbb4a1697db2ac5a7085439ed35a0d (diff)
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.
-rw-r--r--modules/gallery/controllers/javascript.php21
-rw-r--r--modules/gallery/helpers/gallery_installer.php32
-rw-r--r--modules/gallery/libraries/Theme_View.php16
3 files changed, 33 insertions, 36 deletions
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 <script> element to refer to it.
+ */
private function _combine_script() {
$links = array();
$key = "";
@@ -188,18 +192,18 @@ class Theme_View_Core extends View {
}
$key = md5($key);
- $contents = Cache::instance()->get($key);
+ $cache = Cache::instance();
+ $contents = $cache->get($key);
if (empty($contents)) {
- $contents = '';
+ $contents = "";
foreach ($links as $link) {
$contents .= file_get_contents($link);
}
- Cache::instance()->set($key, $contents, array("javascript"), 84600);
+ $cache->set($key, $contents, array("javascript"), 30 * 84600);
if (function_exists("gzencode")) {
- Cache::instance()->set("{$key}_gz", gzencode($contents, 9, FORCE_GZIP),
- array("javascript", "gzip"), 84600);
+ $cache->set("{$key}_gz", gzencode($contents, 9, FORCE_GZIP),
+ array("javascript", "gzip"), 30 * 84600);
}
- Cache::instance()->set("{$key}_modified", time(), array("javascript", "modified"), 84600);
}
// Handcraft the script link because html::script will add a .js extenstion