@ardrive/ardrive-promise-cache

中文文档
1.5.0发布于 2025/11/05AGPL-3.0-or-laterTypeScriptCommonJS构建来源已验证近 30 天下载 2430

A promise cache utility.

原始使用文档
文档目录7 节
  1. @ardrive/ardrive-promise-cache
  2. Installation
  3. Usage
  4. PromiseCache
  5. ReadThroughPromiseCache
  6. API
  7. Note

@ardrive/ardrive-promise-cache

@ardrive/ardrive-promise-cache is a caching library designed to cache promises, enabling faster subsequent retrievals of data. It includes two types of caching implementations:

  • PromiseCache - a simple cache that stores promises in memory
  • ReadThroughPromiseCache - a wrapper of PromiseCache that allows providing an async readThroughFunction that is called when the cache does not contain the requested key

Installation

You can install the library using npm or yarn:

npm i @ardrive/ardrive-promise-cache
yarn add @ardrive/ardrive-promise-cache

Usage

PromiseCache

Example

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)

Creates a new PromiseCache instance with the specified capacity and time-to-live (TTL) for cached items.

put(key: K, value: Promise<V>): Promise<V>

Stores a promise with the specified key in the cache.

get(key: K): Promise<V> | undefined

Retrieves a promise from the cache using the specified key. Returns undefined if the key is not found.

remove(key: K): void

Removes a promise from the cache using the specified key.

clear(): void

Clears the entire cache.

size(): number

Returns the current number of items in the cache.


ReadThroughPromiseCache

A Read Through Cache is useful for high throughput systems. If the cache contains the requested data, it is immediately returned. If the data does not exist (or has expired), the cache will fetch and place resulting promise in the cache for future requests. Some examples and use cases for Read Through Caching (and other caching patterns) can be found here.

Example

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)

Creates a new ReadThroughPromiseCache instance with the specified capacity and time-to-live (TTL) for cached items and readThroughFunction that will be called when the cache does not contain the key.

getWithStatus(key: K): Promise<{status: 'hit' | 'miss', data: V}>

Returns a Promise'd object, with the key 'data' pre-awaited and 'status' key indicating if it was a hit or a miss.

Note

The method cacheKeyString(key: K): string is used internally to create cache keys. The default implementation may not sufficiently differentiate keys for certain object types, depending on their toJSON implementation. You may need to override this method if you encounter issues.

版本与发布记录

@ardrive/ardrive-promise-cache 共发布 34 个版本,首次发布于 2023/08/02;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.5.0latest2025/11/051 年前
1.5.0-alpha.1alpha2025/11/051 年前
1.2.0-alpha.62025/07/021.4 年前
1.2.0-alpha.52025/06/281.4 年前
1.2.0-alpha.42025/06/271.4 年前
1.2.0-alpha.32025/06/271.4 年前

关于 @ardrive/ardrive-promise-cache 的常见问题

@ardrive/ardrive-promise-cache 是什么?

A promise cache utility.

如何安装 @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 和提交记录判断。