DianaIonita / serverless-api-gateway-caching

A plugin for the Serverless framework which helps with configuring caching for API Gateway endpoints.
ISC License
136 stars 35 forks source link

Question on caching with querystring #71

Closed ghost closed 3 years ago

ghost commented 3 years ago

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 commented 3 years ago

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:

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:

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 commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Hi @pavlelekic,

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

image

pavlelekic commented 1 year ago

Thanks!

vvmspace commented 10 months ago

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 commented 8 months ago

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.