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 db553fb

Browse filesBrowse files
author
remieuronews
committed
[cache] #45109 add stale while revalidate and stale if error cache header
1 parent 057844d commit db553fb
Copy full SHA for db553fb

File tree

3 files changed

+85
-0
lines changed
Filter options

3 files changed

+85
-0
lines changed

‎src/Symfony/Component/HttpFoundation/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* Add stale while revalidate and stale if error cache header
8+
49
6.0
510
---
611

‎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,
@@ -773,6 +775,38 @@ public function setMaxAge(int $value): static
773775
return $this;
774776
}
775777

778+
/**
779+
* Sets the number of seconds after which the response should no longer be returned by shared caches when backend is down.
780+
*
781+
* This method sets the Cache-Control stale-if-error directive.
782+
*
783+
* @return $this
784+
*
785+
* @final
786+
*/
787+
public function setStaleIfError(int $value): static
788+
{
789+
$this->headers->addCacheControlDirective('stale-if-error', $value);
790+
791+
return $this;
792+
}
793+
794+
/**
795+
* Sets the number of seconds after which the response should no longer return stale content by shared caches.
796+
*
797+
* This method sets the Cache-Control stale-while-revalidate directive.
798+
*
799+
* @return $this
800+
*
801+
* @final
802+
*/
803+
public function setStaleWhileRevalidate(int $value): static
804+
{
805+
$this->headers->addCacheControlDirective('stale-while-revalidate', $value);
806+
807+
return $this;
808+
}
809+
776810
/**
777811
* Sets the number of seconds after which the response should no longer be considered fresh by shared caches.
778812
*
@@ -946,6 +980,14 @@ public function setCache(array $options): static
946980
$this->setSharedMaxAge($options['s_maxage']);
947981
}
948982

983+
if (isset($options['stale_while_revalidate'])) {
984+
$this->setStaleWhileRevalidate($options['stale_while_revalidate']);
985+
}
986+
987+
if (isset($options['stale_if_error'])) {
988+
$this->setStaleIfError($options['stale_if_error']);
989+
}
990+
949991
foreach (self::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES as $directive => $hasValue) {
950992
if (!$hasValue && isset($options[$directive])) {
951993
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
@@ -344,6 +344,44 @@ public function testSetSharedMaxAge()
344344
$this->assertEquals('public, s-maxage=20', $cacheControl);
345345
}
346346

347+
public function testSetStaleIfError()
348+
{
349+
$response = new Response();
350+
$response->setSharedMaxAge(20);
351+
$response->setStaleIfError(86400);
352+
353+
$cacheControl = $response->headers->get('Cache-Control');
354+
$this->assertEquals('public, s-maxage=20, stale-if-error=86400', $cacheControl);
355+
}
356+
357+
public function testSetStaleWhileRevalidate()
358+
{
359+
$response = new Response();
360+
$response->setSharedMaxAge(20);
361+
$response->setStaleWhileRevalidate(300);
362+
363+
$cacheControl = $response->headers->get('Cache-Control');
364+
$this->assertEquals('public, s-maxage=20, stale-while-revalidate=300', $cacheControl);
365+
}
366+
367+
public function testSetStaleIfErrorWithoutSharedMaxAge()
368+
{
369+
$response = new Response();
370+
$response->setStaleIfError(86400);
371+
372+
$cacheControl = $response->headers->get('Cache-Control');
373+
$this->assertEquals('stale-if-error=86400, private', $cacheControl);
374+
}
375+
376+
public function testSetStaleWhileRevalidateWithoutSharedMaxAge()
377+
{
378+
$response = new Response();
379+
$response->setStaleWhileRevalidate(300);
380+
381+
$cacheControl = $response->headers->get('Cache-Control');
382+
$this->assertEquals('stale-while-revalidate=300, private', $cacheControl);
383+
}
384+
347385
public function testIsPrivate()
348386
{
349387
$response = new Response();

0 commit comments

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