cacache

中文文档
21.0.1发布于 2026/06/10ISCCommonJS构建来源已验证近 30 天下载 1.5亿

快速,容错,跨平台,基于磁盘,数据不可知,内容可处理缓存.

中文使用文档
文档目录6 节
  1. 安装
  2. 目录
  3. 示例
  4. 特征
  5. 捐款
  6. API 密码

缓存 npm version license Travis AppVeyor Coverage Status

cacache 是一个用于管理 Node.js 库 本地密钥和内容地址缓存。 真的很快,真的很擅长 货币,它永远不会给你损坏的数据,即使缓存文件 腐败或操纵.

关于支持文件用户和组设置的系统, cacache 将 符合 uid 财务报告和审定财务报表 gid 缓存所在文件夹的值,甚至 当运行为 root。 。 。 .

它被写成 无本地缓存, 但可以 同样容易被自己使用.

安装

$ npm install --save cacache

目录

示例

const cacache = require('cacache')
const fs = require('fs')

const cachePath = '/tmp/my-toy-cache'
const key = 'my-unique-key-1234'

// Cache it! Use `cachePath` as the root of the content cache
cacache.put(cachePath, key, '10293801983029384').then(integrity => {
  console.log(`Saved content to ${cachePath}.`)
})

const destination = '/tmp/mytar.tgz'

// Copy the contents out of the cache and into their destination!
// But this time, use stream instead!
cacache.get.stream(
  cachePath, key
).pipe(
  fs.createWriteStream(destination)
).on('finish', () => {
  console.log('done extracting!')
})

// The same thing, but skip the key index.
cacache.get.byDigest(cachePath, integrityHash).then(data => {
  fs.writeFile(destination, data, err => {
    console.log('tarball data fetched based on its sha512sum and written out!')
  })
})

特征

  • 按密钥或内容地址提取(shasum等)
  • 分资源 网络标准支持
  • 多随机支持 - 安全主机 sha1, sha512等, 在一个缓存中
  • 自动内容分解
  • 过失容忍(贪污罪、部分写作、过程种族等)
  • 读写的一致性保障(全面数据核实)
  • 无锁高通量缓存访问
  • 流动支助
  • 承诺支持
  • 快速 -- -- 次毫升读取和写出包括校验
  • 任意元数据存储
  • 垃圾收集和其他离线核查
  • 彻底测试覆盖率
  • 里面可能有个开花的过滤器 不错吧

捐款

Cacache团队热情欢迎捐款和项目参与!. 如果你想出力,你们可以做很多事! 如果你愿意的话 请不要犹豫 或问我们问题 如果事情还不清楚.

预计该项目的所有参与者和维护者将遵循 行为守则,只是一般是 优于对方.

请参看 更改日志 还有项目历史细节.

黑客快乐!

API 密码

> cacache.ls(cache) -> Promise<Object>

将缓存中当前所有条目的信息列为单个大对象。 每个 对象中的条目将由唯一的索引密钥键来键入,并对应 get.info 对象作为值.

示例
cacache.ls(cachePath).then(console.log)
// Output
{
  'my-thing': {
    key: 'my-thing',
    integrity: 'sha512-BaSe64/EnCoDED+HAsh=='
    path: '.testcache/content/deadbeef', // joined with `cachePath`
    time: 12345698490,
    size: 4023948,
    metadata: {
      name: 'blah',
      version: '1.2.3',
      description: 'this was once a package but now it is my-thing'
    }
  },
  'other-thing': {
    key: 'other-thing',
    integrity: 'sha1-ANothER+hasH=',
    path: '.testcache/content/bada55',
    time: 11992309289,
    size: 111112
  }
}

> cacache.ls.stream(cache) -> Readable

将缓存中当前所有条目的信息列为单个大对象.

这工作就像 ls时,除 get.info 条目是 返回为 'data' 返回流中的事件.

示例
cacache.ls.stream(cachePath).on('data', console.log)
// Output
{
  key: 'my-thing',
  integrity: 'sha512-BaSe64HaSh',
  path: '.testcache/content/deadbeef', // joined with `cachePath`
  time: 12345698490,
  size: 13423,
  metadata: {
    name: 'blah',
    version: '1.2.3',
    description: 'this was once a package but now it is my-thing'
  }
}

