proxy-cache

描述

proxy-cache 插件提供缓存后端响应数据的能力,它可以和其他插件一起使用。该插件支持基于磁盘和内存的缓存。目前可以根据响应码和请求模式来指定需要缓存的数据,也可以通过 no_cachecache_bypass属性配置更复杂的缓存策略。

属性

名称类型必选项默认值有效值描述
cache_strategystringdisk[“disk”,”memory”]缓存策略,指定缓存数据存储在磁盘还是内存中。
cache_zonestringdisk_cache_one指定使用哪个缓存区域,不同的缓存区域可以配置不同的路径,在 conf/config.yaml 文件中可以预定义使用的缓存区域。如果指定的缓存区域与配置文件中预定义的缓存区域不一致,那么缓存无效。
cache_keyarray[string][“$host”, “$request_uri”]缓存 key,可以使用变量。例如:[“$host”, “$uri”, “-cache-id”]
cache_bypassarray[string]当该属性的值不为空或者非 0 时则会跳过缓存检查,即不在缓存中查找数据,可以使用变量,例如:[“$arg_bypass”]
cache_methodarray[string][“GET”, “HEAD”][“GET”, “POST”, “HEAD”]根据请求 method 决定是否需要缓存。
cache_http_statusarray[integer][200, 301, 404][200, 599]根据 HTTP 响应码决定是否需要缓存。
hide_cache_headersbooleanfalse当设置为 true 时将 ExpiresCache-Control 响应头返回给客户端。
cache_controlbooleanfalse当设置为 true 时遵守 HTTP 协议规范中的 Cache-Control 的行为。
no_cachearray[string]当此参数的值不为空或非 0 时将不会缓存数据,可以使用变量。
cache_ttlinteger300 秒当选项 cache_control 未开启或开启以后服务端没有返回缓存控制头时,提供的默认缓存时间。
proxy-cache - 图1注意
  • 对于基于磁盘的缓存,不能动态配置缓存的过期时间,只能通过后端服务响应头 ExpiresCache-Control 来设置过期时间,当后端响应头中没有 ExpiresCache-Control 时,默认缓存时间为 10 秒钟
  • 当上游服务不可用时, APISIX 将返回 502504 HTTP 状态码,默认缓存时间为 10 秒钟;
  • 变量以 $ 开头,不存在时等价于空字符串。也可以使用变量和字符串的结合,但是需要以数组的形式分开写,最终变量被解析后会和字符串拼接在一起。

启用插件

你可以在 APISIX 配置文件 conf/config.yaml 中添加你的缓存配置,示例如下:

conf/config.yaml

  1. proxy_cache: # 代理缓存配置
  2. cache_ttl: 10s # 如果上游未指定缓存时间,则为默认缓存时间
  3. zones: # 缓存的参数
  4. - name: disk_cache_one # 缓存名称(缓存区域),管理员可以通过 admin api 中的 cache_zone 字段指定要使用的缓存区域
  5. memory_size: 50m # 共享内存的大小,用于存储缓存索引
  6. disk_size: 1G # 磁盘大小,用于存储缓存数据
  7. disk_path: "/tmp/disk_cache_one" # 存储缓存数据的路径
  8. cache_levels: "1:2" # 缓存的层次结构级别

以下示例展示了如何在指定路由上启用 proxy-cache 插件,cache_zone 字段默认设置为 disk_cache_one

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "plugins": {
  5. "proxy-cache": {
  6. "cache_key": ["$uri", "-cache-id"],
  7. "cache_bypass": ["$arg_bypass"],
  8. "cache_method": ["GET"],
  9. "cache_http_status": [200],
  10. "hide_cache_headers": true,
  11. "no_cache": ["$arg_test"]
  12. }
  13. },
  14. "upstream": {
  15. "nodes": {
  16. "127.0.0.1:1999": 1
  17. },
  18. "type": "roundrobin"
  19. },
  20. "uri": "/hello"
  21. }'

测试插件

按上述配置启用插件后,使用 curl 命令请求该路由:

  1. curl http://127.0.0.1:9080/hello -i

如果返回 200 HTTP 状态码,并且响应头中包含 Apisix-Cache-Status字段,则表示该插件已启用:

  1. HTTP/1.1 200 OK
  2. ···
  3. Apisix-Cache-Status: MISS
  4. hello

如果你是第一次请求该路由,数据未缓存,那么 Apisix-Cache-Status 字段应为 MISS。此时再次请求该路由:

  1. curl http://127.0.0.1:9080/hello -i

如果返回的响应头中 Apisix-Cache-Status 字段变为 HIT,则表示数据已被缓存,插件生效:

  1. HTTP/1.1 200 OK
  2. ···
  3. Apisix-Cache-Status: HIT
  4. hello

如果你设置 "cache_zone": "invalid_disk_cache" 属性为无效值,即与配置文件 conf/config.yaml 中指定的缓存区域不一致,那么它将返回 404 HTTP 响应码。

proxy-cache - 图2提示

为了清除缓存数据,你只需要指定请求的 method 为 PURGE

  1. curl -i http://127.0.0.1:9080/hello -X PURGE

HTTP 响应码为 200 即表示删除成功,如果缓存的数据未找到将返回 404

  1. HTTP/1.1 200 OK

禁用插件

当你需要禁用该插件时,可以通过以下命令删除相应的 JSON 配置,APISIX 将会自动重新加载相关配置,无需重启服务:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "uri": "/hello",
  5. "plugins": {},
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:1999": 1
  10. }
  11. }
  12. }'