-
Notifications
You must be signed in to change notification settings - Fork 60
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
fix custom ttl with symfony HttpCache to work regardless of s-maxage #577
Conversation
->andReturn($expectedResponse) | ||
->getMock(); | ||
$store = \Mockery::mock(StoreInterface::class) | ||
->shouldReceive('lookup')->andReturn(null)->times(1) | ||
->shouldReceive('write')->times(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when i only add this, the test starts failing because write is never called.
For reference here I added a comment to: sulu/sulu#7441 (comment) which also is little bit related to new flag Copy of the comment: so changes here are understandable in future: The only part which we may need to recheck is the If we want that diff --git a/src/SymfonyCache/CustomTtlListener.php b/src/SymfonyCache/CustomTtlListener.php
index 4be17d9..9ff303e 100644
--- a/src/SymfonyCache/CustomTtlListener.php
+++ b/src/SymfonyCache/CustomTtlListener.php
@@ -77,12 +77,18 @@ class CustomTtlListener implements EventSubscriberInterface
? $response->headers->getCacheControlDirective('s-maxage')
: 'false'
;
+
$response->headers->set(static::SMAXAGE_BACKUP, $backup);
+ $isPrivate = $response->headers->getCacheControlDirective('private');
+ // calling setTtl will make a response public, so we need to set it back to private if it was private before
$response->setTtl(
$response->headers->has($this->ttlHeader)
? $response->headers->get($this->ttlHeader)
: 0
);
+ if ($isPrivate) {
+ $response->setPrivate();
+ }
}
/** Or alternative us diff --git a/src/SymfonyCache/CustomTtlListener.php b/src/SymfonyCache/CustomTtlListener.php
index 4be17d9..31126f3 100644
--- a/src/SymfonyCache/CustomTtlListener.php
+++ b/src/SymfonyCache/CustomTtlListener.php
@@ -77,8 +77,11 @@ class CustomTtlListener implements EventSubscriberInterface
? $response->headers->getCacheControlDirective('s-maxage')
: 'false'
;
+
$response->headers->set(static::SMAXAGE_BACKUP, $backup);
- $response->setTtl(
+
+ $response->headers->addCacheControlDirective(
+ 's-maxage',
$response->headers->has($this->ttlHeader)
? $response->headers->get($this->ttlHeader)
: 0 What do you think would be expected behaviour for: $response = new Response();
$response->setPrivate(0);
$response->header->set('X-Reverse-Proxy-TTL', 1200); Current version (2.15.3) would not cache it. With #577 and |
uh, very good catch! i change the code to use setCacheControlDirective. i guess until now this mistake was hidden by the listener not being called at all for a private response. i think we should keep that behaviour. indeed targeted cache control would be very useful to avoid this mess: symfony/symfony#47288 (we could also provide that functionality with FOSHttpCache rules, but first should try to do the pull request to core symfony) |
@dbu retested the change and looks good for me. |
fix #576