Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5f16138

Browse filesBrowse files
author
remieuronews
committed
[cache] #45109 add stale while revalidate cache header
1 parent cd7bf9b commit 5f16138
Copy full SHA for 5f16138

File tree

2 files changed

+80
-0
lines changed
Filter options

2 files changed

+80
-0
lines changed

‎src/Symfony/Component/HttpFoundation/Response.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Response.php
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ class Response
9898
'proxy_revalidate' => false,
9999
'max_age' => true,
100100
's_maxage' => true,
101+
'stale_if_error' => true, // RFC5861
102+
'stale_while_revalidate' => true, // RFC5861
101103
'immutable' => false,
102104
'last_modified' => true,
103105
'etag' => true,
@@ -796,6 +798,38 @@ public function setMaxAge(int $value): object
796798
return $this;
797799
}
798800

801+
/**
802+
* Sets the number of seconds after which the response should no longer be returned by shared caches when backend is down.
803+
*
804+
* This methods sets the Cache-Control stale-if-error directive.
805+
*
806+
* @return $this
807+
*
808+
* @final
809+
*/
810+
public function setStaleIfError(int $value): object
811+
{
812+
$this->headers->addCacheControlDirective('stale-if-error', $value);
813+
814+
return $this;
815+
}
816+
817+
/**
818+
* Sets the number of seconds after which the response should no longer return stale content by shared caches.
819+
*
820+
* This methods sets the Cache-Control stale-while-revalidate directive.
821+
*
822+
* @return $this
823+
*
824+
* @final
825+
*/
826+
public function setStaleWhileRevalidate(int $value): object
827+
{
828+
$this->headers->addCacheControlDirective('stale-while-revalidate', $value);
829+
830+
return $this;
831+
}
832+
799833
/**
800834
* Sets the number of seconds after which the response should no longer be considered fresh by shared caches.
801835
*
@@ -969,6 +1003,14 @@ public function setCache(array $options): object
9691003
$this->setSharedMaxAge($options['s_maxage']);
9701004
}
9711005

1006+
if (isset($options['stale_while_revalidate'])) {
1007+
$this->setStaleWhileRevalidate($options['stale_while_revalidate']);
1008+
}
1009+
1010+
if (isset($options['stale_if_error'])) {
1011+
$this->setStaleIfError($options['stale_if_error']);
1012+
}
1013+
9721014
foreach (self::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES as $directive => $hasValue) {
9731015
if (!$hasValue && isset($options[$directive])) {
9741016
if ($options[$directive]) {

‎src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,44 @@ public function testSetSharedMaxAge()
356356
$this->assertEquals('public, s-maxage=20', $cacheControl);
357357
}
358358

359+
public function testSetStaleIfError()
360+
{
361+
$response = new Response();
362+
$response->setSharedMaxAge(20);
363+
$response->setStaleIfError(86400);
364+
365+
$cacheControl = $response->headers->get('Cache-Control');
366+
$this->assertEquals('public, s-maxage=20, stale-if-error=86400', $cacheControl);
367+
}
368+
369+
public function testSetStaleWhileRevalidate()
370+
{
371+
$response = new Response();
372+
$response->setSharedMaxAge(20);
373+
$response->setStaleWhileRevalidate(300);
374+
375+
$cacheControl = $response->headers->get('Cache-Control');
376+
$this->assertEquals('public, s-maxage=20, stale-while-revalidate=300', $cacheControl);
377+
}
378+
379+
public function testSetStaleIfErrorWithoutSharedMaxAge()
380+
{
381+
$response = new Response();
382+
$response->setStaleIfError(86400);
383+
384+
$cacheControl = $response->headers->get('Cache-Control');
385+
$this->assertEquals('stale-if-error=86400, private', $cacheControl);
386+
}
387+
388+
public function testSetStaleWhileRevalidateWithoutSharedMaxAge()
389+
{
390+
$response = new Response();
391+
$response->setStaleWhileRevalidate(300);
392+
393+
$cacheControl = $response->headers->get('Cache-Control');
394+
$this->assertEquals('stale-while-revalidate=300, private', $cacheControl);
395+
}
396+
359397
public function testIsPrivate()
360398
{
361399
$response = new Response();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.