{
  key: 'other-thing',
  integrity: 'whirlpool-WoWSoMuchSupport',
  path: '.testcache/content/bada55',
  time: 11992309289,
  size: 498023984029
}

{
  ...
}

> cacache.get(cache, key, [opts]) -> Promise({data, metadata, integrity})

以缓存数据、摘要和元数据返回对象 key编辑 data 此对象的属性将是 a Buffer 实例 可能有些数据对你很重要 我敢肯定,你知道什么 来对付它! 卡卡切只是不会在乎.

integrity 是一个 次级资源 诚信 绳子。 也就是说,可以用来验证的字符串 data,这看起来像 <hash-algorithm>-<base64-integrity-hash>。 。 。 .

如果没有识别的内容 key,或者如果本地存储的数据确实存在 不通过有效性校验和, 承诺将被拒绝.

一个子功能, get.byDigest 可能用于相同的行为, 但检查除外 将完全绕过指数, 这个版本 函数 仅限 返回时 data 自己,没有包装.

见: 选项

说明

此函数在返回内存之前将整个缓存条目装入内存 。 若为 您正在处理非常大的数据, 考虑使用 get.stream 换句话说.

示例
// Look up by key
cache.get(cachePath, 'my-thing').then(console.log)
// Output:
{
  metadata: {
    thingName: 'my'
  },
  integrity: 'sha512-BaSe64HaSh',
  data: Buffer#<deadbeef>,
  size: 9320
}

// Look up by digest
cache.get.byDigest(cachePath, 'sha512-BaSe64HaSh').then(console.log)
// Output:
Buffer#<deadbeef>

> cacache.get.stream(cache, key, [opts]) -> Readable

回返a 可读流线 以 key。 。 。 .

如果没有识别的内容 key,或者如果本地存储的数据确实存在 无法通过有效性校验和, 错误将被释放 .

metadata 财务报告和审定财务报表 integrity 如果 您需要收集更多关于缓存条目的数据.

一个子功能, get.stream.byDigest 可能被用于相同的行为, 完全绕过索引。 这个 版本不发送 metadata 财务报告和审定财务报表 integrity 完全没有.

见: 选项

示例
// Look up by key
cache.get.stream(
  cachePath, 'my-thing'
).on('metadata', metadata => {
  console.log('metadata:', metadata)
}).on('integrity', integrity => {
  console.log('integrity:', integrity)
}).pipe(
  fs.createWriteStream('./x.tgz')
)
// Outputs:
metadata: { ... }
integrity: 'sha512-SoMeDIGest+64=='

// Look up by digest
cache.get.stream.byDigest(
  cachePath, 'sha512-SoMeDIGest+64=='
).pipe(
  fs.createWriteStream('./x.tgz')
)

> cacache.get.info(cache, key) -> Promise

抬头看 key 在缓存索引中,如果 一个存在.

字段
  • key - 钥匙,入口被放在下面 匹配 key 参数.
  • integrity - 说吧 分资源 对于本条目所指的内容.
  • path - 内容存储、加入的文件系统路径 cache 参数.
  • time - 时间戳是先加的.
  • metadata - 与条目/内容有关的用户指定元数据.
示例
cacache.get.info(cachePath, 'my-thing').then(console.log)

// Output
{
  key: 'my-thing',
  integrity: 'sha256-MUSTVERIFY+ALL/THINGS=='
  path: '.testcache/content/deadbeef',
  time: 12345698490,
  size: 849234,
  metadata: {
    name: 'blah',
    version: '1.2.3',
    description: 'this was once a package but now it is my-thing'
  }
}

> cacache.get.hasContent(cache, integrity) -> Promise

看起来像一个 分资源 在缓存。 如果内容 已存在此选项 integrity,它将返回一个对象,带有特定的单一完整性散列 发现于 sri 键,并查找内容大小为 size。如果没有关于这种完整性的内容,它将返回 false。 。 。 .

示例
cacache.get.hasContent(cachePath, 'sha256-MUSTVERIFY+ALL/THINGS==').then(console.log)

