Skip to main content

graphql-proxy-cache

The graphql-proxy-cache plugin provides the capability to cache responses for GraphQL queries. It uses MD5 algorithm to generate cache key based on the plugin configurations and GraphQL queries. The plugin supports both disk-based and memory-based caching options to cache for GET and POST GraphQL requests.

If a request contains a mutation operation, the plugin will not cache the data. Instead, it adds an Apisix-Cache-Status: BYPASS header to the response to show that the request bypasses the caching mechanism.

Examples

The examples below use GitHub GraphQL API as an upstream and demonstrate how you can configure graphql-proxy-cache for different scenarios.

To follow along, create a GitHub personal access token with the appropriate scopes for the resources you want to interact with.

Cache Data on Disk

On-disk caching strategy offers the advantages of data persistency when system restarts and having larger storage capacity compared to in-memory cache. It is suitable for applications that prioritize durability and can tolerate slightly larger cache access latency.

The following example demonstrates how you can use graphql-proxy-cache plugin on a route to cache data on disk.

Create a route with the graphql-proxy-cache plugin with the default configuration to cache data on disk:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "graphql-proxy-cache-route",
"uri": "/graphql",
"plugins": {
"graphql-proxy-cache": {}
},
"upstream": {
"type": "roundrobin",
"pass_host": "node",
"scheme": "https",
"nodes": {
"api.github.com:443": 1
}
}
}'

Send a request with a GraphQL query to verify:

curl -i "http://127.0.0.1:9080/graphql" -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${GH_ACCESS_TOKEN}" \
-d '{"query": "query {viewer{login}}"}'

You should see an HTTP/1.1 200 OK response with the following headers, showing the plugin is successfully enabled:

APISIX-Cache-Key: e9c1624ee35f792548512ff9f6ff1bfa
Apisix-Cache-Status: MISS

As there is no cache available before the first response, Apisix-Cache-Status: MISS is shown.

Send the same request again within the cache TTL window. You should see an HTTP/1.1 200 OK response with the following headers, showing the cache is hit:

APISIX-Cache-Key: e9c1624ee35f792548512ff9f6ff1bfa
Apisix-Cache-Status: HIT

Wait for the cache to expire after the TTL and send the same request again. You should see an HTTP/1.1 200 OK response with the following headers, showing the cache has expired:

APISIX-Cache-Key: e9c1624ee35f792548512ff9f6ff1bfa
Apisix-Cache-Status: EXPIRED

Cache Data in Memory

In-memory caching strategy offers the advantage of low-latency access to the cached data, as retrieving data from RAM is faster than retrieving data from disk storage. It also works well for storing temporary data that does not need to be persisted long-term, allowing for efficient caching of frequently changing data.

The following example demonstrates how you can use graphql-proxy-cache plugin on a route to cache data in memory.

Create a route with graphql-proxy-cache enabled and configure it to use memory-based caching:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "graphql-proxy-cache-route",
"uri": "/graphql",
"plugins": {
"graphql-proxy-cache": {
"cache_strategy": "memory",
"cache_zone": "memory_cache",
"cache_ttl": 10
}
},
"upstream": {
"type": "roundrobin",
"pass_host": "node",
"scheme": "https",
"nodes": {
"api.github.com:443": 1
}
}
}'

cache_strategy: set to memory for in-memory setting.

cache_zone: set to the name of an in-memory cache zone.

cache_ttl: set the time to live for the in-memory cache.

Send a request with a GraphQL query to verify:

curl "http://127.0.0.1:9080/graphql" -i -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${GH_ACCESS_TOKEN}" \
-d '{"query": "query {viewer{login}}"}'

You should see an HTTP/1.1 200 OK response with the following headers, showing the plugin is successfully enabled:

APISIX-Cache-Key: a661316c4b1b70ae2db5347743dec6b6
Apisix-Cache-Status: MISS

As there is no cache available before the first response, Apisix-Cache-Status: MISS is shown.

Send the same request again within the cache TTL window. You should see an HTTP/1.1 200 OK response with the following headers, showing the cache is hit:

APISIX-Cache-Key: a661316c4b1b70ae2db5347743dec6b6
Apisix-Cache-Status: HIT

Remove Cache Manually

While most of the time it is not necessary, there may be situations where you would want to manually remove cached data.

The following example demonstrates how you can use the public-api plugin to expose the /apisix/plugin/graphql-proxy-cache/{cache_strategy}/{route_id}/{key} endpoint created by the graphql-proxy-cache plugin to manually remove cache.

Create a route that matches the URI /apisix/plugin/graphql-proxy-cache/*:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "graphql-cache-purge",
"uri": "/apisix/plugin/graphql-proxy-cache/*",
"plugins": {
"public-api": {}
}
}'

Send a PURGE request to remove data cached on disk for route 1:

curl -i "http://127.0.0.1:9080/apisix/plugin/graphql-proxy-cache/disk/1/e9c1624ee35f792548512ff9f6ff1bfa" -X PURGE

An HTTP/1.1 200 OK response verifies that the cache corresponding to the key is successfully removed.

If you send the same request again, you should see an HTTP/1.1 404 Not Found response, showing there is no cache on disk with this cache key after the cache removal.


API7.ai Logo

API Management for Modern Architectures with Edge, API Gateway, Kubernetes, and Service Mesh.

Product

API7 Cloud

SOC2 Type IRed Herring

Copyright © APISEVEN Ltd. 2019 – 2024. Apache, Apache APISIX, APISIX, and associated open source project names are trademarks of the

Apache Software Foundation