# airbuild-cache

> 在多次云端构建之间保留 node_modules 或包存储等文件，并列出或删除团队已存储的缓存。

Source: https://air.build/cn/docs/cli/airbuild-cache/

`airbuild-cache` 在多次云端构建之间保留文件。每次云端构建都从一台全新的构建机开始；脚本用 `airbuild-cache` 恢复某个目录时，会取回上一次构建留下的那份副本，脚本结束时 AirBuild 会重新保存它。

| 在哪里 | 可用的功能 |
| --- | --- |
| 云端 `runbuild`、`runtest`、`runmacos` 或 `runlinux` 的脚本中 | 所有子命令。`airbuild-cache` 在脚本的 `PATH` 中。 |
| 云端团队的环境中（构建之外） | `list`、`delete` 和 `hash`。 |
| Mac 团队 | 没有构建机缓存：Mac 自己保留文件。`restore` 和 `save` 会输出 `no Builder cache here; … skipped.`，不做任何事。 |

## 用法

```bash
airbuild-cache restore --key KEY [--restore-key PREFIX]... [--shared] --path PATH...
airbuild-cache save --key KEY [--shared] --path PATH...
airbuild-cache hash FILE...
airbuild-cache list [--prefix STORED-PREFIX]
airbuild-cache delete --key STORED-KEY | --prefix STORED-PREFIX | --all
airbuild-cache --help
```

## 子命令

| 子命令 | 作用 |
| --- | --- |
| `restore` | 恢复 `KEY` 的最新副本；没有的话，恢复键以某个 `--restore-key` 开头的最新条目。它会记住这个键及其路径：脚本结束后，无论成功还是失败，运行都会把这些路径保存为 `KEY` 的新副本，每次都会。一个脚本只需要一条 `restore`。 |
| `save` | 立即把路径存到 `KEY` 下，除非 `KEY` 已存在（一个键的内容永远不会改变）。对于已被 `restore` 记住的键，它什么也不做。用于没有被恢复过的文件。 |
| `hash` | 输出文件名和内容的简短摘要（16 个十六进制字符）。用它生成随锁文件变化而变化的键。缺失的文件也会计入（以“缺失”的状态）。 |
| `list` | 显示团队的条目，最近使用的排在前面。 |
| `delete` | 删除一个已存储的键、某个已存储前缀下的所有条目，或团队的全部条目。 |

## 选项

| 选项 | 用于 | 取值 | 作用 |
| --- | --- | --- | --- |
| `--key` | `restore`、`save` | 键 | 键名：1 到 200 个 `A-Z a-z 0-9 . _ - + = : @` 中的字符。不能是 `linux`。必填。 |
| `--key` | `delete` | 已存储的键 | `list` 显示的键。 |
| `--restore-key` | `restore` | 前缀 | 备选：键以它开头的最新条目。可重复多次，按顺序尝试。 |
| `--path` | `restore`、`save` | 路径 | 要保留的文件或目录。绝对路径、`~/…`，或相对于当前目录的路径。加引号的 `*` 在保存时才匹配，所以可以指向尚不存在的文件。可重复多次，至少一个。 |
| `--shared` | `restore`、`save` | — | 使用团队的键（团队所有项目都能看到），而不是项目的键。 |
| `--prefix` | `list`、`delete` | 已存储的前缀 | 只处理已存储键以它开头的条目。 |
| `--all` | `delete` | — | 团队的所有条目。 |

`delete` 只能接受 `--key`、`--prefix`、`--all` 中的一个。

## 键及其存储位置

除非传入 `--shared`，键都属于项目。`list` 显示已存储的键：

| 已存储的键 | 内容 |
| --- | --- |
| `p/<project-id>/KEY/-<time>` | 项目的 `KEY` 副本 |
| `t/KEY/-<time>` | `--shared`（团队）副本 |
| `p/<project-id>/linux/…`、`t/linux/…` | 同上，但保存在 Linux 构建机上 |

macOS 和 Linux 构建机在同一个键下保存各自独立的条目。每个键只保留最新的副本：新副本上传完成后旧副本就会被删除，最迟在下一次恢复该键时删除。以 `p/<project-id>/` 作为前缀可以清除整个项目的缓存。

