Skip to main content

缓存任务结果

¥Cache Task Results

当涉及到运行任务、缓存等时,Lerna 和 Nx 可以互换使用。当我们说 "Lerna 可以缓存构建" 时,我们的意思是 Lerna 使用可以缓存构建的 Nx。

¥When it comes to running tasks, caching etc., Lerna and Nx can be used interchangeably. When we say "Lerna can cache builds", we mean that Lerna uses Nx which can cache builds.

一遍又一遍地重建和重新测试相同的代码成本高昂。Lerna 使用计算缓存来避免两次重建相同的代码。

¥It's costly to rebuild and retest the same code over and over again. Lerna uses a computation cache to never rebuild the same code twice.

设置

¥Setup

Lerna via Nx 拥有最复杂且经过实战考验的计算缓存系统。它知道你要运行的任务之前已经执行过,因此它可以使用缓存来恢复运行该任务的结果。

¥Lerna via Nx has the most sophisticated and battle-tested computation caching system. It knows when the task you are about to run has been executed before, so it can use the cache to restore the results of running that task.

提示

如果没有 nx.json,请运行 npx lerna add-caching

¥If you don't have nx.json, run npx lerna add-caching.

要启用 buildtest 的缓存,请编辑 nx.json 中的 targetDefaults 属性以包含 buildtest 任务:

¥To enable caching for build and test, edit the targetDefaults property in nx.json to include the build and test tasks:

nx.json
{
"targetDefaults": {
"build": {
"cache": true
},
"test": {
"cache": true
}
}
}
信息

请注意,可缓存操作需要无副作用,这意味着给定相同的输入,它们应该始终产生相同的输出。例如,无法缓存命中后端 API 的 e2e 测试运行,因为后端可能会影响测试运行的结果。

¥Note, cacheable operations need to be side effect free, meaning that given the same input they should always result in the same output. As an example, e2e test runs that hit the backend API cannot be cached as the backend might influence the result of the test run.

现在,运行以下命令两次。第二次操作将是即时的:

¥Now, run the following command twice. The second time the operation will be instant:

lerna run build --scope=header
Terminal Output
> lerna run build --scope=header

> header:build [existing outputs match the cache, left as is]

> header@0.0.0 build
> rimraf dist && rollup --config


src/index.tsx → dist...
created dist in 858ms

—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

Lerna (powered by Nx) Successfully ran target test for project header (4ms)

Nx read the output from the cache instead of running the command for 1 out of 1 tasks.

从缓存重播

¥Replaying from Cache

当 Lerna 确定任务的输入未更改时,它会重新创建该任务的输出,就像它实际在你的计算机上运行一样 - 但速度更快。缓存任务的输出包括终端输出和在为该任务定义的 output 目录中创建的文件。

¥When Lerna determines that the inputs for a task have not changed, it recreates the outputs of that task as if it actually ran on your machine - but much faster. The outputs of a cached task include both the terminal output and the files created in the defined output directories for that task.

你可以通过删除 header:build 任务输出到的 dist 文件夹然后再次运行 lerna run build --scope=header 来测试这一点。缓存的任务将立即重播,并且正确的文件将出现在 dist 文件夹中。

¥You can test this out by deleting the dist folder that the header:build task outputs to and then running lerna run build --scope=header again. The cached task will replay instantly and the correct files will be present in the dist folder.

header/
└── dist/ <-- this folder gets recreated

如果你的任务在不同位置创建输出工件,你可以 更改输出文件夹 缓存这些工件。如果缓存发生更改,你还可以 自定义哪些输入 使缓存失效。

¥If your task creates output artifacts in a different location, you can change the output folder(s) that are cached. You can also customize which inputs will invalidate the cache if they are changed.

高级缓存

¥Advanced Caching

要更深入地了解缓存实现并微调存储库的缓存,请阅读 缓存的工作原理

¥For a more in-depth understanding of the caching implementation and to fine-tune the caching for your repo, read How Caching Works.

本地计算缓存

¥Local Computation Caching

默认情况下,Lerna(通过 Nx)使用本地计算缓存。Nx 仅将缓存值存储一周,之后将被删除。要清除缓存,请运行 nx reset,Nx 将在下次尝试访问它时创建一个新缓存。

¥By default, Lerna (via Nx) uses a local computation cache. Nx stores the cached values only for a week, after which they are deleted. To clear the cache run nx reset, and Nx will create a new one the next time it tries to access it.