[WIP] Convert X-Location-Id into tags for non-view responses#21
[WIP] Convert X-Location-Id into tags for non-view responses#21Plopix wants to merge 1 commit intoezsystems:masterfrom Plopix:feature-fix-http-cache-varnish
Conversation
| { | ||
| $view = $event->getRequest()->attributes->get('view'); | ||
| if (!$view instanceof CachableView || !$view->isCacheEnabled()) { | ||
| if ($view !== null && ( !$view instanceof CachableView || !$view->isCacheEnabled() )) { |
There was a problem hiding this comment.
why this change? We don't want to set ttl on non ContentView response automatically. People can set their own s-maxage depending on what logic they have and what they need.
At lest let's keep this out of this bug fix.
There was a problem hiding this comment.
Ok I have used @vidarl logic here, it breaks the unit tests though, can someone fix them?
| ) { | ||
| $this->repository = $repository; | ||
| $this->contentInfoTagger = $contentInfoTagger; | ||
| $this->locationTagger = $locationTagger; |
There was a problem hiding this comment.
I get what you are trying to do here, but let's just map the location id to xkey equivalent here, and unset the x-Location-Id parameter to not let that stay around.
PR's like #7 and similar are meant to add the other tags based on the data already loaded, to avoid us having to load repository objects several times.
In other words, you won't need any dependencies here.
--
In addtion, this PR should remove the following lines as it will be covered here instead: https://github.com/ezsystems/ezplatform-http-cache/blob/master/src/Proxy/TagAwareStore.php#L76-L78
|
ping @andrerom @bdunogier @vidarl I have reworked it @andrerom and I have used your name @vidarl |
bdunogier
left a comment
There was a problem hiding this comment.
You could add a Spec for the tagger. You'll see, phpsec is great :)
| use EzSystems\PlatformHttpCacheBundle\ResponseTagger\ResponseTagger; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
|
|
||
| class XLocationIdBCTagger implements ResponseTagger |
There was a problem hiding this comment.
Could you add a summary of what the Tagger does, and in what context it is (not) useful ?
| const LOCATION_ID_HEADER = 'X-Location-Id'; | ||
|
|
||
| /** | ||
| * XLocationIdTagger constructor. |
There was a problem hiding this comment.
Nitpick: I'd remove that block altogether. Including the @param.
| foreach ($locationIds as $locationId) { | ||
| $location = $this->repository->getLocationService()->loadLocation($locationId); | ||
| $this->locationTagger->tag($configurator, $response, $location); | ||
| $response->headers->remove('X-Location-Id'); |
There was a problem hiding this comment.
You should replace 'X-Location-ID' with self::LOCATION_ID_HEADER.
| $view = $event->getRequest()->attributes->get('view'); | ||
| if (!$view instanceof CachableView || !$view->isCacheEnabled()) { | ||
| return; | ||
| if ($event->getRequest()->attributes->has('is_rest_request') && $event->getRequest()->attributes->get('is_rest_request') === true) { |
There was a problem hiding this comment.
I still don't see why this needs to be added, afaik REST already sets s-maxage doesn't it? Is this needed to solve this bug or can this be handled separately?
There was a problem hiding this comment.
Yes, that is required because for REST requests the kernel does not return a view.
Then, as it is not a view, it returns null and nothing is tagged accordingly.
Please note that when we return null here, the response is cached for the default time.
|
|
||
| $locationIds = explode("|", $response->headers->get(static::LOCATION_ID_HEADER)); | ||
| foreach ($locationIds as $locationId) { | ||
| $location = $this->repository->getLocationService()->loadLocation($locationId); |
There was a problem hiding this comment.
After internal discussions, we aren't very comfortable with loading locations in this part. What happens if loading fails, for instance because of permissions ? We only need to do this because the Value Objects used to generate this header aren't available anymore.
Did you consider going for a simpler tagging, and only generate location-<locationId> ? It would avoid having to load the location.
On the long term, this will have to be fixed in the proper layers anyway. For instance, we have two pull-requests about REST & HTTP caching, both in regards to REST includes: ezsystems/ezpublish-kernel#1741 & ezsystems/ezpublish-kernel#1789 (WIP). They've been here for a while, but do touch the same areas.
As an alternative, an option would be to override the CachedValue value object visitor from the REST server. This is where we generate the X-Location-ID header. As for available data, we have both the location ID and the the cached value object, meaning that we can use Response Taggers.
|
@andrerom @bdunogier @vidarl I have changed stuff. |
|
And we would not mess with the '$view' in the subscriber. It sounds better
to tag there directly indeed.
But how about custom devs that are using X-Location-Id? that needs to be
working right?
ok for location i will remove the load etc.
Questions remain to know where to do it correctly.
|
|
I suppose this PR is not useful anymore right? if so, you can close. |
With this bundle, REST HttpCache is not cleared when moving content, editing content types, (...) And given the time it will take to fix REST server to return data that can be used for ResponseTaggers and how this also affects custum responses given, as we now only purge by specific tags. Adding this is thus safests. This somewhat brings back what @Plopix suggested in #21, essentially making sure we add all tags also to X-Location responses by loading location, but to be on the safe side it does it using sudo() and catching NotFound. Opted not to reuse ReponseTaggers here as they don't fully fit, and as we want to rewrite this header independently of if view cache is enabled or not.
With this bundle, REST HttpCache is not cleared when moving content, editing content types, (...) And given the time it will take to fix REST server to return data that can be used for ResponseTaggers and how this also affects custum responses given, as we now only purge by specific tags. Adding this is thus safests. This somewhat brings back what @Plopix suggested in #21, essentially making sure we add all tags also to X-Location responses by loading location, but to be on the safe side it does it using sudo() and catching NotFound. Opted not to reuse ReponseTaggers here as they don't fully fit, and as we want to rewrite this header independently of if view cache is enabled or not.
With this bundle, REST HttpCache is not cleared when moving content, editing content types, (...) And given the time it will take to fix REST server to return data that can be used for ResponseTaggers and how this also affects custum responses given, as we now only purge by specific tags. Adding this is thus safests. This somewhat brings back what @Plopix suggested in #21, essentially making sure we add all tags also to X-Location responses by loading location, but to be on the safe side it does it using sudo() and catching NotFound. Opted not to reuse ReponseTaggers here as they don't fully fit, and as we want to rewrite this header independently of if view cache is enabled or not.
Attempts to fix #20