@ ardrive/ ardrive- 保证- cache
@ardrive/ardrive-promise-cache 是一个缓存库,旨在缓存承诺,从而能够更快地随后检索数据。 它包括两种类型的缓存执行:
- 承诺缓存 - 一个简单的缓存,在记忆中存储承诺
- 通过Promise缓存读取 - 包装
PromiseCache允许提供合成readThroughFunction当缓存未包含请求的密钥时调用
安装
您可以使用 npm 或纱线安装库 :
npm i @ardrive/ardrive-promise-cache
yarn add @ardrive/ardrive-promise-cache
使用量
承诺缓存
示例
import { PromiseCache, CacheParams } from '@ardrive/ardrive-promise-cache';
const params: CacheParams = {
cacheCapacity: 100,
cacheTTL: 60_000, // cache for 1 minute
};
const cache = new PromiseCache<string, number>(params);
// Storing a promise
const promise1 = new Promise<number>((resolve) => resolve(42));
cache.put('answer', promise1);
// resolves to 42
await cache.get('answer');
const promise2 = new Promise<number>((resolve) => resolve(100));
// overwrite existing cached promise
cache.put('answer', promise2);
// resolves to 100
await cache.get('answer');
// removes the promise from the cache
cache.remove('answer');
// resolves to undefined as no longer exists in the cache
await cache.get('answer');
// clear the cache
cache.clear();
// Getting the cache size
const size = cache.size();
API 密码
constructor({ cacheCapacity, cacheTTL }: CacheParams)
创建新 PromiseCache 实例,包含缓存项目的指定容量和运行时间.
put(key: K, value: Promise<V>): Promise<V>
在缓存中以指定的密钥存储一个承诺 .
get(key: K): Promise<V> | undefined
使用指定的密钥从缓存中获取一个承诺 。 回返 undefined 如果找不到密钥.
remove(key: K): void
使用指定的密钥从缓存中删除一个承诺.
clear(): void
清除整个缓存.
size(): number
返回缓存中当前项目的数量.
通过Promise缓存读取
通过缓存读取对高吞吐量系统有用. 如果缓存中包含请求的数据,则立即返回. 如果数据不存在(或已过期),缓存会获取并放置由此产生的承诺,用于未来的请求. 通过缓存读取的一些实例和使用例(和其他缓存模式) 这里。 。 。 .
示例
import { ReadThroughPromiseCache, CacheParams } from '@ardrive/ardrive-promise-cache';
const params: CacheParams = {
cacheCapacity: 100,
cacheTTL: 60_000, // cache for 1 minute
};
const readThroughCacheParams: ReadThroughPromiseCacheParams<string,AxiosResponse> = {
cacheParams: params,
readThroughFunction: async (key: K) => {
// This function will be called when the cache does not contain the requested key.
// It should return a promise that resolves with the value to cache.
return axios.get(`https://example.com/api/${key}`);
}
};
// create ReadThroughPromiseCache with capacity of 100 and TTL of 1 minute, and readThroughFunction to call API function
const readThroughCache = new ReadThroughPromiseCache<string, AxiosResponse>(readThroughCacheParams);
// caches new key and resulting axios promise (https://example.com/api/example) for 1 minute
const { status, data } = await readThroughCache.get('example')
// the cached axios promise will be returned
const { status: cachedStatus, data: cacheData } = await readThroughCache.get('example')
// wait 1 minute
await new Promise((res) => setTimeout(() => res, 60_000);
// 'example' key has expired, so readThroughFunction will be called again and new promise will be returned
const { status: refreshedStatus, data:refreshedData } = await readThroughCache.get('example')
API 密码
constructor({ cacheParams: { cacheCapacity, cacheTTL }, readThroughFunction: (key: string) => Promise<V>: ReadThroughPromiseCacheParams)
创建新 ReadThroughPromiseCache 实例,包含缓存项目和 readThroughFunction 当缓存中不包含密钥时,将调用.
getWithStatus(key: K): Promise<{status: 'hit' | 'miss', data: V}>
返回一个“ 承诺” 对象, 键“ 数据” 预期待, 和“ 状态” 键表示它是命中还是错失 .
说明
方法 cacheKeyString(key: K): string 用于内部创建缓存密钥。 默认执行可能无法充分区分某些对象类型的密钥,视其 toJSON 执行。 如果遇到问题, 您可能需要推翻这种方法 .