## AirBuild 自动保留的缓存

以下缓存不需要 `airbuild-cache`：

- **DerivedData（Mac）：** 无论在 `runbuild`、`runtest` 还是 `runmacos` 中，一次运行里第一次 `airbuild-xcode install` 或 `test` 都会恢复项目的 DerivedData，脚本结束后运行会保存它。`archive` 不保留缓存。
- **Gradle（Linux）：** `runbuild` 和 `runtest` 会自行恢复 Gradle 的缓存。`runlinux` 除非脚本要求，否则不恢复任何内容。

在 `runbuild`、`runtest`、`runmacos` 或 `runlinux` 上使用 `--no-cache`，会在单次运行中关闭 `restore` 和 `save`。

## 输出

`restore` 和 `save` 在标准错误输出中报告，每条一行，以 `[airbuild-cache]` 开头：

```text
[airbuild-cache] node-1a2b3c4d5e6f7a8b: miss.
[airbuild-cache] node-1a2b3c4d5e6f7a8b: restored (exact, 210 MB, 4.2 s).
[airbuild-cache] node-1a2b3c4d5e6f7a8b: restored (prefix from node-0f9e8d7c6b5a4f3e, 205 MB, 4.0 s).
[airbuild-cache] vendor: copied (80 MB, 0.3 s); uploads after the Build reports.
[airbuild-cache] vendor: already cached, not saved again.
[airbuild-cache] vendor: nothing to save (no listed path exists).
[airbuild-cache] caching is off for this run (--no-cache); restore skipped.
```

保存的上传发生在构建报告结果之后，所以永远不会拖慢结果。

`list` 输出一行摘要和一个表格：

```text
Team cache: 12 entries, 3.4 GB of <quota>.
KEY                                              SIZE  LAST USED
p/<project-id>/node-1a2b3c4d5e6f7a8b/-1763460000  210 MB  2026-11-18 09:12
t/tools/-1763400000                               80 MB  2026-11-17 16:40 (uploading)
```

条目多于显示的数量时，会加上 `(the 500 most recently used; narrow with --prefix)`。`delete` 输出 `[airbuild-cache] deleted N entries (<size>).`

## 退出码和错误

缓存问题永远不会让脚本失败：下载失败、副本损坏或上传被拒，都只会输出一行 `warning:`，`restore` 或 `save` 仍以 `0` 退出。

| 退出码 | 含义 |
| --- | --- |
| `0` | 完成，或已跳过并给出提示。 |
| `1` | `list` 或 `delete` 收到错误响应（`the cache answered HTTP …`）。 |
| `64` | 参数有误；消息会指出是哪个。 |

| 消息 | 怎么办 |
| --- | --- |
| `--key is required: 1-200 of A-Z a-z 0-9 . _ - + = : @.` | 传入有效的 `--key`。 |
| `linux is reserved for the Linux Builder's entries; pick another key.` | 换一个键。 |
| `name at least one --path.` | 添加 `--path`。 |
| `--restore-key needs a value and applies to restore.` | 只在 `restore` 中使用。 |
| `delete: name exactly one of --key, --prefix or --all.` | 只传一个选择条件。 |
| `list takes --prefix only.` | 从 `list` 中去掉 `--key` / `--all`。 |
| `hash: name at least one file.` | 传入一个文件。 |
| `no Builder cache here: list and delete work in cloud Build scripts and in a cloud Team's Environment.` | 在云端团队中运行。 |
| `this Team has no Builder cache.` | 还没有存储任何内容。 |

## 示例

```bash
# Keep node_modules, keyed on the lockfile, falling back to any older copy
airbuild-cache restore --key "node-$(airbuild-cache hash bun.lock)" --restore-key node- --path node_modules
bun install
```

```bash
# Share a package store across all of the Team's Projects
airbuild-cache restore --key pnpm-store --shared --path ~/.local/share/pnpm/store
```

```bash
# See what a Project has stored, then clear it
airbuild-cache list --prefix p/3f6c1a2e-8b4d-4f0a-9c1e-2d7b5a6e9f10/
airbuild-cache delete --prefix p/3f6c1a2e-8b4d-4f0a-9c1e-2d7b5a6e9f10/
```
