Policies

Caching Policy

Respond to matched incoming requests with cached content

Configuration

The configuration shows how to configure the policy in the 'policies.json' document.

{ "name": "my-caching-inbound-policy", "policyType": "caching-inbound", "handler": { "export": "CachingInboundPolicy", "module": "$import(@zuplo/runtime)", "options": { "cacheHttpMethods": [ "GET" ], "expirationSecondsTtl": 60, "headers": "content-type", "statusCodes": [ 200, 201, 404 ] } } }

Policy Options

The options for this policy are specified below. All properties are optional unless specifically marked as required.

  • cacheId <string> -
    Specifies an id or 'key' for this policy to store cache. This is useful for cache-busting. For example, set this property to an env var and if you change that env var value, you invalidate the cache.
  • dangerouslyIgnoreAuthorizationHeader <boolean> -
    By default, the Authorization header is always considered in the caching policy. You can disable by setting this to true.
    Defaults to false.
  • headers <string[]> -
    The headers to be considered when caching.
    Defaults to [].
  • cacheHttpMethods <string[]> -
    HTTP Methods to be cached. Valid methods are: GET, POST, PUT, PATCH, DELETE, HEAD.
    Defaults to ["GET"].
  • expirationSecondsTtl <number> -
    The timeout of the cache in seconds.
    Defaults to 60.
  • statusCodes <number[]> -
    Response status codes to be cached.
    Defaults to [[200,206,301,302,303,404,410]].

Using the Policy

Cache-busting#

If you need to support cache-busting on demand, we recommend applying a cacheId property based on an Environment Variable. Ensure all your cache policies are using a cachedId based on a variable and then change that variable (and trigger a redeploy) to clear the cache.

e.g.

{ "export": "CachingInboundPolicy", "module": "$import(@zuplo/runtime)", "options": { "cachedId": "$env(CACHE_ID)", // this is reading an env var "expirationSecondsTtl": 60, "dangerouslyIgnoreAuthorizationHeader": false, "headers": ["header_used_as_part_of_cache_key"] } }

Then you would setup an env var for this, we recommend using the current date it was set, e.g. 2023-07-05-11-57 and then simply change this value and trigger a redeploy to bust your cache.

Env Var

Read more about how policies work

Previous
Brown Out