-
Notifications
You must be signed in to change notification settings - Fork 2.8k
docs: add cache-api-responses.md in docs/zh/latest/tutorials #8252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Can you modify the format? please refer to: https://apisix.apache.org/zh/docs/general/documentation-style-guide/
Perhaps you can also modify the sentence in the document.
| @@ -0,0 +1,223 @@ | |||
| --- | |||
| title: 缓存 API 响应 | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| title: 缓存 API 响应 | |
| title: API 缓存 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
API 缓存 or 缓存 API ?
| @@ -0,0 +1,223 @@ | |||
| --- | |||
| title: 缓存 API 响应 | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| title: 缓存 API 响应 | |
| title: 缓存 API |
OK |
|
|
||
| ## 通过缓存提升性能 | ||
|
|
||
| 我们总是希望构建简单快速的 API,但随着 API 请求的并发数增加,如下问题日益凸显,此时考虑使用缓存: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 我们总是希望构建简单快速的 API,但随着 API 请求的并发数增加,如下问题日益凸显,此时考虑使用缓存: | |
| 在一般情况下,我们希望可以构建简单快速的服务,但随着请求的并发数增加,以下问题日益凸显: |
|
|
||
| ## API 网关缓存 | ||
|
|
||
| [缓存](https://en.wikipedia.org/wiki/Cache_(computing))能够将请求的响应存储下来,在下次请求到来时直接使用。在 Web 应用架构的不同层级中,缓存均有应用,比如: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| [缓存](https://en.wikipedia.org/wiki/Cache_(computing))能够将请求的响应存储下来,在下次请求到来时直接使用。在 Web 应用架构的不同层级中,缓存均有应用,比如: | |
| 通过[缓存](https://en.wikipedia.org/wiki/Cache_(computing))可以将请求的响应存储下来,当请求再次到来时,可以直接使用缓存中的数据。在 Web 应用架构的不同层级中,缓存均有应用,比如: |
| - 服务端缓存(API 缓存) | ||
| - 数据库缓存 | ||
|
|
||
| 本文讨论另一种缓存机制——**反向代理缓存**,通常在 API 网关中实现,通过在网关中缓存后端服务的响应,以达到减少对后端服务的访问次数,降低请求延迟的目的。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 本文讨论另一种缓存机制——**反向代理缓存**,通常在 API 网关中实现,通过在网关中缓存后端服务的响应,以达到减少对后端服务的访问次数,降低请求延迟的目的。 | |
| 本教程则介绍另外一种缓存机制——**反向代理缓存**。该缓存机制通常在 API 网关中实现,通过在网关中缓存后端服务的响应,以达到减少对后端服务的访问次数,降低请求延迟的目的。 |
|
|
||
| 本文讨论另一种缓存机制——**反向代理缓存**,通常在 API 网关中实现,通过在网关中缓存后端服务的响应,以达到减少对后端服务的访问次数,降低请求延迟的目的。 | ||
|
|
||
| 其基本工作原理为:如果 API 网关已经缓存了请求资源的最新副本,则直接用该副本响应请求,不会再请求后端服务;否则,请求将会被转发到对应的后端服务。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 其基本工作原理为:如果 API 网关已经缓存了请求资源的最新副本,则直接用该副本响应请求,不会再请求后端服务;否则,请求将会被转发到对应的后端服务。 | |
| 工作原理:如果 API 网关已经缓存了请求资源的最新副本,则直接用该副本响应请求;否则,请求将会被转发到对应的后端服务。 |
| :::note 注意 | ||
| 注意:一般来说,除特殊情况外,`GET`请求应默认开启缓存。 | ||
| ::: | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| :::note 注意 | |
| 注意:一般来说,除特殊情况外,`GET`请求应默认开启缓存。 | |
| ::: | |
| :::note 注意 | |
| 除特殊情况外,`GET` 请求应默认开启缓存。 | |
| ::: | |
| }' | ||
| ``` | ||
|
|
||
| 最后,添加新的 `route` ,配置 `proxy-cache` 插件开启缓存能力,并通过 `upstream_id` 引用上一步创建的上游服务。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 最后,添加新的 `route` ,配置 `proxy-cache` 插件开启缓存能力,并通过 `upstream_id` 引用上一步创建的上游服务。 | |
| 最后,添加新的 Route,配置 `proxy-cache` 插件开启缓存能力,并通过 `upstream_id` 引用上一步创建的上游服务。 |
| }' | ||
| ``` | ||
|
|
||
| 如上,配置完成,我们的配置中,仅会缓存 `GET` 请求成功后的响应。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 如上,配置完成,我们的配置中,仅会缓存 `GET` 请求成功后的响应。 | |
| 配置完成后,仅会缓存 `GET` 请求成功后的响应。 |
|
|
||
| ## Validate Proxy Caching | ||
|
|
||
| 为了验证缓存是否正常工作,我们可以向 `/api/products` 发送多个请求,每次都应该能得到 `HTTP 200 OK` 的响应。首次访问时,由于响应尚未被缓存,响应头 `Apisix-Cache-Status` 的值将会 `MISS` 。再次访问时,将会得到一个已缓存的响应, `Apisix-Cache-Status` 的值将会是 `HIT` 。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 为了验证缓存是否正常工作,我们可以向 `/api/products` 发送多个请求,每次都应该能得到 `HTTP 200 OK` 的响应。首次访问时,由于响应尚未被缓存,响应头 `Apisix-Cache-Status` 的值将会 `MISS` 。再次访问时,将会得到一个已缓存的响应, `Apisix-Cache-Status` 的值将会是 `HIT` 。 | |
| 为了验证缓存是否正常工作,我们可以向 `/api/products` 发送多个请求,每次都应该能得到 `HTTP 200 OK` 的响应。首次访问时,由于响应尚未被缓存,响应头 `Apisix-Cache-Status` 的值将会 `MISS`。再次访问时,将会得到一个已缓存的响应,`Apisix-Cache-Status` 的值将会是 `HIT`。 |
| Apisix-Cache-Status: HIT | ||
| ``` | ||
|
|
||
| 如果我们在缓存存活时间( `time-to-live` ,简称 `TTL` )之后访问,将会得到如下响应: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 如果我们在缓存存活时间( `time-to-live` ,简称 `TTL` )之后访问,将会得到如下响应: | |
| 如果我们在缓存存活时间(`time-to-live` ,简称 `TTL`)之后访问,将会得到如下响应: |
| 你可参考[发布 API](https://apisix.apache.org/zh/docs/apisix/tutorials/expose-api/)了解如何发布你的第一个 API。 | ||
|
|
||
| 也可参考[保护 API](https://apisix.apache.org/zh/docs/apisix/tutorials/protect-api/)了解如何保护你的 API。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 你可参考[发布 API](https://apisix.apache.org/zh/docs/apisix/tutorials/expose-api/)了解如何发布你的第一个 API。 | |
| 也可参考[保护 API](https://apisix.apache.org/zh/docs/apisix/tutorials/protect-api/)了解如何保护你的 API。 | |
| 你可参考[发布 API](https://apisix.apache.org/zh/docs/apisix/tutorials/expose-api/) 了解如何发布你的第一个 API。 | |
| 也可参考[保护 API](https://apisix.apache.org/zh/docs/apisix/tutorials/protect-api/) 了解如何保护你的 API。 |
|
|
||
| ## API 网关缓存 | ||
|
|
||
| 通过[缓存](https://en.wikipedia.org/wiki/Cache_(computing))可以将请求的响应存储下来,当请求再次到来时,可以直接使用缓存中的数据。在 Web 应用架构的不同层级中,缓存均有应用,比如: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 通过[缓存](https://en.wikipedia.org/wiki/Cache_(computing))可以将请求的响应存储下来,当请求再次到来时,可以直接使用缓存中的数据。在 Web 应用架构的不同层级中,缓存均有应用,比如: | |
| 通过[缓存](https://en.wikipedia.org/wiki/Cache_(computing)) APISIX 可以将请求的响应存储到内存或者磁盘中,当请求再次到来时,可以直接使用缓存中的数据。在 Web 应用架构的不同层级中,缓存均有应用,比如: |
|
|
||
| ## 运行示例项目 | ||
|
|
||
| 首先将该示例项目([apisix-dotnet-docker](https://github.com/Boburmirzo/apisix-dotnet-docker))运行起来。用户可在 Github 上获取完整源码,并了解如何用 Docker 命令行工具构建镜它。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 首先将该示例项目([apisix-dotnet-docker](https://github.com/Boburmirzo/apisix-dotnet-docker))运行起来。用户可在 Github 上获取完整源码,并了解如何用 Docker 命令行工具构建镜它。 | |
| 首先通过 [apisix-dotnet-docker](https://github.com/Boburmirzo/apisix-dotnet-docker) 仓库,运行示例项目。你可以在 Github 上获取完整源码,并了解如何用 Docker 命令行工具构建镜它。 |
|
|
||
| 首先将该示例项目([apisix-dotnet-docker](https://github.com/Boburmirzo/apisix-dotnet-docker))运行起来。用户可在 Github 上获取完整源码,并了解如何用 Docker 命令行工具构建镜它。 | ||
|
|
||
| 此项目暴露了一个简单 API — 在 [ProductsControllers.cs](https://github.com/Boburmirzo/apisix-dotnet-docker/blob/main/ProductApi/Controllers/ProductsController.cs) 中调用 service 层获取产品列表。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 此项目暴露了一个简单 API — 在 [ProductsControllers.cs](https://github.com/Boburmirzo/apisix-dotnet-docker/blob/main/ProductApi/Controllers/ProductsController.cs) 中调用 service 层获取产品列表。 | |
| 该项目暴露了一个简单的 API — 在 [ProductsControllers.cs](https://github.com/Boburmirzo/apisix-dotnet-docker/blob/main/ProductApi/Controllers/ProductsController.cs) 中调用 service 层获取产品列表。 |
| - proxy-cache | ||
| ``` | ||
|
|
||
| 用户也可以在该配置文件中添加缓存的配置信息,如 `disk_size`、`memory_size` 等,如下: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 用户也可以在该配置文件中添加缓存的配置信息,如 `disk_size`、`memory_size` 等,如下: | |
| 你也可以在该配置文件中添加缓存的配置信息,例如 `disk_size`、`memory_size` 等,示例如下: |
| ``` yaml | ||
| proxy_cache: | ||
| cache_ttl: 10s # default caching time if the upstream doesn't specify the caching time | ||
| zones: | ||
| - name: disk_cache_one # name of the cache. Admin can specify which cache to use in the Admin API by name | ||
| memory_size: 50m # size of shared memory, used to store the cache index | ||
| disk_size: 1G # size of disk, used to store the cache data | ||
| disk_path: "/tmp/disk_cache_one" # path to store the cache data | ||
| cache_levels: "1:2" # hierarchy levels of the cache |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ``` yaml | |
| proxy_cache: | |
| cache_ttl: 10s # default caching time if the upstream doesn't specify the caching time | |
| zones: | |
| - name: disk_cache_one # name of the cache. Admin can specify which cache to use in the Admin API by name | |
| memory_size: 50m # size of shared memory, used to store the cache index | |
| disk_size: 1G # size of disk, used to store the cache data | |
| disk_path: "/tmp/disk_cache_one" # path to store the cache data | |
| cache_levels: "1:2" # hierarchy levels of the cache | |
| ``` yaml | |
| proxy_cache: # 代理缓存配置 | |
| cache_ttl: 10s # 如果上游未指定缓存时间,则为默认缓存时间 | |
| zones: # 缓存的参数 | |
| - name: disk_cache_one # 缓存名称(缓存区域),管理员可以通过 admin api 中的 cache_zone 字段指定要使用的缓存区域 | |
| memory_size: 50m # 共享内存的大小,用于存储缓存索引 | |
| disk_size: 1G # 磁盘大小,用于存储缓存数据 | |
| disk_path: "/tmp/disk_cache_one" # 存储缓存数据的路径 | |
| cache_levels: "1:2" # 缓存的层次结构级别 |
|
|
||
| ### 进一步测试 | ||
|
|
||
| 我们也可以在 Product Controller 代码中手动添加延迟,用以对比有缓存和没有缓存时的响应时间: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 我们也可以在 Product Controller 代码中手动添加延迟,用以对比有缓存和没有缓存时的响应时间: | |
| 你也可以在 Product Controller 代码中手动添加延迟,用以对比有缓存和没有缓存时的响应时间: |
| } | ||
| ``` | ||
|
|
||
| 测量响应耗时的 `curl` 命令如下: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 测量响应耗时的 `curl` 命令如下: | |
| 你可以通过以下命令测试响应耗时: |
|
|
||
| ## 下一步 | ||
|
|
||
| 通过本文,我们了解到,有了 APISIX 的赋能,为示例项目设置 API 响应缓存将会非常简单。它能显著减少对后端服务的访问次数,降低 API 访问延迟。此外,APISIX 中还有很多其它内建的插件可用,具体请参考[插件中心](https://apisix.apache.org/plugins/),根据需要取用。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 通过本文,我们了解到,有了 APISIX 的赋能,为示例项目设置 API 响应缓存将会非常简单。它能显著减少对后端服务的访问次数,降低 API 访问延迟。此外,APISIX 中还有很多其它内建的插件可用,具体请参考[插件中心](https://apisix.apache.org/plugins/),根据需要取用。 | |
| 通过本教程,相信你已经了解了如何为项目设置 API 响应缓存。设置缓存后,可以显著地减少请求对后端服务的访问次数,降低 API 访问延迟。此外,APISIX 还支持了很多插件,你可以访问[插件中心](https://apisix.apache.org/plugins/)查找自己需要的插件。 |
|
|
||
| 此项目暴露了一个简单 API — 在 [ProductsControllers.cs](https://github.com/Boburmirzo/apisix-dotnet-docker/blob/main/ProductApi/Controllers/ProductsController.cs) 中调用 service 层获取产品列表。 | ||
|
|
||
| 假设该产品列表每天仅更新一次,而后端服务每天会接收数以亿计获取该列表的请求。此时,`proxy-cache` 插件就有了用武之地。作为演示,我们仅为 `GET` 请求开启缓存。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 假设该产品列表每天仅更新一次,而后端服务每天会接收数以亿计获取该列表的请求。此时,`proxy-cache` 插件就有了用武之地。作为演示,我们仅为 `GET` 请求开启缓存。 | |
| 假设该产品列表每天仅更新一次,而后端服务每天会接收数以亿计获取该列表的请求。此时,你可以通过 `proxy-cache` 插件来实现 API 响应信息缓存,降低服务延迟,减轻后端服务压力。本示例中,仅为 `GET` 请求开启缓存。 |
|
|
||
| ## 配置缓存插件 | ||
|
|
||
| 由于在当前项目中,我们尚未在 APISIX 中注册 `proxy-cache` 插件,所以首先需要注册,方法是将其添加在配置文件 `./conf/config.yaml` 的插件列表末尾: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 由于在当前项目中,我们尚未在 APISIX 中注册 `proxy-cache` 插件,所以首先需要注册,方法是将其添加在配置文件 `./conf/config.yaml` 的插件列表末尾: | |
| `proxy-cache` 插件默认为启用状态,你也可以在配置文件 `./conf/config.yaml` 中启用该插件,示例如下: |
|
Hello, @hf400159, you updated almost all my translations, I don't think you need my contribution. Thanks. |
|
Sorry, for the documentation of APISIX, we should keep the content of the documentation concise and standardized. Could you submit this PR first; what do you think? Thanks for your contribution~ @zou8944 |
Description
In official website, the
cache-api-responsesin language zh is dazzle, so here I am, contributing this page.Checklist