proxy-cache
Caches upstream responses on disk or in memory.
proxy-cachePriority 1085What it does
proxy-cache keeps a copy of the answers a route's backend gives and serves the next matching request from that copy, without asking the backend. Its Cache strategy chooses where the copies live, on disk or in memory, and Cache key decides which requests share one.
The first request for a key reaches the backend, and its answer carries the cache status MISS. The same request again is answered from the cache with HIT, for as long as the copy lives.
When to use it
- A list of products that every client reads and that changes rarely, served from memory for a minute instead of from the backend on every request.
- A slow report that may be a few minutes old, computed once and then served from the cache.
- A burst of identical reads, for example after a mobile app release, that the backend should not answer one by one.
- Answers that differ by caller, kept apart per consumer with Consumer isolation.
Guides
Pages that walk through a job with proxy-cache, step by step.
- Serve answers from a cacheKeep a route's answers in the gateway's memory for a minute and serve repeated requests without asking the backend.
Set it up
Keep successful responses in memory for one minute
Under Basics set Cache strategy to memory, set Cache TTL to 60 seconds, set Cache zone to memory_cache and fill Cache key as in the JSON below. Under Behavior fill Cached response statuses as in the JSON below.
{
"cache_strategy": "memory",
"cache_zone": "memory_cache",
"cache_ttl": 60,
"cache_key": [
"$host",
"$request_uri"
],
"cache_http_status": [
200
]
}
What you will see
- The first request is answered
200by the backend, and the answer's cache status header readsMISS; the gateway keeps the answer. - The second request within the minute is answered
200by the gateway from its memory, and the cache status header readsHIT; the backend is not called.
Things to know
- Cache key — With the key
$hostand$request_uri, a second request to the same path is answered from memory. The first answer carriesMISS, the secondHIT. - Cache key — Each part is a $variable or text of two characters or more. $request_method is refused, and a variable the request lacks adds nothing to the key.
- Cache strategy — Under Disk the lifetime of a stored response is set by the gateway's own cache configuration and not by this object, which is why the lifetime field is drawn under Memory alone.
- Cache zone — Any other name, or a zone of the other strategy, is refused on save.
- Cache zone — Under Cache strategy
disk, the zonememory_cacheis refused when you save. - Cache control — On, a response is cached only as long as its own Cache-Control or Expires allows, and one carrying neither is never stored. The lifetime box above is not read. A request that asks for no-store goes to the upstream.
- Cached request methods — A method other than
GET,HEADandPOST, for examplePUT, is refused when you save. - Cache bypass — Each part is a $variable or text of two characters or more, so a lone 0 or 1 is refused.
- No cache — Each part is a $variable or text of two characters or more, so a lone 0 or 1 is refused.
- Consumer isolation — On, each consumer gets its own entries; off, an entry cached for one consumer is served to the others. Not read when the cache key carries $consumer_name, $consumer_group_id, $remote_user or $http_authorization: a name empty for all leaves them one cache.
- AdviceCache only answers that every caller of the route may see. For an answer that depends on who asks, keep Consumer isolation on or put the caller in the key.
Settings
The fields as the console shows them, section by section. A field under “When … is …” is there only while that value is set.
Basics
cache_strategystringdefault diskWhen Cache strategy is memory
cache_ttlintegerdefault 300cache_controlbooleandefault falsecache_set_cookiebooleandefault falseOff, a response that carries a Set-Cookie header is never stored, wherever that header came from.
max_resp_body_sizeintegerdefault 67108864Responses of this many bytes or more are passed through and not stored.
cache_zonestringdefault disk_cache_oneA zone the gateway declares for this strategy: disk_cache_one for Disk and memory_cache for Memory, as shipped.
cache_keyarray of stringsdefault ["$host","$request_uri"]Behavior
cache_methodarray of stringsdefault ["GET","HEAD"]cache_http_statusarray of integersdefault [200,301,404]cache_bypassarray of stringsSkip the cache when these parts join into anything but empty or 0, such as $arg_nocache.
no_cachearray of stringsDo not store the response when these parts join into anything but empty or 0, such as $arg_nostore.
hide_cache_headersbooleandefault falseOn, the response reaches the client without the Cache-Control and Expires headers it was stored with.
consumer_isolationbooleandefault true