@actions/cache

中文文档
6.3.0发布于 2026/09/28MITTypeScriptES Module构建来源已验证近 30 天下载 125.6万

Actions cache lib

原始使用文档

@actions/cache

Functions necessary for caching dependencies and build outputs to improve workflow execution time.

See "Caching dependencies to speed up workflows" for how caching works.

Note that GitHub will remove any cache entries that have not been accessed in over 7 days. There is no limit on the number of caches you can store, but the total size of all caches in a repository is limited to 10 GB. If you exceed this limit, GitHub will save your cache but will begin evicting caches until the total size is less than 10 GB.

⚠️ Important changes

The cache backend service has been rewritten from the ground up for improved performance and reliability. The @actions/cache package now integrates with the new cache service (v2) APIs.

The new service will gradually roll out as of February 1st, 2025. The legacy service will also be sunset on the same date. Changes in this release are fully backward compatible.

All previous versions of this package will be deprecated. We recommend upgrading to version 4.0.0 as soon as possible before February 1st, 2025.

If you do not upgrade, all workflow runs using any of the deprecated @actions/cache packages will fail.

Upgrading to the recommended version should not break or require any changes to your workflows beyond updating your package.json to version 4.0.0.

Read more about the change & access the migration guide: reference to the announcement.

Usage

This package is used by the v2+ versions of our first party cache action. You can find an example implementation in the cache repo here.

Save Cache

Saves a cache containing the files in paths using the key provided. The files would be compressed using zstandard compression algorithm if zstd is installed, otherwise gzip is used. Function returns the cache id if the cache was saved succesfully and throws an error if cache upload fails.

const cache = require('@actions/cache');
const paths = [
    'node_modules',
    'packages/*/node_modules/'
]
const key = 'npm-foobar-d5ea0750'
const cacheId = await cache.saveCache(paths, key)

Restore Cache

Restores a cache based on key and restoreKeys to the paths provided. Function returns the cache key for cache hit and returns undefined if cache not found.

const cache = require('@actions/cache');
const paths = [
    'node_modules',
    'packages/*/node_modules/'
]
const key = 'npm-foobar-d5ea0750'
const restoreKeys = [
    'npm-foobar-',
    'npm-'
]
const cacheKey = await cache.restoreCache(paths, key, restoreKeys)
Cache segment restore timeout

A cache gets downloaded in multiple segments of fixed sizes (now 128MB to fail-fast, previously 1GB for a 32-bit runner and 2GB for a 64-bit runner were used). Sometimes, a segment download gets stuck which causes the workflow job to be stuck forever and fail. Version v3.0.4 of cache package introduces a segment download timeout. The segment download timeout will allow the segment download to get aborted and hence allow the job to proceed with a cache miss.

Default value of this timeout is 10 minutes (starting v3.2.1 and higher, previously 60 minutes in versions between v.3.0.4 and v3.2.0, both included) and can be customized by specifying an environment variable named SEGMENT_DOWNLOAD_TIMEOUT_MINS with timeout value in minutes.

版本与发布记录

@actions/cache 共发布 64 个版本,首次发布于 2020/05/16;过去 12 个月发布了 14 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
6.3.0latest2026/09/281 个月前
5.3.0v5-commonjs2026/09/281 个月前
5.2.02026/07/163 个月前
6.2.02026/07/143 个月前
5.1.02026/06/194 个月前
6.1.02026/06/194 个月前

关于 @actions/cache 的常见问题

@actions/cache 是什么?

Actions cache lib

如何安装 @actions/cache?

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

@actions/cache 支持 TypeScript 吗?

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

@actions/cache 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@actions/cache 可以商用吗?

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

@actions/cache 的下载量有多少?

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

@actions/cache 还在维护吗?

最新版本 6.3.0 发布于 2026/09/28(1 个月前);至今共发布 64 个版本,其中过去 12 个月发布了 14 个;首次发布于 2020/05/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。