SylviaBABY commented on code in PR #7858: URL: https://github.com/apache/apisix/pull/7858#discussion_r962398354
########## docs/zh/latest/plugins/proxy-cache.md: ########## @@ -23,53 +28,51 @@ title: proxy-cache ## 描述 -代理缓存插件,该插件提供缓存后端响应数据的能力,它可以和其他插件一起使用。该插件支持基于磁盘的缓存,未来也会支持基于内存的缓存。目前可以根据响应码、请求 Method 来指定需要缓存的数据,另外也可以通过 no_cache 和 cache_bypass 配置更复杂的缓存策略。 - -基于磁盘的缓存需要注意: - -1. 不能动态配置缓存的过期时间,只能通过后端服务响应头 Expires 或 Cache-Control 来设置过期时间,如果后端响应头中没有 Expires 或 Cache-Control,那么 APISIX 将默认只缓存 10 秒钟 -2. 如果后端服务不可用, APISIX 将返回 502 或 504,那么 502 或 504 将被缓存 10 秒钟 +`proxy-cache` 插件提供缓存后端响应数据的能力,它可以和其他插件一起使用。该插件支持基于磁盘的缓存,未来也会支持基于内存的缓存。目前可以根据响应码、请求模式来指定需要缓存的数据,也可以通过 `no_cache` 和 `cache_bypass`属性配置更复杂的缓存策略。 Review Comment: ```suggestion `proxy-cache` 插件提供缓存后端响应数据的能力,它可以和其他插件一起使用。该插件支持基于磁盘的缓存,未来也会支持基于内存的缓存。目前可以根据响应码和请求模式来指定需要缓存的数据,也可以通过 `no_cache` 和 `cache_bypass`属性配置更复杂的缓存策略。 ``` ########## docs/zh/latest/plugins/proxy-cache.md: ########## @@ -91,192 +94,61 @@ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f1 }' ``` -测试: +## 测试插件 -```shell -$ curl http://127.0.0.1:9080/hello -i -HTTP/1.1 200 OK -Content-Type: application/octet-stream -Content-Length: 6 -Connection: keep-alive -Server: APISIX web server -Date: Tue, 03 Mar 2020 10:45:36 GMT -Last-Modified: Tue, 03 Mar 2020 10:36:38 GMT -Apisix-Cache-Status: MISS +按上述配置启用插件后,使用 `curl` 命令请求该路由: -hello +```shell +curl http://127.0.0.1:9080/hello -i ``` -> http status 返回 `200` 并且响应头中包含 `Apisix-Cache-Status`,表示该插件已启用。 - -2、验证数据是否被缓存,再次请求上边的地址: +如果返回 `200` HTTP 状态码,并且响应头中包含 `Apisix-Cache-Status`字段,则表示该插件已启用: ```shell -$ curl http://127.0.0.1:9080/hello -i HTTP/1.1 200 OK -Content-Type: application/octet-stream -Content-Length: 6 -Connection: keep-alive -Server: APISIX web server -Date: Tue, 03 Mar 2020 11:14:46 GMT -Last-Modified: Thu, 20 Feb 2020 14:21:41 GMT -Apisix-Cache-Status: HIT +··· +Apisix-Cache-Status: MISS hello ``` -> 响应头 Apisix-Cache-Status 值变为了 HIT,说明数据已经被缓存 - -示例二:自定义 cache_zone 参数为 `disk_cache_two` - -1、在 `conf/config.yaml` 文件中的指定缓存区域等信息: - -```yaml -proxy_cache: - cache_ttl: 10s - zones: - - name: disk_cache_two - memory_size: 50m - disk_size: 1G - disk_path: "/tmp/disk_cache_one" - cache_levels: "1:2" -``` - -2、为特定路由启用 `proxy-cache` 插件: +如果你是第一次请求该路由,数据未缓存,那么 `Apisix-Cache-Status` 字段应为 `MISS`。此时再次请求该路由: ```shell -$ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' -{ - "plugins": { - "proxy-cache": { - "cache_zone": "disk_cache_two", - "cache_key": ["$uri", "-cache-id"], - "cache_bypass": ["$arg_bypass"], - "cache_method": ["GET"], - "cache_http_status": [200], - "hide_cache_headers": true, - "no_cache": ["$arg_test"] - } - }, - "upstream": { - "nodes": { - "127.0.0.1:1999": 1 - }, - "type": "roundrobin" - }, - "uri": "/hello" -}' +curl http://127.0.0.1:9080/hello -i ``` -测试: - -```shell -$ curl http://127.0.0.1:9080/hello -i -HTTP/1.1 200 OK -Content-Type: application/octet-stream -Content-Length: 6 -Connection: keep-alive -Server: APISIX web server -Date: Tue, 03 Mar 2020 10:45:36 GMT -Last-Modified: Tue, 03 Mar 2020 10:36:38 GMT -Apisix-Cache-Status: MISS - -hello -``` - -> http status 返回 `200` 并且响应头中包含 `Apisix-Cache-Status`,表示该插件已启用。 - -3、验证数据是否被缓存,再次请求上面的地址: +如果返回的响应头中 `Apisix-Cache-Status` 字段变为 `HIT`,则表示数据已被缓存,插件生效: ```shell -$ curl http://127.0.0.1:9080/hello -i HTTP/1.1 200 OK -Content-Type: application/octet-stream -Content-Length: 6 -Connection: keep-alive -Server: APISIX web server -Date: Tue, 03 Mar 2020 11:14:46 GMT -Last-Modified: Thu, 20 Feb 2020 14:21:41 GMT +··· Apisix-Cache-Status: HIT hello ``` -> 响应头 `Apisix-Cache-Status` 值变为了 HIT,说明数据已经被缓存 +如果你设置 `"cache_zone": "invalid_disk_cache"` 属性为无效值,即与配置文件 `conf/config.yaml` 中指定的缓存区域不一致,那么它将返回 `404` HTTP 响应码。 -示例 3:指定 cache_zone 为 `invalid_disk_cache` 与 `conf/config.yaml` 文件中指定的缓存区域 `disk_cache_one` 不一致。 +:::tip 提示 -```shell -$ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' -{ - "plugins": { - "proxy-cache": { - "cache_zone": "invalid_disk_cache", - "cache_key": ["$uri", "-cache-id"], - "cache_bypass": ["$arg_bypass"], - "cache_method": ["GET"], - "cache_http_status": [200], - "hide_cache_headers": true, - "no_cache": ["$arg_test"] - } - }, - "upstream": { - "nodes": { - "127.0.0.1:1999": 1 - }, - "type": "roundrobin" - }, - "uri": "/hello" -}' -``` +为了清除缓存数据,你只需要指定请求的 method 为 `PURGE`: -```json -{"error_msg":"failed to check the configuration of plugin proxy-cache err: cache_zone invalid_disk_cache not found"} +```shell +curl -i http://127.0.0.1:9080/hello -X PURGE ``` -响应错误信息,表示插件配置无效。 - -### 清除缓存数据 - -如何清理缓存的数据,只需要指定请求的 method 为 PURGE。 - -测试: +HTTP 响应码为 `200` 即表示删除成功,如果缓存的数据未找到将返回 `404`: ```shell -$ curl -i http://127.0.0.1:9080/hello -X PURGE HTTP/1.1 200 OK -Date: Tue, 03 Mar 2020 11:17:35 GMT -Content-Type: text/plain -Transfer-Encoding: chunked -Connection: keep-alive -Server: APISIX web server ``` -> 响应码为 200 即表示删除成功,如果缓存的数据未找到将返回 404。 +::: -再次请求,缓存数据未找到返回 404: +## 禁用插件 -```shell -$ curl -i http://127.0.0.1:9080/hello -X PURGE -HTTP/1.1 404 Not Found -Date: Wed, 18 Nov 2020 05:46:34 GMT -Content-Type: text/plain -Transfer-Encoding: chunked -Connection: keep-alive -Server: APISIX web server - -<html> -<head><title>404 Not Found</title></head> -<body> -<center><h1>404 Not Found</h1></center> -<hr><center>openresty</center> -</body> -</html> -``` - -### 禁用插件 - -移除插件配置中相应的 JSON 配置可立即禁用该插件,无需重启服务: +当你需要禁用该插件时,可以通过以下命令删除相应的 JSON 配置,APISIX 将会自动重新加载相关配置,无需重启服务: ```shell curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' Review Comment: ```suggestion curl http://127.0.0.1:9180/apisix/admin/routes/1 \ -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
