缓存操作
invalidateCache()
主动失效缓存。
前往手动失效缓存查看详情。
- 类型
type MethodFilter =
| string
| RegExp
| {
name?: string | RegExp;
filter?: MethodFilterHandler;
alova?: Alova;
};
function invalidateCache(matcher?: Method | Method[] | MethodFilter): void;
- 参数
matcher
:缓存失效的匹配器,值为 method 实例或数组,也可以设置为method 匹配器。
- 返回
无
- 示例
import { invalidateCache } from 'alova';
invalidateCache(method);
invalidateCache([method1, method2]);
invalidateCache({
name: 'userMethod',
filter: method => method.name === 'method1'
});
setCache()
设置响应缓存。
前往缓存更新与查找查看详情。
- 类型
type MethodFilter =
| string
| RegExp
| {
name?: string | RegExp;
filter?: MethodFilterHandler;
alova?: Alova;
};
function setCache(
matcher: Method | Method[] | MethodFilter,
dataOrUpdater: R | ((oldCache: R) => R | undefined | void)
): void;
- 参数
matcher
:值为 method 实例、method 的 name 字符串、method 的 name 正则表达式,也可以设置为method 匹配器,将会为所有符合条件的 method 实例设置缓存数据。dataOrUpdater
:缓存数据或更新函数,如果为函数,则需要返回新的缓存数据,如果返回undefined
或不返回则取消更新。
- 返回
无
- 示例
import { setCache } from 'alova';
setCache(method, {});
setCache([method1, method2], {});
setCache(
{
name: 'userMethod',
filter: method => method.name === 'method1'
},
{}
);
queryCache()
查询缓存。
前往缓存更新与查找查看详情。
- 类型
type MethodFilter =
| string
| RegExp
| {
name?: string | RegExp;
filter?: MethodFilterHandler;
alova?: Alova;
};
function queryCache(matcher?: Method | MethodFilter): R | undefined;
- 参数
matcher
:值为 method 实例、method 的 name 字符串、method 的 name 正则表达式,也可以设置为method 匹配器,将会为符合条件的第一个 method 实例查询缓存数据。
- 返回
缓存数据,如果没有缓存则返回undefined
。
- 示例
import { queryCache } from 'alova';
const responseCache = queryCache(method);
const responseCache = queryCache({
name: 'userMethod',
filter: method => method.name === 'method1'
});