DianaIonita / serverless-api-gateway-caching

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

Question about caching #97

Closed th3fl4w3d closed 2 years ago

th3fl4w3d commented 3 years ago

When caching is enabled (using query string key parameter), and they visit a URL without the query string cache parameter, the request is still cached. Is there a way to not cache the response unless query string parameter is present?

Desired output

cacheKeyParameters:
  - name: request.querystring.cache

This should not cache or return a cached response api.domain.com/v2/users

This should cache and return a cached response api.domain.com/v2/users?cache=1

DianaIonita commented 3 years ago

Hi @th3fl4w3d,

Thanks for asking. There isn't a way that I know of to tell API Gateway to only cache on certain cache key parameters. However, you can send a Cache-Control header to instruct API Gateway to clear the cache for a specific entry:

For example,

GET api.domain.com/v2/users with header: Cache-Control: max-age=0

will clear the cache for that specific cache key (in this case, when the cache key is undefined), and return a newly generated response. It would not affect other cache entries.

This plugin also offers a way to configure per-key cache invalidation authorization, which you may need to tweak based on your requirements:

plugins:
  - serverless-api-gateway-caching

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

Hope this helps.