配置代理缓存
proxy-cache插件可以在网关中缓存上游响应,显著缩短频繁请求资源的响应时间,并降低后端服务负载。
前置条件
- API7 企业版实例正在运行。
- 已创建网关组且网关实例正在运行。
- 一个 Dashboard Token。
- 如需使用自定义缓存区域,请在网关的 Nginx 配置中进行配置,例如
disk_cache_one或memory_cache。
配置基于磁盘的代理缓存
插件默认使用基于磁盘的缓存,网关重启后仍会保留。
- Admin API
- ADC
curl -k "https://localhost:7443/apisix/admin/services/proxy-cache-service?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "proxy-cache-service",
"upstream": {
"type": "roundrobin",
"scheme": "http",
"nodes": [{"host": "httpbin.org", "port": 80, "weight": 100}]
}
}'
curl -k "https://localhost:7443/apisix/admin/routes/proxy-cache-route?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "proxy-cache-route",
"methods": ["GET"],
"paths": ["/anything/cache"],
"service_id": "proxy-cache-service",
"plugins": {"proxy-cache": {"cache_strategy": "disk", "hide_cache_headers": false}}
}'
adc.yaml
services:
- name: proxy-cache-service
upstream:
nodes:
- host: httpbin.org
port: 80
weight: 1
routes:
- name: proxy-cache-route
uris:
- /anything/cache
methods:
- GET
plugins:
proxy-cache:
cache_strategy: disk
hide_cache_headers: false
adc sync -f adc.yaml