# @actions/tool-cache 中文文档

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

版本：4\.0\.0
npm 元数据更新时间：2026-06-11T21:11:13.797Z
中文译文更新时间：2026-11-05T16:58:07.228Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/%40actions/tool-cache
原文对照：https://npmcn.com/package/%40actions/tool-cache/?lang=original
项目主页：https://github.com/actions/toolkit/tree/main/packages/tool-cache
源码仓库：https://github.com/actions/toolkit

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

- 包体积（解压后）：109 kB，14 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：5 个
- 首次发布：2019-08-07T17:33:21.401Z
- 最新版本发布：2026-01-29T16:49:48.639Z
- 近 30 天下载量：2495269（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：28（近 12 个月 3 个）

## 中文简介

动作工具缓存 lib

## 安装

```sh
npm install @actions/tool-cache
```

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

## README

# `@actions/tool-cache`

> 下载和缓存工具所需的功能.

## 使用量

#### 下载

您可以使用它从下载 URL 下载工具( 或其他文件) :

```js
const tc = require('@actions/tool-cache');

const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-linux-x64.tar.gz');
```

#### 摘录

然后可以具体的方式从平台中提取:

```js
const tc = require('@actions/tool-cache');

if (process.platform === 'win32') {
  const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-win-x64.zip');
  const node12ExtractedFolder = await tc.extractZip(node12Path, 'path/to/extract/to');

  // Or alternately
  const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-win-x64.7z');
  const node12ExtractedFolder = await tc.extract7z(node12Path, 'path/to/extract/to');
}
else if (process.platform === 'darwin') {
  const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0.pkg');
  const node12ExtractedFolder = await tc.extractXar(node12Path, 'path/to/extract/to');
}
else {
  const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-linux-x64.tar.gz');
  const node12ExtractedFolder = await tc.extractTar(node12Path, 'path/to/extract/to');
}
```

#### 缓存

最后,你可以把这些目录隐藏在我们的工具笼中. 如果您想要在工具的版本之间前后切换, 或者在运行之间保存一个工具, 供自行托管的跑取者使用, 这将会有用 .

作为这个步骤的一部分,你常常想把它添加到路径中:

```js
const tc = require('@actions/tool-cache');
const core = require('@actions/core');

const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-linux-x64.tar.gz');
const node12ExtractedFolder = await tc.extractTar(node12Path, 'path/to/extract/to');

const cachedPath = await tc.cacheDir(node12ExtractedFolder, 'node', '12.7.0');
core.addPath(cachedPath);
```

也可以缓存文件进行再利用.

```js
const tc = require('@actions/tool-cache');

const cachedPath = await tc.cacheFile('path/to/exe', 'destFileName.exe', 'myExeName', '1.1.0');
```

#### 查找

最后,您可以找到您先前缓存过的目录和文件:

```js
const tc = require('@actions/tool-cache');
const core = require('@actions/core');

const nodeDirectory = tc.find('node', '12.x', 'x64');
core.addPath(nodeDirectory);
```

您甚至可以找到工具的所有缓存版本 :

```js
const tc = require('@actions/tool-cache');

const allNodeVersions = tc.findAllVersions('node');
console.log(`Versions of node available: ${allNodeVersions}`);
```

## 常见问题

### @actions/tool-cache 是什么？

动作工具缓存 lib

### 如何安装 @actions/tool-cache？

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

### @actions/tool-cache 支持 TypeScript 吗？

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

### @actions/tool-cache 支持 ESM 还是 CommonJS？

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

### @actions/tool-cache 可以商用吗？

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

### @actions/tool-cache 的下载量有多少？

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

### @actions/tool-cache 还在维护吗？

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