// Output
{
  sri: {
    source: 'sha256-MUSTVERIFY+ALL/THINGS==',
    algorithm: 'sha256',
    digest: 'MUSTVERIFY+ALL/THINGS==',
    options: []
  },
  size: 9001
}

cacache.get.hasContent(cachePath, 'sha521-NOT+IN/CACHE==').then(console.log)

// Output
false
选项
opts.integrity

如果存在,插入内容的预计算摘要. 如果这个选项 已提供,且与插入后摘要不符,插入失败 带有 EINTEGRITY 错误.

opts.memoize

默认: 无效

如果明确真实,cacache会从内存中读取并记忆到批量读取的数据. 若为 false,cacache将从磁盘数据读取。 默认读取器功能从内存缓存读取.

opts.size

如果提供,将核查数据流,以检查是否有足够的数据 通过。 如果数据比预期多或少,插入就会失败 带有 EBADSIZE 错误.

> cacache.put(cache, key, data, [opts]) -> Promise

将传递给它的数据插入到缓存中。 回归的诺言以 文摘(根据 opts.algorithms在 缓存条目已成功写入 .

见: 选项

示例
fetch(
  'https://registry.npmjs.org/cacache/-/cacache-1.0.0.tgz'
).then(data => {
  return cacache.put(cachePath, 'registry.npmjs.org|[email protected]', data)
}).then(integrity => {
  console.log('integrity hash is', integrity)
})

> cacache.put.stream(cache, key, [opts]) -> Writable

回返a 可写 流线 插入时 输入到缓存中的数据。 输出 a integrity 附带摘要的事件 成功时写入内容.

见: 选项

示例
request.get(
  'https://registry.npmjs.org/cacache/-/cacache-1.0.0.tgz'
).pipe(
  cacache.put.stream(
    cachePath, 'registry.npmjs.org|[email protected]'
  ).on('integrity', d => console.log(`integrity digest is ${d}`))
)
选项
opts.metadata

将任意元数据附加在插入的密钥上 .

opts.size

如果提供,将核查数据流,以检查是否有足够的数据 通过。 如果数据比预期多或少,插入就会失败 带有 EBADSIZE 错误.

opts.integrity

如果存在,插入内容的预计算摘要. 如果这个选项 已提供,且与插入后摘要不符,插入失败 带有 EINTEGRITY 错误.

algorithms 如果存在此选项,则无效.

opts.integrityEmitter

仅流出 如果存在,则使用所提供的事件发射器作为源 诚实和大小 这允许使用完整性的情况 已计算在缓存之外重用该数据 算它第二次.

发射者必须同时发射 'integrity' 财务报告和审定财务报表 'size' 事件.

注:如果提供了这一选项,则必须证明您收到正确的选项 忠诚重视自己并释放 'error' 如果出现不匹配,则出现事件. ssri 诚信流 当你们达到应有的境界的时候,你们要达到这个目的.

opts.algorithms

默认 : ['sha512']

计算时使用的哈兴算法 分资源完整性 摘要 对于插入的数据。 可以使用列表中的任何算法 crypto.getHashes() 或者说 'omakase'编号 :'お任せします' 在每次插入时选择随机散列算法。 你个 也可以使用任何动画 'modnar' 用于此特性.

目前只支持一次一个算法(即数组长度为 没错 1) (中文(简体) ). 如果 opts.integrity 已存在.

opts.memoize

默认: 无效

如果提供, cacache 将提醒给定的缓存插入内存, 绕过 未来缓存中对该密钥或摘要的任何文件系统检查。 没什么 除非此选项明确属实,否则将写入内存.

若为 opts.memoize 是对象或对象 Map- 喜欢(即,一个物体与 get 财务报告和审定财务报表 set 而不是全球记忆 缓存.

从磁盘上读取数据可强制通过 memoize: false 改为 读取器的功能,但他们的默认是从内存读取.

opts.tmpPrefix

默认: 无效

在缓存 tmp dir 内的临时目录名称上附加的前缀 .

> cacache.rm.all(cache) -> Promise

清除整个缓存。 主要通过吹走缓存目录本身.

示例
cacache.rm.all(cachePath).then(() => {
  console.log('THE APOCALYPSE IS UPON US 😱')
})

> cacache.rm.entry(cache, key, [opts]) -> Promise

别名: cacache.rm

删除索引条目 key如果 由内容地址直接请求(get.stream.byDigest) (中文(简体) ).

默认情况下,此选项附加到索引中的新条目,其完整性为: null。 。 。 . 若为 opts.removeFully 设置为 true 那么索引文件本身将是 实际删除,而不是附加 null。 。 。 .

删除内容本身(其他条目可能仍然使用),使用 rm.content。或者,为了安全地清除任何未使用的内容,使用 verify。 。 。 .

示例
cacache.rm.entry(cachePath, 'my-thing').then(() => {
  console.log('I did not like it anyway')
})

> cacache.rm.content(cache, integrity) -> Promise

删除识别的内容 integrity。任何与之相关的索引条目 在内容重新添加到缓存中之前无法再次使用 同样的文摘.

示例
cacache.rm.content(cachePath, 'sha512-SoMeDIGest/IN+BaSE64==').then(() => {
  console.log('data for my-thing is gone!')
})

> cacache.index.compact(cache, key, matchFn, [opts]) -> Promise

用途 matchFn,它必须是接受两个条目的同步函数 返回布尔表示两个条目是否匹配,以便 为给定的缓存复制所有条目 key。 。 。 .

若为 opts.validateEntry 提供后,它将与 参数只是单个索引条目。 函数必须返回布尔, 返回时 true 该条目被视为有效,并将保留在索引中, 返回时 false 该条目将从索引中删除.

若为 opts.validateEntry 然而,没有提供指数中的每一个条目 复制并保存到第一次 null 实现完整性, 删除 全部在 null。 。 。 .

被删减的条目列表同时被写入索引,以取代 已存在的内容,并在“承诺”中返回.

> cacache.index.insert(cache, key, integrity, opts) -> Promise

为给定的缓存写入索引条目 key 不写内容.

假设您正在使用这种方法, 您已经存储了内容 以其他方式,你只想在内容中添加一个新的索引。 这个 metadata 财务报告和审定财务报表 size 属性读取自 opts 并用作索引条目的一部分.

A 向新添加的条目返回承诺解析.

> cacache.clearMemoized()

完全重置内存条目缓存 .

> tmp.mkdir(cache, opts) -> Promise<Path>

返回缓存内唯一的临时目录 tmp 目录 。 这个 目录将使用与所有其他东西相同的安全用户指派 .

一旦目录完成,所有文件都是用户的责任 在其中给予适当的 gid编号 :uid 要匹配的所有权设置 其余的缓存。 如果不行的话,你可以叫cacache帮你做 电话 tmp.fix(),它将修正所有 tmp 目录 权限.

如果您想要自动清理此目录, 请使用 tmp.withTmp()

见: 选项

示例
cacache.tmp.mkdir(cache).then(dir => {
  fs.writeFile(path.join(dir, 'blablabla'), Buffer#<1234>, ...)
})

> tmp.fix(cache) -> Promise

设置 uid 财务报告和审定财务报表 gid tmp 中所有文件和文件夹的属性 匹配缓存的文件夹 .

手动写入文件后使用此功能 tmp.mkdir 或者说 tmp.withTmp。 。 。 .

示例
cacache.tmp.mkdir(cache).then(dir => {
  writeFile(path.join(dir, 'file'), someData).then(() => {
    // make sure we didn't just put a root-owned file in the cache
    cacache.tmp.fix().then(() => {
      // all uids and gids match now
    })
  })
})

> tmp.withTmp(cache, opts, cb) -> Promise

创建一个临时目录 tmp.mkdir() 和电话 cb 带着它 返回值时将删除已创建的临时目录 联 合 国 cb() 解析后, tmp 目录将自动被删除 承诺完成.

在管理 tmp dir 权限时, 也适用同样的警告 目录.

见: 选项

示例
cacache.tmp.withTmp(cache, dir => {
  return fs.writeFile(path.join(dir, 'blablabla'), 'blabla contents', { encoding: 'utf8' })
}).then(() => {
  // `dir` no longer exists
})
选项
opts.tmpPrefix

默认: 无效

在缓存 tmp dir 内的临时目录名称上附加的前缀 .

分资源廉正文摘

对于内容的校验和地址,cacache使用字符串跟踪 次级资源 诚信谱。 。 。 . 即,任何时间cacache期望一个 integrity 参数或选项,它 应采用格式 <hashAlgorithm>-<base64-hash>。 。 。 .

与当前规格的一个偏差是cacache会支持任意散列 由基础节点.js进程支持的算法. 你用得着 crypto.getHashes() 看看你能用到哪个.

制作摘要

如果您有已有的内容 shasum , 它们一般被格式化为 六进制字符串(即:sha1会看起来像: 5f5513f8822fdbe5145af33b64d8d970dcf95c6e) (中文(简体) ). 为了兼容 cacache,你需要把这个转换成 相当的子资源完整性 绳子。 例如,相应的散列将是: sha1-X1UT+IIv2+UUWvM7ZNjZcNz5XG4=。 。 。 .

如果您想要为已有数据创建完整字符串, 您可以 用这样的东西:

const crypto = require('crypto')
const hashAlgorithm = 'sha512'
const data = 'foobarbaz'

const integrity = (
  hashAlgorithm +
  '-' +
  crypto.createHash(hashAlgorithm).update(data).digest('base64')
)

您还可以使用 ssri 拥有一套更丰富的功能 环绕SRI字符串,包括生成、解析和从现有 六进制字符串 .

> cacache.verify(cache, opts) -> Promise

检查并修正您的缓存 :

  • 清理已损坏或无效的索引条目.
  • 自定义条目过滤选项 .
  • 垃圾收集索引未引用的任何内容条目.
  • 检查所有内容条目的完整性并删除无效内容.
  • 修复缓存所有权 .
  • 删除 tmp 缓存中的目录及其全部内容.

完成后,它会返回一个有各种数据记录的物体 程序,包括重新封存的数量、有效条目的数量、数量 已删除的条目等.

选项
opts.concurrency

默认: 20

在清理时同时读取文件的数量 .

opts.filter

接收格式化条目。 归假取去. 注:在同一条目中,可能不止一次被称作.

opts.log

自定义日志函数 :

  log: { silly () {} }
  log.silly('verify', 'verifying cache at', cache)
示例
echo somegarbage >> $CACHEPATH/content/deadbeef
cacache.verify(cachePath).then(stats => {
  // deadbeef collected, because of invalid checksum.
  console.log('cache is much nicer now! stats:', stats)
})

> cacache.verify.lastRun(cache) -> Promise

回返a Date 代表最后一次 cacache.verify 运行在 cache。 。 。 .

示例
cacache.verify(cachePath).then(() => {
  cacache.verify.lastRun(cachePath).then(lastTime => {
    console.log('cacache.verify was last called on' + lastTime)
  })
})

版本与发布记录

cacache 共发布 110 个版本,首次发布于 2016/11/18;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
21.0.1latest2026/06/104 个月前
21.0.02026/05/195 个月前
20.0.42026/03/197 个月前
20.0.32025/11/2211 个月前
20.0.22025/11/1811 个月前
20.0.12025/08/191.2 年前

其他发布通道:legacy → 12.0.4。

关于 cacache 的常见问题

cacache 是什么?

快速,容错,跨平台,基于磁盘,数据不可知,内容可处理缓存.

如何安装 cacache?

在项目目录运行 npm install cacache,或使用 pnpm add / yarn add / bun add。

cacache 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cacache 是否存在。

cacache 支持 ESM 还是 CommonJS?

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

cacache 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^22.22.2 || ^24.15.0 || >=26.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

cacache 可以商用吗?

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

cacache 的下载量有多少?

根据 npm 官方下载统计,cacache 在 2026/10/05 – 2026/11/03 这 30 天内被下载 145,894,923 次,日均约 4,863,164 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

cacache 还在维护吗?

最新版本 21.0.1 发布于 2026/06/10(4 个月前);至今共发布 110 个版本,其中过去 12 个月发布了 5 个;首次发布于 2016/11/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。