From db553fb9f3e347d4f8aa3acd3109ab8a7f76bf50 Mon Sep 17 00:00:00 2001 From: remieuronews Date: Tue, 25 Jan 2022 09:56:01 +0100 Subject: [PATCH] [cache] #45109 add stale while revalidate and stale if error cache header --- .../Component/HttpFoundation/CHANGELOG.md | 5 +++ .../Component/HttpFoundation/Response.php | 42 +++++++++++++++++++ .../HttpFoundation/Tests/ResponseTest.php | 38 +++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/src/Symfony/Component/HttpFoundation/CHANGELOG.md b/src/Symfony/Component/HttpFoundation/CHANGELOG.md index b528419d38ac1..04140a3050be1 100644 --- a/src/Symfony/Component/HttpFoundation/CHANGELOG.md +++ b/src/Symfony/Component/HttpFoundation/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.1 +--- + + * Add stale while revalidate and stale if error cache header + 6.0 --- diff --git a/src/Symfony/Component/HttpFoundation/Response.php b/src/Symfony/Component/HttpFoundation/Response.php index accacff8e549b..f45c5386c56b4 100644 --- a/src/Symfony/Component/HttpFoundation/Response.php +++ b/src/Symfony/Component/HttpFoundation/Response.php @@ -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, @@ -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. * @@ -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']); + } + foreach (self::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES as $directive => $hasValue) { if (!$hasValue && isset($options[$directive])) { if ($options[$directive]) { diff --git a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php index dc4a395c1690c..68e54acdcdb1b 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php @@ -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();