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

[WIP] Convert X-Location-Id into tags for non-view responses #21

Closed
wants to merge 1 commit into from
Closed

[WIP] Convert X-Location-Id into tags for non-view responses #21

wants to merge 1 commit into from

Conversation

Plopix
Copy link
Contributor

@Plopix Plopix commented Nov 4, 2017

issue: https://jira.ez.no/browse/EZP-28183

Attempts to fix #20

@@ -40,7 +40,7 @@ public static function getSubscribedEvents()
public function configureCache(FilterResponseEvent $event)
{
$view = $event->getRequest()->attributes->get('view');
if (!$view instanceof CachableView || !$view->isCacheEnabled()) {
if ($view !== null && ( !$view instanceof CachableView || !$view->isCacheEnabled() )) {
Copy link
Contributor

@andrerom andrerom Nov 5, 2017

Choose a reason for hiding this comment

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

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.

Copy link
Contributor Author

@Plopix Plopix Nov 5, 2017

Choose a reason for hiding this comment

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

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;
Copy link
Contributor

@andrerom andrerom Nov 5, 2017

Choose a reason for hiding this comment

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

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

@andrerom andrerom mentioned this pull request Nov 5, 2017
@Plopix
Copy link
Contributor Author

Plopix commented Nov 5, 2017

ping @andrerom @bdunogier @vidarl

I have reworked it @andrerom and I have used your name @vidarl
@andrerom I have kept the dependency on the LocationTaggger to get the parent location for free.
I did not want to remove X-Location-Id because other taggers (in the actual pattern) could use it but I suppose it is fine and only this one will play with X-Location-id...

Copy link
Member

@bdunogier bdunogier left a comment

Choose a reason for hiding this comment

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

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
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a summary of what the Tagger does, and in what context it is (not) useful ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

const LOCATION_ID_HEADER = 'X-Location-Id';

/**
* XLocationIdTagger constructor.
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: I'd remove that block altogether. Including the @param.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok done!

foreach ($locationIds as $locationId) {
$location = $this->repository->getLocationService()->loadLocation($locationId);
$this->locationTagger->tag($configurator, $response, $location);
$response->headers->remove('X-Location-Id');
Copy link
Member

Choose a reason for hiding this comment

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

You should replace 'X-Location-ID' with self::LOCATION_ID_HEADER.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch! Fixed.

$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) {
Copy link
Contributor

@andrerom andrerom Nov 6, 2017

Choose a reason for hiding this comment

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

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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);
Copy link
Member

@bdunogier bdunogier Nov 6, 2017

Choose a reason for hiding this comment

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

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.

@Plopix
Copy link
Contributor Author

Plopix commented Nov 6, 2017

@andrerom @bdunogier @vidarl I have changed stuff.
Also, the code was breaking as repository was not injected anymore and we need it to load the location... not sure you want that. let me know.

@Plopix
Copy link
Contributor Author

Plopix commented Nov 6, 2017 via email

@Plopix
Copy link
Contributor Author

Plopix commented Nov 8, 2017

I suppose this PR is not useful anymore right? if so, you can close.

@andrerom
Copy link
Contributor

andrerom commented Nov 8, 2017

Thanks for the pointers here @Plopix, we'll handle this in #25

@andrerom andrerom closed this Nov 8, 2017
andrerom added a commit that referenced this pull request Nov 27, 2017
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.
andrerom added a commit that referenced this pull request Nov 27, 2017
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.
andrerom added a commit that referenced this pull request Nov 27, 2017
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

[Api][Admin UI] Responses are not taggued - Admin UI is not purged
3 participants