cache_by_headers
propertyX-Tyk-Cached-Response
is added to the response header with the value 1
GET
, HEAD
or OPTIONS
that do not modify data and can be safely cached for performance gains (i.e. they should be idempotent and so are good candidates for caching). If these methods are not idempotent for your API, then you should not use safe request caching.
Safe request caching at the API level is enabled by setting the cache_all_safe_requests
option to true
, or by checking the equivalent checkbox in the Dashboard UI. This will enable safe request caching on all endpoints for an API.
This mode of operation is referred to as Global Caching because it is applied globally within the scope of a single API. Picking this approach will override any per-endpoint (per-path) caching configuration, so it’s not suitable if granular control is required.
Tyk does support safe request caching at the more granular, per-endpoint level, as described here - but cache_all_safe_requests
must be set to false
in that scenario.
cache_options
section.
The main configuration options are:
enable_cache
: Set to true
to enable caching for the APIcache_timeout
: Number of seconds to cache a response for, after which the next new response will be cachedcache_response_codes
: The HTTP status codes a response must have in order to be cachedcache_all_safe_requests
: Set to true
to apply the caching rules to all requests using GET
, HEAD
and OPTIONS
HTTP methodscache_by_headers
: used to create multiple cache entries based on the value of a header value of your choiceenable_upstream_cache
: used to allow your upstream service to identify the responses to be cachedcache_control_ttl_headers
: used with enable_upstream_cache
cache_all_safe_requests
to true, then the cache will be global and all inbound requests made to the API will be evaluated by the caching middleware. This is great for simple APIs, but for most, a finer-grained control is required. This control will over-ride any per-endpoint cache configuration.cache_option.cache_by_headers
option with a list of the headers to be cached.
For example, to cache each value in the custom Unique-User-Id
header of your API response separately you would set:
cache_by_headers
configuration is not currently exposed in the Dashboard UI, so it must be enabled though either the raw API editor or the Dashboard API.operationId
defined in the OpenAPI Document that declares both the path and method for which the middleware should be added. The path
can contain wildcards in the form of any string bracketed by curly braces, for example {user_id}
. These wildcards are so they are human readable and do not translate to variable names. Under the hood, a wildcard translates to the “match everything” regex of: (.*)
.
Configuring the endpoint cache is performed in two parts:
cache
object to the global
section of the Tyk OAS Extension (x-tyk-api-gateway
) in your Tyk OAS API.
This object has the following configuration:
enabled
: enable the cache for the APItimeout
: set as the default cache refresh period for any endpoints for which you don’t want to configure individual timeouts (in seconds)cache
) should then be added to the operations
section of x-tyk-api-gateway
for the appropriate operationId
(as configured in the paths
section of your OpenAPI Document).
The cache
object has the following configuration:
enabled
: enable the middleware for the endpointtimeout
: set to the refresh period for the cache (in seconds)cacheResponseCodes
: HTTP responses codes to be cached (for example 200
)cacheByRegex
: Pattern match for selective caching by body valueHTTP 200
responses to requests to the POST /delay/5
endpoint. The cache will refresh after 5 seconds. Note that requests to other endpoints will also be cached, with a default cache timeout of 60 seconds according to the configuration in lines 37-40.
The configuration above is a complete and valid Tyk OAS API Definition that you can import into Tyk to try out the endpoint caching.
x
next to it.
cache
object to the extended_paths
section of your API definition. The cache
object is a list of endpoints for which you wish to cache all safe requests.
In the API-level cache_options
you must enable caching and configure the timeout whilst ensuring that the option to cache all safe requests is disabled.
The cache_options
object has the following configuration:
enable_cache
: set to true
to enable caching for this APIcache_all_safe_requests
: set to false
to allow selective caching per-endpointcache_timeout
: set to the refresh period for the cache (in seconds)/widget
and /fish
) with a cache refresh period of 60 seconds.
advance_cache_config
object to the extended_paths
section of your API definition.
In the API-level cache_options
you must enable caching and ensure that the option to cache all safe requests is disabled. The timeout that you set here will be used as a default for any endpoints for which you don’t want to configure individual timeouts.
The advance_cache_config
object has the following configuration:
path
: the endpoint pathmethod
: the endpoint methodtimeout
: set to the refresh period for the cache (in seconds)cache_response_codes
: HTTP response codes to be cached (for example 200
)cache_key_regex
: pattern match for selective caching by body value/widget
and /fish
) as follows:
endpoint | HTTP response codes to cache | cache refresh timeout | body value regex |
---|---|---|---|
POST /widget | 200 | 10 seconds | none |
GET /fish | 200, 300 | 60 seconds (taken from cache_options ) | shark |
cache
list in the extended_paths
section.
In the API-level cache_options
object, you must enable caching by setting enable_cache
to true and configure the cache refresh period by setting a value for the cache_timeout
in seconds. To allow selective caching per endpoint you should also set cache_all_safe_requests
to false
.
advance_cache_config
object to the extended_paths
section of your API definition.
This allows you to configure caching per endpoint. For each endpoint, it is possible to specify the endpoint path, method, list of response codes to cache, cache timeout and a cache key regular expression. The cache key regular expression represents a pattern match to cache only requests containing specific data in the request body
For example:
/anything
endpoint as follows:
endpoint | HTTP response codes to cache | cache refresh timeout | body value regex |
---|---|---|---|
GET /anything | 200 | 30 seconds (taken from cache_options ) | none |
OPTIONS
method, you can configure the upstream cache control accordingly and return cache control headers only in those responses. With this configuration, Tyk will cache only those responses, not those for other methods for the same path.
Upstream cache control is configured on a per-API and per-endpoint basis, giving maximum flexibility. All configuration is performed within the API definition.
cache_options
in the API definition as follows:
enable_cache
)cache_all_safe_requests
is set to false
)enable_upstream_cache_control
to true
extended_paths.cache
/ip
endpoint (path) of your API you would add the following to the API definition:
x-tyk-cache-action-set
:
1
or true
then the response will be stored in the cachecache_response_codes
)x-tyk-cache-action-set-ttl
in the response:
cache_options.cache_timeout
cache_control_ttl_header
option to the API definition.
For example, if you configure:
x-expire: 30
in the response header, Tyk will cache that specific response for 30 seconds.
tyk.conf
configuration file.
You must
enable_separate_cache_store
to true
cache_storage
sectionenable_cluster
to false
, you only need to set one entry in addrs
.
hosts
was used instead of addrs
; since v2.9.3 hosts
has been deprecated.