Skip to content

Conversation

@zou8944
Copy link

@zou8944 zou8944 commented Nov 6, 2022

Description

In official website, the cache-api-responses in language zh is dazzle, so here I am, contributing this page.

image

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have updated the documentation to reflect this change

Copy link
Contributor

@guitu168 guitu168 left a 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 响应
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
title: 缓存 API 响应
title: API 缓存

Copy link
Author

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 响应
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
title: 缓存 API 响应
title: 缓存 API

@zou8944
Copy link
Author

zou8944 commented Nov 7, 2022

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.

OK


## 通过缓存提升性能

我们总是希望构建简单快速的 API,但随着 API 请求的并发数增加,如下问题日益凸显,此时考虑使用缓存:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
我们总是希望构建简单快速的 API,但随着 API 请求的并发数增加,如下问题日益凸显,此时考虑使用缓存
在一般情况下,我们希望可以构建简单快速的服务,但随着请求的并发数增加,以下问题日益凸显


## API 网关缓存

[缓存](https://en.wikipedia.org/wiki/Cache_(computing))能够将请求的响应存储下来,在下次请求到来时直接使用。在 Web 应用架构的不同层级中,缓存均有应用,比如:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[缓存](https://en.wikipedia.org/wiki/Cache_(computing))能够将请求的响应存储下来,在下次请求到来时直接使用。在 Web 应用架构的不同层级中,缓存均有应用,比如:
通过[缓存](https://en.wikipedia.org/wiki/Cache_(computing))可以将请求的响应存储下来,当请求再次到来时,可以直接使用缓存中的数据。在 Web 应用架构的不同层级中,缓存均有应用,比如:

- 服务端缓存(API 缓存)
- 数据库缓存

本文讨论另一种缓存机制——**反向代理缓存**,通常在 API 网关中实现,通过在网关中缓存后端服务的响应,以达到减少对后端服务的访问次数,降低请求延迟的目的。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
本文讨论另一种缓存机制——**反向代理缓存**,通常在 API 网关中实现,通过在网关中缓存后端服务的响应,以达到减少对后端服务的访问次数,降低请求延迟的目的。
本教程则介绍另外一种缓存机制——**反向代理缓存**。该缓存机制通常在 API 网关中实现,通过在网关中缓存后端服务的响应,以达到减少对后端服务的访问次数,降低请求延迟的目的。


本文讨论另一种缓存机制——**反向代理缓存**,通常在 API 网关中实现,通过在网关中缓存后端服务的响应,以达到减少对后端服务的访问次数,降低请求延迟的目的。

其基本工作原理为:如果 API 网关已经缓存了请求资源的最新副本,则直接用该副本响应请求,不会再请求后端服务;否则,请求将会被转发到对应的后端服务。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
其基本工作原理为:如果 API 网关已经缓存了请求资源的最新副本,则直接用该副本响应请求,不会再请求后端服务;否则,请求将会被转发到对应的后端服务。
工作原理:如果 API 网关已经缓存了请求资源的最新副本,则直接用该副本响应请求;否则,请求将会被转发到对应的后端服务。

Comment on lines 71 to 74
:::note 注意
注意:一般来说,除特殊情况外,`GET`请求应默认开启缓存。
:::

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:::note 注意
注意:一般来说,除特殊情况外,`GET`请求应默认开启缓存。
:::
:::note 注意
除特殊情况外,`GET` 请求应默认开启缓存。
:::

}'
```

最后,添加新的 `route` ,配置 `proxy-cache` 插件开启缓存能力,并通过 `upstream_id` 引用上一步创建的上游服务。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
最后,添加新的 `route` ,配置 `proxy-cache` 插件开启缓存能力,并通过 `upstream_id` 引用上一步创建的上游服务。
最后,添加新的 Route,配置 `proxy-cache` 插件开启缓存能力,并通过 `upstream_id` 引用上一步创建的上游服务。

}'
```

如上,配置完成,我们的配置中,仅会缓存 `GET` 请求成功后的响应。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
如上,配置完成,我们的配置中,仅会缓存 `GET` 请求成功后的响应。
配置完成后,仅会缓存 `GET` 请求成功后的响应。


## Validate Proxy Caching

为了验证缓存是否正常工作,我们可以向 `/api/products` 发送多个请求,每次都应该能得到 `HTTP 200 OK` 的响应。首次访问时,由于响应尚未被缓存,响应头 `Apisix-Cache-Status` 的值将会 `MISS` 。再次访问时,将会得到一个已缓存的响应, `Apisix-Cache-Status` 的值将会是 `HIT` 。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
为了验证缓存是否正常工作,我们可以向 `/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` )之后访问,将会得到如下响应:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
如果我们在缓存存活时间( `time-to-live` ,简称 `TTL` )之后访问,将会得到如下响应:
如果我们在缓存存活时间(`time-to-live` ,简称 `TTL`)之后访问,将会得到如下响应:

