Skip to content

Commit 29d9c10

Browse files
committed
Merge pull request #9 from dhoffend/must-revalidate
Add must-revalidate flag to Cache-Control
2 parents 8498366 + 9c94301 commit 29d9c10

File tree

5 files changed

+51
-11
lines changed

5 files changed

+51
-11
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"psr/http-message": "^1.0"
1919
},
2020
"require-dev": {
21-
"slim/slim": "dev-develop"
21+
"slim/slim": "3.x-dev"
2222
},
2323
"autoload": {
2424
"psr-4": {

src/Cache.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,25 @@ class Cache
2222
*/
2323
protected $maxAge;
2424

25+
/**
26+
* Cache-Control includes must-revalidate flag
27+
*
28+
* @var bool
29+
*/
30+
protected $mustRevalidate;
31+
2532
/**
2633
* Create new HTTP cache
2734
*
28-
* @param string $type The cache type: "public" or "private"
29-
* @param int $maxAge The maximum age of client-side cache
35+
* @param string $type The cache type: "public" or "private"
36+
* @param int $maxAge The maximum age of client-side cache
37+
* @param bool $mustRevalidate must-revalidate
3038
*/
31-
public function __construct($type = 'private', $maxAge = 86400)
39+
public function __construct($type = 'private', $maxAge = 86400, $mustRevalidate = false)
3240
{
3341
$this->type = $type;
3442
$this->maxAge = $maxAge;
43+
$this->mustRevalidate = $mustRevalidate;
3544
}
3645

3746
/**
@@ -50,9 +59,10 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
5059
// Cache-Control header
5160
if (!$response->hasHeader('Cache-Control')) {
5261
$response = $response->withHeader('Cache-Control', sprintf(
53-
'%s, max-age=%s',
62+
'%s, max-age=%s%s',
5463
$this->type,
55-
$this->maxAge
64+
$this->maxAge,
65+
$this->mustRevalidate ? ', must-revalidate' : ''
5666
));
5767
}
5868

src/CacheProvider.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,31 @@ public function register(Container $container)
2121
/**
2222
* Enable client-side HTTP caching
2323
*
24-
* @param ResponseInterface $response PSR7 response object
25-
* @param string $type Cache-Control type: "private" or "public"
26-
* @param null|int|string $maxAge Maximum cache age (integer timestamp or datetime string)
24+
* @param ResponseInterface $response PSR7 response object
25+
* @param string $type Cache-Control type: "private" or "public"
26+
* @param null|int|string $maxAge Maximum cache age (integer timestamp or datetime string)
27+
* @param bool $mustRevalidate add option "must-revalidate" to Cache-Control
2728
*
2829
* @return ResponseInterface A new PSR7 response object with `Cache-Control` header
2930
* @throws InvalidArgumentException if the cache-control type is invalid
3031
*/
31-
public function allowCache(ResponseInterface $response, $type = 'private', $maxAge = null)
32+
public function allowCache(ResponseInterface $response, $type = 'private', $maxAge = null, $mustRevalidate = false)
3233
{
3334
if (!in_array($type, ['private', 'public'])) {
3435
throw new InvalidArgumentException('Invalid Cache-Control type. Must be "public" or "private".');
3536
}
3637
$headerValue = $type;
37-
if ($maxAge) {
38+
if ($maxAge || is_integer($maxAge)) {
3839
if (!is_integer($maxAge)) {
3940
$maxAge = strtotime($maxAge);
4041
}
4142
$headerValue = $headerValue . ', max-age=' . $maxAge;
4243
}
4344

45+
if ($mustRevalidate) {
46+
$headerValue = $headerValue . ", must-revalidate";
47+
}
48+
4449
return $response->withHeader('Cache-Control', $headerValue);
4550
}
4651

tests/CacheProviderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ public function testAllowCache()
1616
$this->assertEquals('private, max-age=43200', $cacheControl);
1717
}
1818

19+
public function testAllowCacheWithMustRevalidate()
20+
{
21+
$cacheProvider = new CacheProvider();
22+
$res = $cacheProvider->allowCache(new Response(), 'private', 43200, true);
23+
24+
$cacheControl = $res->getHeaderLine('Cache-Control');
25+
26+
$this->assertEquals('private, max-age=43200, must-revalidate', $cacheControl);
27+
}
28+
1929
public function testDenyCache()
2030
{
2131
$cacheProvider = new CacheProvider();

tests/CacheTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ public function testCacheControlHeader()
3737
$this->assertEquals('public, max-age=86400', $cacheControl);
3838
}
3939

40+
public function testCacheControlHeaderWithMustRevalidate()
41+
{
42+
$cache = new Cache('private', 86400, true);
43+
$req = $this->requestFactory();
44+
$res = new Response();
45+
$next = function (Request $req, Response $res) {
46+
return $res;
47+
};
48+
$res = $cache($req, $res, $next);
49+
50+
$cacheControl = $res->getHeaderLine('Cache-Control');
51+
52+
$this->assertEquals('private, max-age=86400, must-revalidate', $cacheControl);
53+
}
54+
4055
public function testCacheControlHeaderDoesNotOverrideExistingHeader()
4156
{
4257
$cache = new Cache('public', 86400);

0 commit comments

Comments
 (0)