proxy-cache

    基于磁盘的缓存需要注意:

    1. 不能动态配置缓存的过期时间,只能通过后端服务响应头 Expires 或 Cache-Control 来设置过期时间,如果后端响应头中没有 Expires 或 Cache-Control,那么 APISIX 将默认只缓存 10 秒钟
    2. 如果后端服务不可用, APISIX 将返回 502 或 504,那么 502 或 504 将被缓存 10 秒钟

    注:变量以$开头,不存在时等价于空字符串。也可以使用变量和字符串的结合,但是需要以数组的形式分开写,最终变量被解析后会和字符串拼接在一起。

    在 文件中的配置示例:

    示例一:cache_zone 参数默认为 disk_cache_one

    1、为特定路由启用 proxy-cache 插件:

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

    测试:

    1. $ curl http://127.0.0.1:9080/hello -i
    2. HTTP/1.1 200 OK
    3. Content-Type: application/octet-stream
    4. Content-Length: 6
    5. Connection: keep-alive
    6. Server: APISIX web server
    7. Date: Tue, 03 Mar 2020 10:45:36 GMT
    8. Last-Modified: Tue, 03 Mar 2020 10:36:38 GMT
    9. Apisix-Cache-Status: MISS
    1. $ curl http://127.0.0.1:9080/hello -i
    2. HTTP/1.1 200 OK
    3. Content-Type: application/octet-stream
    4. Content-Length: 6
    5. Server: APISIX web server
    6. Date: Tue, 03 Mar 2020 11:14:46 GMT
    7. Last-Modified: Thu, 20 Feb 2020 14:21:41 GMT
    8. Apisix-Cache-Status: HIT
    9. hello

    示例二:自定义 cache_zone 参数为 disk_cache_two

    1、在 conf/config.yaml 文件中的指定缓存区域等信息:

    2、为特定路由启用 proxy-cache 插件:

    1. $ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. {
    3. "plugins": {
    4. "proxy-cache": {
    5. "cache_zone": "disk_cache_two",
    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. }'

    测试:

    1. $ curl http://127.0.0.1:9080/hello -i
    2. HTTP/1.1 200 OK
    3. Content-Type: application/octet-stream
    4. Content-Length: 6
    5. Connection: keep-alive
    6. Server: APISIX web server
    7. Last-Modified: Tue, 03 Mar 2020 10:36:38 GMT
    8. Apisix-Cache-Status: MISS
    9. hello

    3、验证数据是否被缓存,再次请求上面的地址:

    1. $ curl http://127.0.0.1:9080/hello -i
    2. HTTP/1.1 200 OK
    3. Content-Type: application/octet-stream
    4. Content-Length: 6
    5. Connection: keep-alive
    6. Server: APISIX web server
    7. Date: Tue, 03 Mar 2020 11:14:46 GMT
    8. Last-Modified: Thu, 20 Feb 2020 14:21:41 GMT
    9. Apisix-Cache-Status: HIT
    10. hello

    示例 3:指定 cache_zone 为 invalid_disk_cacheconf/config.yaml 文件中指定的缓存区域 disk_cache_one 不一致。

    1. {"error_msg":"failed to check the configuration of plugin proxy-cache err: cache_zone invalid_disk_cache not found"}

    响应错误信息,表示插件配置无效。

    如何清理缓存的数据,只需要指定请求的 method 为 PURGE。

    测试:

    1. $ curl -i http://127.0.0.1:9080/hello -X PURGE
    2. HTTP/1.1 200 OK
    3. Date: Tue, 03 Mar 2020 11:17:35 GMT
    4. Content-Type: text/plain
    5. Transfer-Encoding: chunked
    6. Connection: keep-alive
    7. Server: APISIX web server

    再次请求,缓存数据未找到返回 404:

    1. $ curl -i http://127.0.0.1:9080/hello -X PURGE
    2. HTTP/1.1 404 Not Found
    3. Date: Wed, 18 Nov 2020 05:46:34 GMT
    4. Content-Type: text/plain
    5. Transfer-Encoding: chunked
    6. Connection: keep-alive
    7. Server: APISIX web server
    8. <html>
    9. <head><title>404 Not Found</title></head>
    10. <body>
    11. <center><h1>404 Not Found</h1></center>
    12. <hr><center>openresty</center>
    13. </html>

    移除插件配置中相应的 JSON 配置可立即禁用该插件,无需重启服务: