Skip to main content

缓存策略

概述

¥Overview

Yarn 拥有一套广泛的缓存设置,让你可以根据你喜欢的工作流程或 CI 平台进行调整。本文档介绍了一些最有趣的模式。

¥Yarn boasts a wide set of cache settings, letting you tweak depending on your preferred workflows or CI platforms. This documentation goes over some of the most interesting patterns to know.

提示

Yarn 默认会缓存你安装的所有内容,并将它们与你机器上的所有其他项目共享;这可以提高安装速度和磁盘占用空间,就像你使用硬链接一样。

¥Yarn will by default cache everything you install and mutualize them for all other projects on your machine; this improves both installation speed and disk footprint, just like if you were using hardlinks.

主要模式

¥Major patterns

离线镜像

¥Offline mirror

首次在机器上安装时,通常会从 npm 注册表中检索包。虽然它通常运行良好,但情况并非总是如此 - 众所周知,注册表有时会遇到问题,这经常导致安装失败。如果你没有做好准备,这可能会给你的开发者带来重大干扰,因为切换分支和执行部署可能会更加困难或不稳定。

¥When installed for the first time on a machine, packages are usually retrieved from the npm registry. While it usually works fine, it's not always the case - the registry is known to experience issues from time to time that often result in failed installs. If you're not prepared, it may be a significant disruption for your developers, as switching branches and performing deploys can be much harder or unstable.

一些公司试图通过将其注册表配置为他们控制的镜像来避免此问题(例如,让服务器运行 Verdaccio,这是 npm 注册表的开源实现)。但是,它需要特定的设置,而这种设置并不总是易于部署给开发者和 CI,而这些系统有时附带 risks

¥Some companies try to avoid this problem by configuring their registry to a mirror they control (for example by having a server run Verdaccio, an open-source implementation of the npm registry). It however requires a specific setup that isn't always easy to deploy to both developers and CI, and those systems sometimes come with risks.

Yarn 提供了一种非常简单但有效的替代方案:通过将 enableGlobalCache 设置为 false,它会将包缓存保存到项目本地的文件夹中(默认为 .yarn/cache),然后可以将其添加到 Git。因此,即使 npm 注册表出现问题,每个给定的提交都保证可安装。

¥Yarn provides a very simple but effective alternative: by setting enableGlobalCache to false, it will save the package cache into a folder local to your project (by default .yarn/cache) that can then be added to Git. Every given commit is thus guaranteed to be installable, even should the npm registry go under.

零安装

¥Zero-installs

零安装是两个 Yarn 功能的组合,可让你在切换分支时跳过考虑运行 yarn install 的问题 - 否则很容易忘记这个要求,直到你看到工具崩溃。

¥Zero-installs are the combination of two Yarn features that allow you to skip having to think about running yarn install when switching branches - a requirement otherwise easy to forget until you see your tools crash.

正如我们所见,离线镜像通过将 Yarn 缓存保留在存储库中来消除项目对 npm 注册表的依赖。但是我们可以更进一步,直接将这个缓存变为实际的吗?答案是肯定的!

¥As we saw, the offline mirror removes your project's dependency on the npm registry by keeping the Yarn cache within the repository. But can we go further, and directly make this cache the actual? The answer is yes!

只要你的项目使用 Yarn PnP离线镜像,你所要做的就是将加载器文件添加到 Git,大多数时候你都可以忘记 yarn install。由于 PnP 加载器无论生成它们的机器是什么,都具有完全相同的内容,并且由于离线缓存将包含加载器引用的所有文件,因此 git checkout 调用实际上兼作 yarn install

¥As long as your project uses Yarn PnP and the offline mirror, all you have to do is add the loader files to Git, and you can forget yarn install most of the time. Since the PnP loaders have exactly the same content regardless of the machine that generated them, and since the offline cache will contain all the files that the loaders reference, the git checkout calls effectively double as yarn install of sort.

一个警告:添加或删除具有原生依赖的包仍需要运行 yarn install,因为这些包依赖于文件,与 Node.js 脚本不同,这些文件无法直接从其 zip 存档中进行评估。这些包在实践中非常罕见,不经常更新,如果你忘记更新,Yarn 将显示一条有用的错误消息,因此这不会显著影响模式的实用性。

¥One caveat: adding or removing packages with native dependencies will still require yarn install to be run, as such packages depend on files that, unlike Node.js scripts, can't be evaluated directly from within their zip archives. Those packages are quite rare in practice, aren't frequently updated, and Yarn will display an helpful error message should you forget to do it, so this doesn't significantly impact the usefulness of the pattern.

信息

通过将 node_modules 文件夹添加到 Git,从技术上讲可以实现零安装。然而,不同之处在于 node_modules 文件夹包含数千个文件,Git 必须单独对每个文件进行差异处理,提升会导致它们频繁移动,并且人们倾向于手动更改最终提交的 node_modules 文件夹。

¥Zero-installs are technically possible by adding your node_modules folders to Git. The difference however is that node_modules folders contain multiple thousands of files that Git each has to diff individually, that the hoisting causes them to frequently be moved around, and that people have a bad tendency to make manual changes to their node_modules folder that end up committed.

相比之下,将缓存添加到 Git 并使用 Yarn PnP 会为你提供一个文件夹,其中包含每个包的一个 zip 存档,以及 PnP 加载器文件。正如我们之前看到的,这对于 Git 跟踪来说要容易得多。

¥By contrast, adding your cache to Git and using Yarn PnP gives you a single folder with exactly one zip archive for each package, plus the PnP loader file. This is vastly easier for Git to track, as we saw earlier.

特定环境

¥Specific environments

GitHub Actions

信息

我们仍在研究使 GH Action 缓存更高效的确切默认设置集。我们很可能会为此目的在中期提供官方的 yarn-cache 操作。

¥We're still investigating the exact set of defaults that make GH Action caching more efficient. It's likely that we'll provide an official yarn-cache action mid-term for this purpose.