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

Question on caching with querystring #71

Closed
ghost opened this issue Aug 31, 2020 · 8 comments
Closed

Question on caching with querystring #71

ghost opened this issue Aug 31, 2020 · 8 comments

Comments

@ghost
Copy link

ghost commented Aug 31, 2020

Hi, I have a couple of questions on how caching works with querystrings, and perhaps the README.md can be updated to clarify this:

Suppose we're working with the following endpoint /test and it can take 2 optional querystring parameters: param1 and param2

  1. When I specify request.path.querystring.param1 will it cache as just /test?
  2. If I use multivaluequerystring, will it cache all these 3 possibilities: /test?param1=test, /test?param2=test, /test?param1=test&param2=test?
  3. Is there a way to specify a catch all for querystring params, instead of explicitly specifying the params in multivaluequerystring? So I would want to cache on the url + querystringparams whatever the querystring parameters may be

Thanks

@DianaIonita
Copy link
Owner

DianaIonita commented Sep 1, 2020

Hi @stoverlee,

When I specify request.path.querystring.param1 will it cache as just /test?

If you have settings like:

- http:
   path: /test
   method: get
   caching:
     enabled: true
     cacheKeyParameters:
       - name: request.querystring.param1

Then it will create a cache entry for all the different values it may receive of param1, i.e. one entry for /test?param1=A, another entry for /test?param1=B.
Yes, it includes the case when it's undefined, meaning when it's just /test.

If I use multivaluequerystring, will it cache all these 3 possibilities: /test?param1=test, /test?param2=test, /test?param1=test&param2=test?

You're right that the README needs to be updated. I need to add some of what's here - and also that multivaluequerystring is not supported. The plugin currently only supports cache key parameters coming from header, path and querystring. If you know of a way to define cache key parameters in the API Gateway console for multivaluequerystring, then there might also be a way to do it in code, but so far I've not been able to find out whether it's possible.

In your example, your parameters would be part of the querystring, so you'd only need to do this for it to create cache entries for all possiblities of param1 and param2:

- http:
   path: /test
   method: get
   caching:
     enabled: true
     cacheKeyParameters:
       - name: request.querystring.param1
       - name: request.querystring.param2

Multi-value query strings would look something like: /test?param=A&param=B, so param would be multi-valued: an array of A and B. The plugin doesn't currently support this, as I was mentioning above.

Is there a way to specify a catch all for querystring params, instead of explicitly specifying the params in multivaluequerystring? So I would want to cache on the url + querystringparams whatever the querystring parameters may be

I can only think of a way to have a catch-all for paths:

- http:
   path: /test/{proxy+}
   method: get
   caching:
     enabled: true
     cacheKeyParameters:
       - name: request.querystring.proxy

This would allow you to cache any paths to the right of /test/, so paths like:

  • /test/param1value/param2value
  • /test/some/path/here

In lambda code, you would have access to the path variable through the request.pathParameters.proxy and it would have the values param1value/param2value and some/path/here respectively, so you'd need to parse them.

However, this doesn't seem to work for query string parameters, so for the paths:

  • /test/some/path/here
    and
  • /test/some/path/here?query=123

the same cache entry would be created. request.pathParameters.proxy will always be some/path/here, regardless of query string. For a user, that would mean that no matter what they changed the query string to, they'd always get the page at /test/some/path/here.

Please let me know if it doesn't make sense.

Edit: I just noticed this PR on multivaluequerystring, I'll have a look at whether it's possible to add support.

Later edit: The PR was good, but didn't add support for multivaluequerystring.

@pavlelekic
Copy link

pavlelekic commented Oct 3, 2022

@DianaIonita Hello,
I also have a question on caching, is there a way to invalidate a certain route? Lets say api.domain.com/v2/users/47 route was cached, and I want to invalidate the cache only for that specific user 47 but not for all users. Is there a way to do that once the route was cached for that specific path parameter value? Thanks.

I imagine there is some kind of an endpoint like /invalidations where you can send the path and cache key parameters for which you want an invalidation to happen.

@DianaIonita
Copy link
Owner

Hi @pavlelekic,

Yes, you can invalidate specific cache keys by setting the header Cache-Control: max-age=0 in your request to api.domain.com/v2/users/47.

You might also need to configure per-key cache invalidation authorization with these plugin settings:

custom:
  apiGatewayCaching:
    enabled: true
    perKeyInvalidation:
      requireAuthorization: true # default is true
      handleUnauthorizedRequests: Fail # default is "IgnoreWithWarning".

@pavlelekic
Copy link

Thanks @DianaIonita!
Is there a way to flush the whole cache? Let’s say you deploy a new version of you application, and you want to flush the old cache (for all routes)?

@DianaIonita
Copy link
Owner

Hi @pavlelekic,

You can do that, for example, via the API Gateway console, in the Stage editor:

image

@pavlelekic
Copy link

Thanks!

@vvmspace
Copy link

vvmspace commented Sep 7, 2023

How to cache full query string?
I have a complex search and I want to cache it with a full querystring as a key

@DianaIonita
Copy link
Owner

Hi @vvmspace,
Apologies for the delay. You would use the query string as is as a cache key parameter, for example:

- http:
   path: /search
   method: get
   caching:
     enabled: true
     cacheKeyParameters:
       - name: request.querystring.searchParams

Please consider raising another issue if you'd like to have further discussions around this topic.

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

No branches or pull requests

3 participants