Skip to content

Commit

Permalink
Merge pull request #63 from alovajs/docs/dynamic-method-key-in-cache
Browse files Browse the repository at this point in the history
docs: add a note about the dynamic method key
  • Loading branch information
MeetinaXD authored Jan 6, 2025
2 parents 10b9d49 + 1566a6c commit 81e7d3c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/tutorial/05-cache/01-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,28 @@ cacheFor: {
## Automatic response maintenance instructions

The key of the response data cache is a combination of the request method (method), request address (url), request header parameters (headers), url parameters (params), and request body parameters (requestBody) of the method instance as a unique identifier. Any information or location will be treated as a different key. If you want to customize the cache key, you can refer to [Custom method key](/tutorial/advanced/in-depth/custom-method-key).

Note that the key of the response data cache (or method) is **only** determined when the method created. After the method instance is created, modifying any of the following: request method (method), request address (url), request header parameters (headers), url parameters (params), or request body parameters (requestBody) will not cause the key to change, which will result in the cache pointing to the same value.

In this case, if you need to cache separately pointing to different values, consider dynamically creating methods.

```javascript
const alovaInst = createAlova({
// ...
beforeRequest(method) {
const sec = Math.floor(Date.now() / 1000);
method.config.headers.sec = sec;
}
});

const Getter = () =>
alovaInst.Get('/user/profile', {
// ...
cacheFor: 10 * 1000
});

// create a new method
Getter().send();
// or
useRequest(Getter);
```
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,28 @@ cacheFor: {
## 响应自动维护说明

响应数据缓存的 key 是由 method 实例的请求方法(method)、请求地址(url)、请求头参数(headers)、url 参数(params)、请求体参数(requestBody)组合作为唯一标识,任意一个信息或位置不同都将被当做不同的 key,如果要自定义缓存 key,可以参考[自定义 method key](/tutorial/advanced/in-depth/custom-method-key)

此外,数据缓存的 key **只在** method 实例创建时确定,在创建后修改其:请求方法(method)、请求地址(url)、请求头参数(headers)、url 参数(params)、请求体参数(requestBody) 任一都不会导致 key 发生变化,这会导致缓存指向同一个值。

如果你需要在上述情况下,缓存分别指向不同的值,请考虑动态创建 method。

```javascript
const alovaInst = createAlova({
// ...
beforeRequest(method) {
const sec = Math.floor(Date.now() / 1000);
method.config.headers.sec = sec;
}
});

const Getter = () =>
alovaInst.Get('/user/profile', {
// ...
cacheFor: 10 * 1000
});

// 调用函数来创建一个新的method
Getter().send();
// or
useRequest(Getter);
```
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,28 @@ localCache: {
## 响应自动维护说明

响应数据缓存的 key 是由 method 实例的请求方法(method)、请求地址(url)、请求头参数(headers)、url 参数(params)、请求体参数(requestBody)组合作为唯一标识,任意一个信息或位置不同都将被当做不同的 key。

此外,数据缓存的 key **只在** method 实例创建时确定,在创建后修改其:请求方法(method)、请求地址(url)、请求头参数(headers)、url 参数(params)、请求体参数(requestBody) 任一都不会导致 key 发生变化,这会导致缓存指向同一个值。

如果你需要在上述情况下,缓存分别指向不同的值,请考虑动态创建 method。

```javascript
const alovaInst = createAlova({
// ...
beforeRequest(method) {
const sec = Math.floor(Date.now() / 1000);
method.config.headers.sec = sec;
}
});

const Getter = () =>
alovaInst.Get('/user/profile', {
// ...
localCache: 10 * 1000
});

// 调用函数来创建一个新的method
Getter().send();
// or
useRequest(Getter);
```
25 changes: 25 additions & 0 deletions versioned_docs/version-v2/tutorial/04-cache/01-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,28 @@ localCache: {
## Instruction for response automatic maintenance

The key of the response data cache is uniquely identified by the combination of the request method (method), request address (url), request header parameters (headers), url parameters (params), and request body parameters (requestBody) of the method instance. Any information or Different positions will be treated as different keys.

Note that the key of the response data cache (or method) is **only** determined when the method created. After the method instance is created, modifying any of the following: request method (method), request address (url), request header parameters (headers), url parameters (params), or request body parameters (requestBody) will not cause the key to change, which will result in the cache pointing to the same value.

In this case, if you need to cache separately pointing to different values, consider dynamically creating methods.

```javascript
const alovaInst = createAlova({
// ...
beforeRequest(method) {
const sec = Math.floor(Date.now() / 1000);
method.config.headers.sec = sec;
}
});

const Getter = () =>
alovaInst.Get('/user/profile', {
// ...
localCache: 10 * 1000
});

// create a new method
Getter().send();
// or
useRequest(Getter);
```

0 comments on commit 81e7d3c

Please sign in to comment.