Cache Operations
invalidateCache()
Actively invalidate the cache.
Go to Manually Invalidate Cache for details.
- Type
function invalidateCache(matcher?: Method | Method[]): Promise<void>;
- Parameter
matcher
: method instance or array for cache invalidation.
- Return
Promise Example
- Example
import { invalidateCache } from 'alova';
await invalidateCache(method);
await invalidateCache([method1, method2]);
const methodSnapshots = alova.snapshots.match('method-name');
await invalidateCache(methodSnapshots);
setCache()
Set the response cache.
Go to Cache Update and Lookup for details.
- Type
function setCache(
matcher: Method | Method[],
dataOrUpdater: R | ((oldCache: R) => R | undefined | void),
options?: CacheSetOptions
): Promise<void>;
- Parameter
matcher
: value is method instance or instance array.dataOrUpdater
: cache data or update function. If it is a function, it needs to return the new cache data. If it returnsundefined
or does not return, the update is canceled.options
: Configuration parameters
Parameter name | Type | Description |
---|---|---|
policy | 'l1' | 'l2' | 'all' | Cache update policy, l1 means only updating the l1 layer cache, l2 means only updating the l2 layer cache, and all means updating both the l1 and l2 layers cache. |
- Return
Promise instance
- Example
import { setCache } from 'alova';
await setCache(method, {});
await setCache([method1, method2], {});
const methodSnapshots = alova.snapshots.match('method-name');
await setCache(
methodSnapshots,
{},
{
policy: 'l1'
}
);
queryCache()
Query cache.
Go to Cache Update and Lookup for details.
- Type
function queryCache(
matcher?: Method,
options?: CacheQueryOptions
): Promise<Responded | undefined>;
- Parameter
matcher
: The value is a method instance, a method name string, or a method name regular expression. It can also be set to a method matcher. The cache data will be queried for the first method instance that meets the conditions.options
: Configuration parameters
Parameter name | Type | Description |
---|---|---|
policy | 'l1' | 'l2' | 'all' | Cache acquisition strategy, l1 means only obtaining the l1 layer cache, l2 means only obtaining the l2 layer cache, and all means querying both the l1 and l2 layer caches. |
- Return
Promise instance of cache data, returns undefined
if there is no cache.
- Example
import { queryCache } from 'alova';
const responseCache = await queryCache(method);
const methodSnapshot = alova.snapshots.match('method-name', true);
const responseCache = await queryCache(methodSnapshot, {
policy: 'l2'
});