8000 [HttpKernel] Be smarter when merging cache directives in HttpCache/ResponseCacheStrategy by nicolas-grekas · Pull Request #26352 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] Be smarter when merging cache directives in HttpCache/ResponseCacheStrategy #26352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
{
private $cacheable = true;
private $embeddedResponses = 0;
private $cacheDirectives = array(
'no-cache' => false,
'no-store' => false,
'must-revalidate' => false,
'private' => false,
'proxy-revalidate' => false,
);
private $ttls = array();
private $maxAges = array();
private $isNotCacheableResponseEmbedded = false;
Expand All @@ -41,6 +48,17 @@ public function add(Response $response)
{
if (!$response->isFresh() || !$response->isCacheable()) {
$this->cacheable = false;

$hasCacheDirective = $response->headers->hasCacheControlDirective('public');
foreach ($this->cacheDirectives as $directive => $status) {
if ($response->headers->hasCacheControlDirective($directive)) {
$this->cacheDirectives[$directive] = $hasCacheDirective = true;
}
}

if (!$hasCacheDirective || !$response->isFresh()) {
$this->cacheDirectives['no-cache'] = $this->cacheDirectives['must-revalidate'] = true;
}
} else {
$maxAge = $response->getMaxAge();
$this->ttls[] = $response->getTtl();
Expand Down Expand Up @@ -72,19 +90,14 @@ public function update(Response $response)
$response->setLastModified(null);
}

if (!$response->isFresh()) {
$this->cacheable = false;
}
$this->add($response);

if (!$this->cacheable) {
$response->headers->set('Cache-Control', 'no-cache, must-revalidate');
$response->headers->set('Cache-Control', implode(', ', array_keys(array_filter($this->cacheDirectives))));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about the max age allowing client-side caching ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's say we don't care, we don't have to be that smart?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, allowing private caching without telling the browser how long it can cache it is broken: it won't allow caching in well-behaving browsers (and may allow infinite caching if a crappy browser treats the absence of age on the response as an invitation to cache forever instead of for 0s).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

client side caching can have 2 modes: expiration-based (caching for a timestamp) and validation-based (asking whether the last-modified or the etag they have is still the right one).
When using ESI or SSI (which this code is about), we don't send an Etag or LastModified (which this class is handling) because we cannot build them properly when the final response is built based on embedded responses. So this only leaves the expiration-based caching client-side. If you don't care about it, close your PR as it brings nothing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm about to create unit tests to validate that case so we can implement the re 10000 quired checks.

we don't send an Etag or LastModified (which this class is handling) because we cannot build them properly when the final response is built based on embedded responses.

Actually we could do that by generating an ETag based on the complete response. Something like a hash of the whole content. But I think that's for another PR if we want to add that at all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not helpful, as we could still not use Etag validation of the different sub-requests as we would not have Etags for each of them (unless we build the final Etag as a reversible combination of info about each response), and so we could not perform Etag validation in the controllers generating each subresponse.


return;
}

$this->ttls[] = $response->getTtl();
$this->maxAges[] = $response->getMaxAge();

if ($this->isNotCacheableResponseEmbedded) {
$response->headers->removeCacheControlDirective('s-maxage');
} elseif (null !== $maxAge = min($this->maxAges)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,24 @@ public function testResponseIsExpirableButNotValidateableWhenMasterResponseCombi
$this->assertSame('60', $masterResponse->headers->getCacheControlDirective('s-maxage'));
$this->assertFalse($masterResponse->isValidateable());
}

public function testResponseIsPrivateWhenCombiningPrivateResponses()
{
$cacheStrategy = new ResponseCacheStrategy();

$masterResponse = new Response();
$masterResponse->setSharedMaxAge(60);
$masterResponse->setPrivate();

$embeddedResponse = new Response();
$embeddedResponse->setSharedMaxAge(60);
$embeddedResponse->setPrivate();

$cacheStrategy->add($embeddedResponse);
$cacheStrategy->update($masterResponse);

$this->assertFalse($masterResponse->headers->hasCacheControlDirective('no-cache'));
$this->assertFalse($masterResponse->headers->hasCacheControlDirective('must-revalidate'));
$this->assertTrue($masterResponse->headers->hasCacheControlDirective('private'));
}
}
0