Skip to content

Commit ac4951a

Browse files
committed
add must-revalidate flag to Cache-Control header
1 parent 8498366 commit ac4951a

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

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: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ 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".');
@@ -41,6 +42,10 @@ public function allowCache(ResponseInterface $response, $type = 'private', $maxA
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

0 commit comments

Comments
 (0)