Permalink
Browse files

perf(simplecache): Allow 304 responses even with simplecache off

  • Loading branch information...
mrclay committed Jun 30, 2015
1 parent 23ecfa4 commit 14bd68fbd7110dea82d0935ef284cc7180bc6d25
Showing with 35 additions and 7 deletions.
  1. +35 −7 engine/classes/Elgg/Application/CacheHandler.php
@@ -94,18 +94,18 @@ public function handleRequest($path) {
if (!\_elgg_is_view_cacheable($view)) {
$this->send403("Requested view is not an asset");
} else {
- echo $this->renderView($view, $viewtype);
+ $content = $this->renderView($view, $viewtype);
+ $etag = '"' . md5($content) . '"';
+ $this->sendRevalidateHeaders($etag);
+ $this->handle304($etag);
+
+ echo $content;
}
exit;
}
$etag = "\"$ts\"";
- // If is the same ETag, content didn't change.
- if (isset($this->server_vars['HTTP_IF_NONE_MATCH'])
- && trim($this->server_vars['HTTP_IF_NONE_MATCH']) === $etag) {
- header("HTTP/1.1 304 Not Modified");
- exit;
- }
+ $this->handle304($etag);
$this->application->bootCore();
@@ -230,6 +230,34 @@ protected function sendCacheHeaders($etag) {
header("ETag: $etag");
}
+ /**
+ * Send revalidate cache headers
+ *
+ * @param string $etag ETag value
+ * @return void
+ */
+ protected function sendRevalidateHeaders($etag) {
+ header_remove('Expires');
+ header("Pragma: public", true);
+ header("Cache-Control: public, max-age=0, must-revalidate", true);
+ header("ETag: $etag");
+ }
+
+ /**
+ * Send a 304 and exit() if the ETag matches the request
+ *
+ * @param string $etag ETag value
+ * @return void
+ */
+ protected function handle304($etag) {
+ // If is the same ETag, content didn't change.
+ if (isset($this->server_vars['HTTP_IF_NONE_MATCH'])
+ && trim($this->server_vars['HTTP_IF_NONE_MATCH']) === $etag) {
+ header("HTTP/1.1 304 Not Modified");
+ exit;
+ }
+ }
+
/**
* Get the content type
*

0 comments on commit 14bd68f

Please sign in to comment.