Skip to content

Commit

Permalink
Merge pull request #9 from dhoffend/must-revalidate
Browse files Browse the repository at this point in the history
Add must-revalidate flag to Cache-Control
  • Loading branch information
silentworks committed Aug 13, 2015
2 parents 8498366 + 9c94301 commit 29d9c10
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"psr/http-message": "^1.0"
},
"require-dev": {
"slim/slim": "dev-develop"
"slim/slim": "3.x-dev"
},
"autoload": {
"psr-4": {
Expand Down
20 changes: 15 additions & 5 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,25 @@ class Cache
*/
protected $maxAge;

/**
* Cache-Control includes must-revalidate flag
*
* @var bool
*/
protected $mustRevalidate;

/**
* Create new HTTP cache
*
* @param string $type The cache type: "public" or "private"
* @param int $maxAge The maximum age of client-side cache
* @param string $type The cache type: "public" or "private"
* @param int $maxAge The maximum age of client-side cache
* @param bool $mustRevalidate must-revalidate
*/
public function __construct($type = 'private', $maxAge = 86400)
public function __construct($type = 'private', $maxAge = 86400, $mustRevalidate = false)
{
$this->type = $type;
$this->maxAge = $maxAge;
$this->mustRevalidate = $mustRevalidate;
}

/**
Expand All @@ -50,9 +59,10 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
// Cache-Control header
if (!$response->hasHeader('Cache-Control')) {
$response = $response->withHeader('Cache-Control', sprintf(
'%s, max-age=%s',
'%s, max-age=%s%s',
$this->type,
$this->maxAge
$this->maxAge,
$this->mustRevalidate ? ', must-revalidate' : ''
));
}

Expand Down
15 changes: 10 additions & 5 deletions src/CacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,31 @@ public function register(Container $container)
/**
* Enable client-side HTTP caching
*
* @param ResponseInterface $response PSR7 response object
* @param string $type Cache-Control type: "private" or "public"
* @param null|int|string $maxAge Maximum cache age (integer timestamp or datetime string)
* @param ResponseInterface $response PSR7 response object
* @param string $type Cache-Control type: "private" or "public"
* @param null|int|string $maxAge Maximum cache age (integer timestamp or datetime string)
* @param bool $mustRevalidate add option "must-revalidate" to Cache-Control
*
* @return ResponseInterface A new PSR7 response object with `Cache-Control` header
* @throws InvalidArgumentException if the cache-control type is invalid
*/
public function allowCache(ResponseInterface $response, $type = 'private', $maxAge = null)
public function allowCache(ResponseInterface $response, $type = 'private', $maxAge = null, $mustRevalidate = false)
{
if (!in_array($type, ['private', 'public'])) {
throw new InvalidArgumentException('Invalid Cache-Control type. Must be "public" or "private".');
}
$headerValue = $type;
if ($maxAge) {
if ($maxAge || is_integer($maxAge)) {
if (!is_integer($maxAge)) {
$maxAge = strtotime($maxAge);
}
$headerValue = $headerValue . ', max-age=' . $maxAge;
}

if ($mustRevalidate) {
$headerValue = $headerValue . ", must-revalidate";
}

return $response->withHeader('Cache-Control', $headerValue);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/CacheProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ public function testAllowCache()
$this->assertEquals('private, max-age=43200', $cacheControl);
}

public function testAllowCacheWithMustRevalidate()
{
$cacheProvider = new CacheProvider();
$res = $cacheProvider->allowCache(new Response(), 'private', 43200, true);

$cacheControl = $res->getHeaderLine('Cache-Control');

$this->assertEquals('private, max-age=43200, must-revalidate', $cacheControl);
}

public function testDenyCache()
{
$cacheProvider = new CacheProvider();
Expand Down
15 changes: 15 additions & 0 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ public function testCacheControlHeader()
$this->assertEquals('public, max-age=86400', $cacheControl);
}

public function testCacheControlHeaderWithMustRevalidate()
{
$cache = new Cache('private', 86400, true);
$req = $this->requestFactory();
$res = new Response();
$next = function (Request $req, Response $res) {
return $res;
};
$res = $cache($req, $res, $next);

$cacheControl = $res->getHeaderLine('Cache-Control');

$this->assertEquals('private, max-age=86400, must-revalidate', $cacheControl);
}

public function testCacheControlHeaderDoesNotOverrideExistingHeader()
{
$cache = new Cache('public', 86400);
Expand Down

0 comments on commit 29d9c10

Please sign in to comment.