Skip to content

Commit

Permalink
docs: modify data sharing of useRequest (alibaba#2333)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyib authored Sep 27, 2023
1 parent 6f47ac4 commit b06cfea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 6 additions & 4 deletions packages/hooks/src/useRequest/doc/cache/cache.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ By setting `staleTime`, we can specify the data retention time, during which tim

### Data sharing

The content of the same `cacheKey` is shared globally, which will bring the following features
> Note: If no new request is issued, the "Data sharing" will not be triggered. `cacheTime` and `staleTime` parameters will invalidate "Data sharing". [#2313](https://github.com/alibaba/hooks/issues/2313)
- Sharing request `Promise`, only one of the same `cacheKey` will initiate a request at the same time, and the subsequent ones will share the same request `Promise`.
- Data synchronization. At any time, when we change the content of one of the `cacheKey`, the content of the other `cacheKey` will be synchronized.
The content of the same `cacheKey` is shared globally, which will bring the following features:

- Sharing request `Promise`: Only one of the same `cacheKey` will initiate a request at the same time, and the subsequent ones will share the same request `Promise`.
- Data synchronization: When a request is made by one `cacheKey`, the contents of other identical `cacheKey` will be synchronized accordingly.

In the following example, the two components will only initiate one request during initialization. And the content of the two articles is always synchronized.

Expand Down Expand Up @@ -77,7 +79,7 @@ interface CachedData<TData, TParams> {

| Property | Description | Type | Default |
| --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------- | -------- |
| cacheKey | A unique ID of the request. If `cacheKey` is set, we will enable the caching mechanism. The data of the same `cacheKey` is globally synchronized. | `string` | - |
| cacheKey | A unique ID of the request. Data of the same `cacheKey` will synchronized globally (`cacheTime` and `staleTime` parameters will invalidate this mechanism, see demo: [Data sharing](#data-sharing)) | `string` | - |
| cacheTime | <ul><li> Set the cache time. By default, the cached data will be cleared after 5 minutes.</li><li> If set to `-1`, the cached data will never expire</li></ul> | `number` | `300000` |
| staleTime | <ul><li> Time to consider the cached data is fresh. Within this time interval, the request will not be re-initiated</li><li> If set to `-1`, it means that the data is always fresh</li></ul> | `number` | `0` |
| setCache | <ul><li> Custom set cache </li><li> `setCache` and `getCache` need to be used together</li><li> In the custom cache mode, `cacheTime` and `clearCache` are useless, please implement it yourself according to the actual situation.</li></ul> | `(data: CachedData) => void;` | - |
Expand Down
10 changes: 6 additions & 4 deletions packages/hooks/src/useRequest/doc/cache/cache.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ group:

### 数据共享

同一个 `cacheKey` 的内容,在全局是共享的,这会带来以下几个特性
> 注意:如果没有发起新请求,不会触发数据共享。`cacheTime``staleTime` 参数会使数据共享失效。[#2313](https://github.com/alibaba/hooks/issues/2313)
- 请求 `Promise` 共享,相同的 `cacheKey` 同时只会有一个在发起请求,后发起的会共用同一个请求 `Promise`
- 数据同步,任何时候,当我们改变其中某个 `cacheKey` 的内容时,其它相同 `cacheKey` 的内容均会同步
同一个 `cacheKey` 的内容,在全局是共享的,这会带来以下几个特性:

- 请求 `Promise` 共享:相同的 `cacheKey` 同时只会有一个在发起请求,后发起的会共用同一个请求 `Promise`
- 数据同步:当某个 `cacheKey` 发起请求时,其它相同 `cacheKey` 的内容均会随之同步

下面的示例中,初始化时,两个组件只会发起一个请求。并且两篇文章的内容永远是同步的。

Expand Down Expand Up @@ -77,7 +79,7 @@ interface CachedData<TData, TParams> {

| 参数 | 说明 | 类型 | 默认值 |
| --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------- | -------- |
| cacheKey | 请求唯一标识。如果设置了 `cacheKey`,我们会启用缓存机制。同一个 `cacheKey` 的数据全局同步。 | `string` | - |
| cacheKey | 请求的唯一标识。相同 `cacheKey` 的数据全局同步(`cacheTime``staleTime` 参数会使该机制失效,见示例:[数据共享](#数据共享))。 | `string` | - |
| cacheTime | <ul><li> 设置缓存数据回收时间。默认缓存数据 5 分钟后回收 </li><li> 如果设置为 `-1`, 则表示缓存数据永不过期</li></ul> | `number` | `300000` |
| staleTime | <ul><li> 缓存数据保持新鲜时间。在该时间间隔内,认为数据是新鲜的,不会重新发请求 </li><li> 如果设置为 `-1`,则表示数据永远新鲜</li></ul> | `number` | `0` |
| setCache | <ul><li> 自定义设置缓存 </li><li> `setCache``getCache` 需要配套使用</li><li> 在自定义缓存模式下,`cacheTime``clearCache` 不会生效,请根据实际情况自行实现。</li></ul> | `(data: CachedData) => void;` | - |
Expand Down

0 comments on commit b06cfea

Please sign in to comment.