From 03e25526a4163bb0cc488776aa46e6f1b05c8cbd Mon Sep 17 00:00:00 2001 From: Cody Lundquist Date: Fri, 18 Oct 2013 13:41:27 +1100 Subject: [PATCH] Adding in max-age in cache control and an option to set it in the dispatcher file. The default value is 0. --- src/Munee/Dispatcher.php | 7 +++++-- src/Munee/Response.php | 8 +++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Munee/Dispatcher.php b/src/Munee/Dispatcher.php index 95ee441..cf577f8 100644 --- a/src/Munee/Dispatcher.php +++ b/src/Munee/Dispatcher.php @@ -20,7 +20,10 @@ class Dispatcher * * @var array */ - static $defaultOptions = array('setHeaders' => true); + static $defaultOptions = array( + 'setHeaders' => true, + 'maxAge' => 0 + ); /** * 1) Initialise the Request @@ -79,7 +82,7 @@ public static function run(Request $Request, $options = array()) /** * Set the headers. */ - $Response->setHeaders(); + $Response->setHeaders($options['maxAge']); } /** * If the content hasn't been modified return null so only headers are sent diff --git a/src/Munee/Response.php b/src/Munee/Response.php index 244a162..ba69761 100644 --- a/src/Munee/Response.php +++ b/src/Munee/Response.php @@ -54,7 +54,7 @@ public function __construct($AssetType) } $this->assetType = $AssetType; - + $AssetType->setResponse($this); } @@ -81,11 +81,13 @@ public function setHeaderController($headerController) /** * Set Headers for Response * + * @param integer $maxAge - Used with the cache-control to tell the browser how long it should wait before revalidating + * * @return self * * @throws ErrorException */ - public function setHeaders() + public function setHeaders($maxAge) { $lastModifiedDate = $this->assetType->getLastModifiedDate(); $eTag = md5($lastModifiedDate . $this->assetType->getContent()); @@ -102,7 +104,7 @@ public function setHeaders() $this->notModified = true; } else { // We don't want the browser to handle any cache, Munee will handle that. - $this->headerController->headerField('Cache-Control', 'must-revalidate'); + $this->headerController->headerField('Cache-Control', 'max-age=' . $maxAge . ', must-revalidate'); $this->headerController->headerField('Last-Modified', gmdate('D, d M Y H:i:s', $lastModifiedDate) . ' GMT'); $this->headerController->headerField('ETag', $eTag); $this->assetType->getHeaders();