类: DatabaseCache
定义于: packages/database/src/cache.ts:314
适配器共用的缓存读写与并发请求合并器。
示例
ts
const cache = new DatabaseCache({
store: new MemoryCacheStore({ maxEntries: 100 }),
ttlMs: 60_000,
});
const songs = await cache.json("songs", () => fetchSongs());
const jacket = await cache.bytes("jacket:114", () => fetchJacket(114));构造函数
构造函数
ts
new DatabaseCache(options): DatabaseCache;定义于: packages/database/src/cache.ts:317
参数
| 参数 | 类型 |
|---|---|
options | DatabaseCacheOptions |
返回
DatabaseCache
方法
bytes()
ts
bytes(key, load): Promise<Uint8Array<ArrayBufferLike>>;定义于: packages/database/src/cache.ts:354
读取二进制缓存;未命中时调用 load 并写回。
参数
| 参数 | 类型 |
|---|---|
key | string |
load | () => Promise<Uint8Array<ArrayBufferLike>> |
返回
Promise<Uint8Array<ArrayBufferLike>>
json()
调用签名
ts
json<T>(key, load): Promise<T>;定义于: packages/database/src/cache.ts:324
读取 JSON 缓存;未命中时调用 load 并写回。
类型参数
| 类型参数 |
|---|
T |
参数
| 参数 | 类型 |
|---|---|
key | string |
load | () => Promise<T> |
返回
Promise<T>
调用签名
ts
json<T>(
key,
load,
decode): Promise<T>;定义于: packages/database/src/cache.ts:326
读取 JSON 缓存,并用 decode 校验缓存命中与新加载值。
类型参数
| 类型参数 |
|---|
T |
参数
| 参数 | 类型 |
|---|---|
key | string |
load | () => Promise<unknown> |
decode | (value) => T |
返回
Promise<T>
