Skip to content

[HttpFoundation] add stale while revalidate cache header #45166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.1
---

* Add stale while revalidate and stale if error cache header

6.0
---

Expand Down
42 changes: 42 additions & 0 deletions src/Symfony/Component/HttpFoundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class Response
'proxy_revalidate' => false,
'max_age' => true,
's_maxage' => true,
'stale_if_error' => true, // RFC5861
'stale_while_revalidate' => true, // RFC5861
'immutable' => false,
'last_modified' => true,
'etag' => true,
Expand Down Expand Up @@ -773,6 +775,38 @@ public function setMaxAge(int $value): static
return $this;
}

/**
* Sets the number of seconds after which the response should no longer be returned by shared caches when backend is down.
*
* This method sets the Cache-Control stale-if-error directive.
*
* @return $this
*
* @final
*/
public function setStaleIfError(int $value): static
{
$this->headers->addCacheControlDirective('stale-if-error', $value);

return $this;
}

/**
* Sets the number of seconds after which the response should no longer return stale content by shared caches.
*
* This method sets the Cache-Control stale-while-revalidate directive.
*
* @return $this
*
* @final
*/
public function setStaleWhileRevalidate(int $value): static
{
$this->headers->addCacheControlDirective('stale-while-revalidate', $value);

return $this;
}

/**
* Sets the number of seconds after which the response should no longer be considered fresh by shared caches.
*
Expand Down Expand Up @@ -946,6 +980,14 @@ public function setCache(array $options): static
$this->setSharedMaxAge($options['s_maxage']);
}

if (isset($options['stale_while_revalidate'])) {
$this->setStaleWhileRevalidate($options['stale_while_revalidate']);
}

if (isset($options['stale_if_error'])) {
$this->setStaleIfError($options['stale_if_error']);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not needed, the next foreach will do it IIUC


foreach (self::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES as $directive => $hasValue) {
if (!$hasValue && isset($options[$directive])) {
if ($options[$directive]) {
Expand Down
38 changes: 38 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,44 @@ public function testSetSharedMaxAge()
$this->assertEquals('public, s-maxage=20', $cacheControl);
}

public function testSetStaleIfError()
{
$response = new Response();
$response->setSharedMaxAge(20);
$response->setStaleIfError(86400);

$cacheControl = $response->headers->get('Cache-Control');
$this->assertEquals('public, s-maxage=20, stale-if-error=86400', $cacheControl);
}

public function testSetStaleWhileRevalidate()
{
$response = new Response();
$response->setSharedMaxAge(20);
$response->setStaleWhileRevalidate(300);

$cacheControl = $response->headers->get('Cache-Control');
$this->assertEquals('public, s-maxage=20, stale-while-revalidate=300', $cacheControl);
}

public function testSetStaleIfErrorWithoutSharedMaxAge()
{
$response = new Response();
$response->setStaleIfError(86400);

$cacheControl = $response->headers->get('Cache-Control');
$this->assertEquals('stale-if-error=86400, private', $cacheControl);
}

public function testSetStaleWhileRevalidateWithoutSharedMaxAge()
{
$response = new Response();
$response->setStaleWhileRevalidate(300);

$cacheControl = $response->headers->get('Cache-Control');
$this->assertEquals('stale-while-revalidate=300, private', $cacheControl);
}

public function testIsPrivate()
{
$response = new Response();
Expand Down