@gmod/abortable-promise-cache

中文文档
3.0.4发布于 2025/05/14MITTypeScriptESM + CJS零依赖近 30 天下载 5.4万

add AbortController support to a cache of async requests

原始使用文档
文档目录6 节
  1. Install
  2. Usage
  3. API
  4. Table of Contents
  5. Academic Use
  6. License

@gmod/abortable-promise-cache

NPM version Build Status

Adds AbortController/AbortSignal semantics to a cache of promises. Each get from the cache can optionally take an AbortSignal object that lets that request be aborted.

Cached fill requests will be aborted and evicted from the cache if all the incoming requests for it are aborted before the promise settles.

If the fill request has already settled before all the requests for it have been aborted, it will stay in the cache.

Install

$ npm install --save @gmod/abortable-promise-cache

Usage

import AbortablePromiseCache from '@gmod/abortable-promise-cache'
import QuickLRU from 'quick-lru'

const cache = new AbortablePromiseCache({
  // QuickLRU is a good backing cache to use, but you can use any
  // cache as long as it supports `get`, `set`, `delete`, and `keys`.
  cache: new QuickLRU({ maxSize: 1000 }),

  // the `fill` callback will be called for a cache miss
  async fill(requestData, abortSignal) {
    // do some long-running thing
    return longRunningThing(requestData, abortSignal)
  },
})

// Make a cached request. The returned promise will abort with the given abort signal if
// there is not already a cached copy that has been resolved.
// Fill requests will be signaled to abort if all the requests for them
// so far have been aborted.
const aborter = new AbortController()
const result = await cache.get('some key', { ...anyStuff }, aborter.signal)

// deleting and clearing will abort any outstanding requests
cache.delete('some key')
cache.clear()

API

Table of Contents

Academic Use

This package was written with funding from the NHGRI as part of the JBrowse project. If you use it in an academic project that you publish, please cite the most recent JBrowse paper, which will be linked from jbrowse.org.

License

MIT © Robert Buels

版本与发布记录

@gmod/abortable-promise-cache 共发布 6 个版本,首次发布于 2024/07/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.4latest2025/05/141.5 年前
3.0.32025/04/301.5 年前
3.0.22025/04/301.5 年前
3.0.12025/04/301.5 年前
3.0.02025/04/301.5 年前
2.0.12024/07/232 年前

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

@gmod/abortable-promise-cache 是什么?

add AbortController support to a cache of async requests

如何安装 @gmod/abortable-promise-cache?

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

@gmod/abortable-promise-cache 支持 TypeScript 吗?

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

@gmod/abortable-promise-cache 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@gmod/abortable-promise-cache 可以商用吗?

@gmod/abortable-promise-cache 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@gmod/abortable-promise-cache 的下载量有多少?

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

@gmod/abortable-promise-cache 还在维护吗?

最新版本 3.0.4 发布于 2025/05/14(1.5 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/07/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。