Skip to content
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

Merged
merged 1 commit into from
Jul 24, 2024

Conversation

dbu
Copy link
Contributor

@dbu dbu commented Jul 23, 2024

fix #576

->andReturn($expectedResponse)
->getMock();
$store = \Mockery::mock(StoreInterface::class)
->shouldReceive('lookup')->andReturn(null)->times(1)
->shouldReceive('write')->times(1)
Copy link
Contributor Author

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.

@alexander-schranz
Copy link
Contributor

alexander-schranz commented Jul 23, 2024

For reference here I added a comment to: sulu/sulu#7441 (comment) which also is little bit related to new flag CustomTtlListener(fallbackToSmaxage: false) from #575

Copy of the comment: so changes here are understandable in future:

The only part which we may need to recheck is the CustomTtlListener makes now private requests always public, specially in case of $fallbackToSmaxAge = false.

If we want that private responses are never cached also when CustomTtlListener is used we would require:

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 addCacheControlDirective instead of setTtl:

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 CustomTtlListener(fallbackToSmaxage: false) it will now be cached. But I think explicit set private responses should not be cached even when a custom ttl is set? As example private response with a s-maxage defined is also not cached.

@dbu
Copy link
Contributor Author

dbu commented Jul 24, 2024

For reference here I added a comment to: sulu/sulu#7441 (comment) which also is little bit related to new flag CustomTtlListener(fallbackToSmaxage: false) from #575

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)

@alexander-schranz
Copy link
Contributor

@dbu retested the change and looks good for me.

@dbu dbu merged commit f538e2b into 2.x Jul 24, 2024
15 checks passed
@dbu dbu deleted the fix-custom-ttl branch July 24, 2024 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants