# @ardrive/ardrive-promise-cache 中文文档

规范地址：https://npmcn.com/package/%40ardrive/ardrive-promise-cache/

版本：1\.5\.0
npm 元数据更新时间：2026-07-15T03:49:01.347Z
中文译文更新时间：2026-11-06T19:50:20.457Z
软件包许可：AGPL-3\.0-or-later

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40ardrive/ardrive-promise-cache
原文对照：https://npmcn.com/package/%40ardrive/ardrive-promise-cache/?lang=original
项目主页：https://github.com/ardriveapp/promise-cache#readme
源码仓库：https://github.com/ardriveapp/promise-cache

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：28.1 kB，11 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：2 个
- 首次发布：2023-08-01T18:20:34.150Z
- 最新版本发布：2025-11-04T19:07:02.303Z
- 近 30 天下载量：2430（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：34（近 12 个月 0 个）

## 中文简介

承诺缓存工具 .

## 安装

```sh
npm install @ardrive/ardrive-promise-cache
```

也可以使用：`pnpm add @ardrive/ardrive-promise-cache`、`yarn add @ardrive/ardrive-promise-cache`、`bun add @ardrive/ardrive-promise-cache`

## README

## @ ardrive/ ardrive- 保证- cache

`@ardrive/ardrive-promise-cache` 是一个缓存库,旨在缓存承诺,从而能够更快地随后检索数据。 它包括两种类型的缓存执行:

-   [承诺缓存](https://npmcn.com/package/%40ardrive/ardrive-promise-cache/#readme-promisecache) - 一个简单的缓存,在记忆中存储承诺
-   [通过Promise缓存读取](https://npmcn.com/package/%40ardrive/ardrive-promise-cache/#readme-readthroughpromisecache) - 包装 `PromiseCache` 允许提供合成 `readThroughFunction` 当缓存未包含请求的密钥时调用

## 安装

您可以使用 npm 或纱线安装库 :

```bash
npm i @ardrive/ardrive-promise-cache
```

```bash
yarn add @ardrive/ardrive-promise-cache
```

## 使用量

### 承诺缓存

#### 示例

```typescript
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缓存读取

通过缓存读取对高吞吐量系统有用. 如果缓存中包含请求的数据,则立即返回. 如果数据不存在(或已过期),缓存会获取并放置由此产生的承诺,用于未来的请求. 通过缓存读取的一些实例和使用例(和其他缓存模式) [这里](https://www.prisma.io/dataguide/managing-databases/introduction-database-caching#read-through)。 。 。 .

#### 示例

```typescript
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` 执行。 如果遇到问题, 您可能需要推翻这种方法 .

## 常见问题

### @ardrive/ardrive-promise-cache 是什么？

承诺缓存工具 \.

### 如何安装 @ardrive/ardrive-promise-cache？

在项目目录运行 npm install @ardrive/ardrive-promise-cache，或使用 pnpm add / yarn add / bun add。

### @ardrive/ardrive-promise-cache 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @ardrive/ardrive-promise-cache 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @ardrive/ardrive-promise-cache 可以商用吗？

@ardrive/ardrive-promise-cache 使用 AGPL-3\.0-or-later 许可。最严格的 Copyleft：除分发外，通过网络向用户提供基于它的服务也需要开源对应源码。闭源 SaaS 使用前务必评估。以上为概括，不构成法律意见，请以许可原文为准。

### @ardrive/ardrive-promise-cache 的下载量有多少？

根据 npm 官方下载统计，@ardrive/ardrive-promise-cache 在 2026/10/07 – 2026/11/05 这 30 天内被下载 2,430 次，日均约 81 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @ardrive/ardrive-promise-cache 还在维护吗？

最新版本 1\.5\.0 发布于 2025/11/05（1 年前）；至今共发布 34 个版本，其中过去 12 个月发布了 0 个；首次发布于 2023/08/02。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

