You can define a HTTP response cache that can be applied to Spring Cloud Gateway for Kubernetes at route-level or globally. There is currently one implementation that uses in-memory caches: Caffeine project.

According to HTTP specifications, you must consider the following when activating this feature:

  • It only caches responses when the request is bodiless and has the HTTP method GET.
  • It only caches responses with the following status codes: HTTP 200 (OK), HTTP 206 (Partial Content) and HTTP 301 (Moved Permanently).
  • Response data will not be cached if the Cache-Control header does not allow it (no-store in the request, no-store or private in the response).
  • If the response is already cached and a new request is performed with the no-cache value in the Cache-Control header, it will return a bodiless response with 304 (Not Modified).

The parameter local.timeToLive sets the default time to expire a cache entry (expressed in s for seconds, m for minutes, and h for hours) and local.size sets the default maximum size of the cache to evict entries for this route (in KB, MB and GB).

Activating the local cache without specifying any other configuration will activate the local response cache implementation with a time to live for cached items of 5 minutes.

apiVersion: "tanzu.vmware.com/v1"
kind: SpringCloudGateway
metadata:
  name: my-gateway
spec:
   responseCache:
      local:
         size: 8MB
         timeToLive: 10m

The local response cache implementation automatically calculates and sets the max-age value in the HTTP Cache-Control header. Only if max-age is present on the original response will the value be rewritten with the number of seconds set in the timeToLive configuration parameter. In consecutive calls this value will be recalculated and set to the number of seconds left before the cached entry is evicted.

In order to avoid repeating the LocalResponseCache filter in every route, the flag local.global can be set to true in the SpringCloudGateway definition. Using the following definition, all the routes will cache with the described default configuration.

apiVersion: "tanzu.vmware.com/v1"
kind: SpringCloudGateway
metadata:
   name: my-gateway
spec:
   responseCache:
      local:
         global: true
         size: 8MB
         timeToLive: 10m
check-circle-line exclamation-circle-line close-line
Scroll to top icon