Comment on lines 217 to 219
你可参考[发布 API](https://apisix.apache.org/zh/docs/apisix/tutorials/expose-api/)了解如何发布你的第一个 API。

也可参考[保护 API](https://apisix.apache.org/zh/docs/apisix/tutorials/protect-api/)了解如何保护你的 API。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
你可参考[发布 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 应用架构的不同层级中,缓存均有应用,比如:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
通过[缓存](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 命令行工具构建镜它。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
首先将该示例项目([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 层获取产品列表。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
此项目暴露了一个简单 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` 等,如下:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
用户也可以在该配置文件中添加缓存的配置信息,如 `disk_size``memory_size` 等,如下
你也可以在该配置文件中添加缓存的配置信息,例如 `disk_size``memory_size` 等,示例如下

Comment on lines +93 to +101
``` 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``` 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 代码中手动添加延迟,用以对比有缓存和没有缓存时的响应时间:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
我们也可以在 Product Controller 代码中手动添加延迟,用以对比有缓存和没有缓存时的响应时间:
你也可以在 Product Controller 代码中手动添加延迟,用以对比有缓存和没有缓存时的响应时间:

 }
```

测量响应耗时的 `curl` 命令如下:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
测量响应耗时的 `curl` 命令如下
你可以通过以下命令测试响应耗时


## 下一步

通过本文,我们了解到,有了 APISIX 的赋能,为示例项目设置 API 响应缓存将会非常简单。它能显著减少对后端服务的访问次数,降低 API 访问延迟。此外,APISIX 中还有很多其它内建的插件可用,具体请参考[插件中心](https://apisix.apache.org/plugins/),根据需要取用。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
通过本文,我们了解到,有了 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` 请求开启缓存。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
假设该产品列表每天仅更新一次,而后端服务每天会接收数以亿计获取该列表的请求。此时,`proxy-cache` 插件就有了用武之地。作为演示,我们仅为 `GET` 请求开启缓存。
假设该产品列表每天仅更新一次,而后端服务每天会接收数以亿计获取该列表的请求。此时,你可以通过 `proxy-cache` 插件来实现 API 响应信息缓存,降低服务延迟,减轻后端服务压力。本示例中,仅为 `GET` 请求开启缓存。


## 配置缓存插件

由于在当前项目中,我们尚未在 APISIX 中注册 `proxy-cache` 插件,所以首先需要注册,方法是将其添加在配置文件 `./conf/config.yaml` 的插件列表末尾:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
由于在当前项目中,我们尚未在 APISIX 中注册 `proxy-cache` 插件,所以首先需要注册,方法是将其添加在配置文件 `./conf/config.yaml` 的插件列表末尾
`proxy-cache` 插件默认为启用状态,你也可以在配置文件 `./conf/config.yaml` 中启用该插件,示例如下

@zou8944
Copy link
Author

zou8944 commented Nov 21, 2022

Hello, @hf400159, you updated almost all my translations, I don't think you need my contribution.
If you point out my format is not correct and some fine-tuning about content, I think it's good suggestion.
But according to current situation, it seems that you want follow your own idea completely.
So if needed, I suggest you close this pull request directly.

Thanks.

@zou8944 zou8944 closed this Nov 21, 2022
@guitu168
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants