Skip to content

Commit

Permalink
Adding in max-age in cache control and an option to set it in the dis…
Browse files Browse the repository at this point in the history
…patcher file. The default value is 0.
  • Loading branch information
meenie committed Oct 18, 2013
1 parent 1291d3b commit 03e2552
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/Munee/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class Dispatcher
*
* @var array
*/
static $defaultOptions = array('setHeaders' => true);
static $defaultOptions = array(
'setHeaders' => true,
'maxAge' => 0
);

/**
* 1) Initialise the Request
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/Munee/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct($AssetType)
}

$this->assetType = $AssetType;

$AssetType->setResponse($this);
}

Expand All @@ -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());
Expand All @@ -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();
Expand Down

0 comments on commit 03e2552

Please sign in to comment.