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 09bcbc7

Browse filesBrowse files
committed
Embedding a response that combines expiration and validation, that should not defeat expiration on the combined response
1 parent 551e5ba commit 09bcbc7
Copy full SHA for 09bcbc7

File tree

2 files changed

+45
-1
lines changed
Filter options

2 files changed

+45
-1
lines changed

‎src/Symfony/Component/HttpKernel/HttpCache/ResponseCacheStrategy.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/HttpCache/ResponseCacheStrategy.php
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
3939
*/
4040
public function add(Response $response)
4141
{
42-
if ($response->isValidateable() || !$response->isCacheable()) {
42+
if (!$response->isFresh() || !$response->isCacheable()) {
4343
$this->cacheable = false;
4444
} else {
4545
$maxAge = $response->getMaxAge();
@@ -70,6 +70,9 @@ public function update(Response $response)
7070
if ($response->isValidateable()) {
7171
$response->setEtag(null);
7272
$response->setLastModified(null);
73+
}
74+
75+
if (!$response->isFresh()) {
7376
$this->cacheable = false;
7477
}
7578

‎src/Symfony/Component/HttpKernel/Tests/HttpCache/ResponseCacheStrategyTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/HttpCache/ResponseCacheStrategyTest.php
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,45 @@ public function testEmbeddingPrivateResponseMakesMainResponsePrivate()
178178
// Not sure if we should pass "max-age: 60" in this case, as long as the response is private and
179179
// that's the more conservative of both the master and embedded response...?
180180
}
181+
182+
public function testResponseIsExiprableWhenEmbeddedResponseCombinesExpiryAndValidation()
183+
{
184+
/* When "expiration wins over validation" (https://symfony.com/doc/current/http_cache/validation.html)
185+
* and both the main and embedded response provide s-maxage, then the more restricting value of both
186+
* should be fine, regardless of whether the embedded response can be validated later on or must be
187+
* completely regenerated.
188+
*/
189+
$cacheStrategy = new ResponseCacheStrategy();
190+
191+
$masterResponse = new Response();
192+
$masterResponse->setSharedMaxAge(3600);
193+
194+
$embeddedResponse = new Response();
195+
$embeddedResponse->setSharedMaxAge(60);
196+
$embeddedResponse->setEtag('foo');
197+
198+
$cacheStrategy->add($embeddedResponse);
199+
$cacheStrategy->update($masterResponse);
200+
201+
$this->assertSame('60', $masterResponse->headers->getCacheControlDirective('s-maxage'));
202+
}
203+
204+
public function testResponseIsExpirableButNotValidateableWhenMasterResponseCombinesExpirationAndValidation()
205+
{
206+
$cacheStrategy = new ResponseCacheStrategy();
207+
208+
$masterResponse = new Response();
209+
$masterResponse->setSharedMaxAge(3600);
210+
$masterResponse->setEtag('foo');
211+
$masterResponse->setLastModified(new \DateTime());
212+
213+
$embeddedResponse = new Response();
214+
$embeddedResponse->setSharedMaxAge(60);
215+
216+
$cacheStrategy->add($embeddedResponse);
217+
$cacheStrategy->update($masterResponse);
218+
219+
$this->assertSame('60', $masterResponse->headers->getCacheControlDirective('s-maxage'));
220+
$this->assertFalse($masterResponse->isValidateable());
221+
}
181222
}

0 commit comments

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