Git Advanced: Monorepo, Submodules & Workflows · 课时

构建缓存与仅构建受影响项目

随着单体仓库不断增长,保持其 CI 高效运行:使用任务图、计算缓存和受影响项目检测,仅构建和测试发生变更的部分。

第 4 / 4 课13 个步骤

构建缓存与仅构建受影响项目 是 CoddyKit 上的免费 Git Advanced: Monorepo, Submodules & Workflows 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Git Advanced: Monorepo, Submodules & Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Scaling Wall

As a monorepo grows, rebuilding and retesting everything on every commit becomes painfully slow.

The fix is to only do work that the change actually requires, and to cache results that have not changed.

The Project Dependency Graph

Monorepo tools build a graph of how projects depend on each other. This graph tells the tool what is downstream of any change.

nx graph

Affected Detection

By comparing your branch against a base commit, the tool computes the set of affected projects — those changed plus everything depending on them.

nx affected --target=test --base=main

Why Only Affected

If you change a leaf library, only it and its consumers need rebuilding — unrelated apps are untouched. This can cut CI time from hours to minutes on large repos.

Computation Caching

Tools hash a task's inputs (source files, config, dependencies). If the hash matches a previous run, the cached output is replayed instead of recomputing.

What Goes Into the Hash

The cache key combines:

  • Source file contents
  • The exact command and its options
  • Dependency versions
  • Relevant environment variables

Any change to these invalidates the cache entry.

Local vs Remote Cache

A local cache speeds up your own repeated runs. A remote cache shares results across the whole team and CI, so work done once benefits everyone.

nx run-many --target=build --all

Cache Hits and Misses

A hit replays stored output instantly; a miss runs the task and stores the result. Tools report this so you can see the time saved.

Correctness of Caching

Caching is only safe if tasks are deterministic and inputs are declared accurately. Hidden inputs (timestamps, network calls) cause stale or wrong cache hits.

Wiring It into CI

In CI, run affected targets against the merge base and enable the remote cache. Each pipeline then does the minimum work and reuses shared results.

nx affected --target=build,test,lint --base=origin/main

Tooling Landscape

Popular tools provide these features: Nx and Turborepo for JS/TS, Bazel for polyglot at scale, and Gradle/Pants for JVM. The concepts are the same across all.

Quick Check

Test your build-optimization knowledge.

Recap

You can now keep monorepo CI fast at scale:

  • Use the project dependency graph to compute affected work
  • Cache task results keyed on hashed inputs
  • Share results via a remote cache across team and CI
  • Keep tasks deterministic so caching stays correct

This extends your dependency management and CI/CD strategy lessons.

免费开始

用 AI 导师学习 Git Advanced: Monorepo, Submodules & Workflows — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「构建缓存与仅构建受影响项目」课时是免费的吗?

是的 — 「构建缓存与仅构建受影响项目」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Git Advanced: Monorepo, Submodules & Workflows 课程的其余内容,请升级到 CoddyKit PRO。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。

「构建缓存与仅构建受影响项目」这节课中我会学到什么?

随着单体仓库不断增长,保持其 CI 高效运行:使用任务图、计算缓存和受影响项目检测,仅构建和测试发生变更的部分。 你通过在浏览器中直接运行的动手代码来练习 Git Advanced: Monorepo, Submodules & Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Git Advanced: Monorepo, Submodules & Workflows 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Git Advanced: Monorepo, Submodules & Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「构建缓存与仅构建受影响项目」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Git Advanced: Monorepo, Submodules & Workflows 课中编写并运行代码吗?

能。每节 Git Advanced: Monorepo, Submodules & Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 单体仓库中的依赖管理
  2. 原子提交与跨项目更改
  3. 单体仓库 CI/CD 策略
  4. 构建缓存与仅构建受影响项目
← 返回 Git Advanced: Monorepo, Submodules & Workflows