The Proxy Cache plugin stores cache data in memory, which is a shared dictionary defined in config.memory.dictionary_name
.
The default dictionary, kong_db_cache
, is also used by other plugins and functions of Kong Gateway to store unrelated database cache entities.
Using the kong_db_cache
dictionary is an easy way to bootstrap and test the plugin, but we don’t recommend using it for large-scale installations as significant usage will put pressure on other facets of Kong Gateway’s database caching operations.
In production, we recommend defining a custom lua_shared_dict
via a custom Nginx template.
Cache entities are stored for a configurable period of time, after which subsequent requests to the same resource will fetch and store the resource again.
In Traditional mode, cache entities can also be forcefully purged via the Admin API prior to their expiration time.
Kong Gateway keys each cache element based on:
- The request method
- The full client request (for example, the request path and query parameters)
- The UUID of either the API or Consumer associated with the request
Caches are distinct between APIs and Consumers.
Internally, cache keys are generated by computing the SHA256 hash of the combined parts, then encoding the result in hexadecimal:
key = sha256(UUID | method | request | query_params | headers | consumer_groups)
Where:
-
method
is defined in the OpenResty ngx.req.get_method()
call
-
request
is defined via the Nginx $request
variable
-
query_params
are defined via the plugin’s config.vary_query_params
parameter
-
headers
are defined in the plugin’s config.vary_headers
parameter
-
consumer_groups
are defined based on the Consumer Groups this plugin is applied to
Kong Gateway will return the cache key associated with a given request as the X-Cache-Key
response header.
Note: The cache key format is hardcoded and can’t be modified.
When the config.cache_control
configuration option is enabled,
Kong Gateway respects request and response Cache-Control
headers as
defined by RFC7234, with the following exceptions:
- Cache revalidation is not supported, so directives such as
proxy-revalidate
are ignored
- The behavior of
no-cache
is simplified to exclude the entity from being cached entirely
Kong Gateway identifies the status of a request’s proxy cache behavior via the X-Cache-Status
header.
There are several possible values for this header:
-
Miss
: The request could be satisfied in cache, but an entry for the resource was not found in cache, and the request was proxied upstream.
-
Hit
: The request was satisfied and served from cache.
-
Refresh
: The resource was found in cache, but couldn’t satisfy the request, due to Cache-Control
behaviors or from reaching its hardcoded config.cache_ttl
threshold.
-
Bypass
: The request couldn’t be satisfied from cache based on plugin configuration.