> ## Documentation Index
> Fetch the complete documentation index at: https://ppio.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 缓存

Template 构建会缓存此前已完成的层(layer),这样重复构建时无需重新执行每条指令,从而加快迭代速度。仅在需要强制全新构建时才跳过缓存——跳过缓存会使构建明显变慢。

缓存默认开启,无需任何配置。当你需要跳过缓存并强制重建时,SDK 和 CLI 提供三个层级的控制:

| 作用范围  | 效果                                                              | 用法                                                                                                                     |
| ----- | --------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| 整个构建  | 忽略所有缓存,从零开始重建整个 Template                                        | JS: `ppio.template.build(t, name, { skipCache: true })`  <br />Python: `ppio.template.build(t, name, skip_cache=True)` |
| 从某一层起 | 强制**该指令及其后所有层**重新构建;若正好放在 `from*`(FROM)指令上,则整个 Template 的缓存都会失效 | JS: `.skipCache()`  <br />Python: `.skip_cache()`                                                                      |
| CLI   | 构建过程中忽略缓存                                                       | `ppio template build --no-cache`                                                                                       |

<Warning>
  **重要:** 链式方法 `skipCache()` / `skip_cache()` 并非只跳过单条指令——它们会作用于调用点之后的**所有后续层**。调用位置越靠前,失效的缓存范围就越大。
</Warning>

**示例:** 在定义中跳过缓存并强制完整重建(两种方式可以组合使用)。

<CodeGroup>
  ```python Python theme={null}
  from ppio_sandbox import PPIO

  ppio = PPIO()

  template = (
      ppio.template.new().from_python_image("3.12")
      .skip_cache()  # 从这里开始的所有层都不使用缓存
      .run_cmd("pip install -U pip")
  )

  build = ppio.template.build(
      template,
      "my-template-no-cache",
      skip_cache=True,  # 忽略整个 Template 的缓存
  )
  ```

  ```javascript JavaScript & TypeScript theme={null}
  import { PPIO } from 'ppio-sandbox'

  const ppio = new PPIO()

  const template = ppio.template.new().fromPythonImage('3.12')
    .skipCache() // 从这里开始的所有层都不使用缓存
    .runCmd('pip install -U pip')

  const build = await ppio.template.build(template, 'my-template-no-cache', {
    skipCache: true, // 忽略整个 Template 的缓存
  })
  ```
</CodeGroup>
