Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit1be75f9

Browse files
author
remieuronews
committed
[cache]#45109 add stale while revalidate and stale if error cache header
1 parent43e3e71 commit1be75f9

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

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

Lines 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‎

Lines 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+
publicfunctionsetStaleIfError(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+
publicfunctionsetStaleWhileRevalidate(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_DIRECTIVESas$directive =>$hasValue) {
950992
if (!$hasValue &&isset($options[$directive])) {
951993
if ($options[$directive]) {

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

Lines 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+
publicfunctiontestSetStaleIfError()
348+
{
349+
$response =newResponse();
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+
publicfunctiontestSetStaleWhileRevalidate()
358+
{
359+
$response =newResponse();
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+
publicfunctiontestSetStaleIfErrorWithoutSharedMaxAge()
368+
{
369+
$response =newResponse();
370+
$response->setStaleIfError(86400);
371+
372+
$cacheControl =$response->headers->get('Cache-Control');
373+
$this->assertEquals('stale-if-error=86400, private',$cacheControl);
374+
}
375+
376+
publicfunctiontestSetStaleWhileRevalidateWithoutSharedMaxAge()
377+
{
378+
$response =newResponse();
379+
$response->setStaleWhileRevalidate(300);
380+
381+
$cacheControl =$response->headers->get('Cache-Control');
382+
$this->assertEquals('stale-while-revalidate=300, private',$cacheControl);
383+
}
384+
347385
publicfunctiontestIsPrivate()
348386
{
349387
$response =newResponse();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp