graphql-proxy-cache
Caches GraphQL responses.
graphql-proxy-cachePriority 1009What it does
graphql-proxy-cache keeps a copy of the answers a route's GraphQL backend gives. The next identical query is served from that copy, without asking the backend. Cache strategy chooses where the copies live, on disk or in memory, and Cache zone names the zone that holds them.
The first POST of a query reaches the backend, and its answer carries the cache status MISS. The same query again is answered from the cache with HIT, for as long as the copy lives.
When to use it
- A GraphQL orders API whose catalogue queries every client sends and that changes rarely, served from memory for a minute.
- A burst of identical queries, for example after a mobile app release, that the backend should not answer one by one.
- A slow query whose answer may be a few minutes old, computed once and then served from the cache.
- Answers that differ by caller, kept apart per consumer with Consumer isolation.
Not for caching REST answers by path and query string — see proxy-cache (Caches upstream responses on disk or in memory).
Set it up
Keep GraphQL responses in memory for one minute
Set Cache strategy to memory, set Cache TTL to 60 seconds and set Cache zone to memory_cache.
{
"cache_strategy": "memory",
"cache_zone": "memory_cache",
"cache_ttl": 60
}
Things to know
- Cache strategy — With
memory, the zonememory_cacheand a lifetime of 60 seconds, a second POST of{ orders { id } }is answered from memory. The first answer carriesMISS, the secondHIT. - Cache strategy — Only a GET or a POST carrying a GraphQL query is cached, and only a
200answer. A mutation reaches the backend every time, and other methods are refused with405. - 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 — Memory over a disk zone never caches, and Disk over a memory zone answers 500, so pick the zone that matches the strategy.
- Cache zone — A zone the gateway configuration does not declare is refused when you save.
- Cache zone — An empty zone name is refused when you save.
- Cache TTL — A lifetime of
0is refused when you save. - Consumer isolation — Off, the answer cached for the first caller is served to every other caller.
- AdviceCache only answers that every caller of the route may see. For an answer that depends on who asks, keep Consumer isolation on.
Settings
The fields as the console shows them, section by section. A field under “When … is …” is there only while that value is set.
cache_strategystringdefault diskWhere answers are kept: on disk, or in memory with a lifetime of your own. Memory also needs the memory_cache zone below. Only a GET or a POST carrying a GraphQL query is cached, and only a 200 answer. A mutation is fetched again every time, and other methods are refused with 405.
When Cache strategy is memory
cache_ttlintegerdefault 300How long a stored answer is served before the upstream is asked for it again.
cache_set_cookiebooleandefault falseOff, an answer that carries a Set-Cookie header is never stored, wherever that header came from.
cache_zonestringdefault disk_cache_oneThe cache zone: disk_cache_one for Disk, memory_cache for Memory. A name the gateway configuration does not declare is refused.
consumer_isolationbooleandefault trueOn, each signed-in consumer gets only answers cached for them. Anonymous callers all share one entry.