|
| 1 | +--- |
| 2 | +title: "Caching" |
| 3 | +description: "How the caching process works" |
| 4 | +icon: "layer-group" |
| 5 | +--- |
| 6 | + |
| 7 | +The caching concept is similar to [Docker's layer caching](https://docs.docker.com/build/cache/). For each layer command (`.copy()`, `.runCmd()`, `.setEnvs()`, etc.), we create a new layer on top of the existing. |
| 8 | +Each layer is cached based on the command and its inputs (e.g., files copied, command executed, environment variables set). |
| 9 | +If a layer command is unchanged and its inputs are the same as in any previous build, we reuse the cached layer instead of rebuilding it. |
| 10 | + |
| 11 | +This significantly speeds up the build process, especially for large templates with many layers. |
| 12 | +The cache is scoped to the team, so even if you have multiple templates, they can share the same cache if they have identical layers. |
| 13 | + |
| 14 | +## Invalidation |
| 15 | +You can invalidate the caches only partially, or for the whole template. |
| 16 | + |
| 17 | +### Partial |
| 18 | +Force rebuild from the next instruction, use the method: |
| 19 | + |
| 20 | +<CodeGroup dropdown> |
| 21 | + |
| 22 | +```typescript highlight={3} |
| 23 | +const template = Template() |
| 24 | + .fromBaseImage() |
| 25 | + .skipCache() |
| 26 | + .runCmd("echo 'Hello, World!'") |
| 27 | +``` |
| 28 | + |
| 29 | +```python highlight={4} |
| 30 | +template = ( |
| 31 | + Template() |
| 32 | + .from_base_image() |
| 33 | + .skip_cache() |
| 34 | + .run_cmd("echo 'Hello, World!'") |
| 35 | +) |
| 36 | +``` |
| 37 | + |
| 38 | +</CodeGroup> |
| 39 | + |
| 40 | +This will force rebuild from the next instruction, invalidating the cache for all subsequent instructions in the template. |
| 41 | + |
| 42 | +### Whole Template |
| 43 | +To force rebuild the whole template, you can use also `skipCache`/`skip_cache` parameter in the `Template.build` method: |
| 44 | + |
| 45 | +<CodeGroup dropdown> |
| 46 | + |
| 47 | +```typescript wrap highlight={3} |
| 48 | +Template.build(template, { |
| 49 | + alias: 'my-template', |
| 50 | + skipCache: true, // Configure cache skip (except for files) |
| 51 | +}) |
| 52 | +``` |
| 53 | + |
| 54 | +```python wrap highlight={4} |
| 55 | +Template.build( |
| 56 | + template, |
| 57 | + alias="my-template", |
| 58 | + skip_cache=True, # Configure cache skip (except for files) |
| 59 | +) |
| 60 | +``` |
| 61 | + |
| 62 | +</CodeGroup> |
| 63 | + |
| 64 | +This will skip the cache usage for the whole template build. |
| 65 | + |
| 66 | +## Files Caching |
| 67 | +When using the `.copy()` command, we cache the files based on their content. If the files haven't changed since the last build, we reuse them from the files cache. |
| 68 | + |
| 69 | +We differ from Docker here. Because we build the template on our infrastructure, we use improved caching on files level. |
| 70 | +Even if you invalidate the layer before `.copy()` (e.g., by changing ENV variables), we'll reuse the already uploaded files. |
| 71 | +The `copy()` command will still be re-executed, but the files for the layer will be reused from the files cache, no need to upload them from your computer again. |
| 72 | + |
| 73 | +To invalidate the cache for all subsequent instructions in the template **AND** the layer files cache, use the `forceUpload`/`force_upload` parameter. |
| 74 | + |
| 75 | +<CodeGroup dropdown> |
| 76 | + |
| 77 | +```typescript highlight={3} |
| 78 | +const template = Template() |
| 79 | + .fromBaseImage() |
| 80 | + .copy("config.json", "/app/config.json", { forceUpload: true }) |
| 81 | +``` |
| 82 | + |
| 83 | +```python highlight={4} |
| 84 | +template = ( |
| 85 | + Template() |
| 86 | + .from_base_image() |
| 87 | + .copy("config.json", "/app/config.json", force_upload=True) |
| 88 | +) |
| 89 | +``` |
| 90 | + |
| 91 | +</CodeGroup> |
| 92 | + |
| 93 | +## Use Case for Caching |
| 94 | +You can leverage caching to create templates with multiple variants (e.g., different RAM or CPU) while reusing the common layers. |
| 95 | +When building the template, just change the template alias to a specific RAM/CPU configuration (e.g., `my-template-2cpu-2gb`, `my-template-1cpu-4gb`), keep the rest of the template definition the same, and the build process will reuse the cached layers. |
| 96 | + |
| 97 | +## Optimize Build Times |
| 98 | +To optimize build times, place frequently changing commands (e.g., copying source code) towards the end of your template definition. This way, earlier layers can be cached and reused more often. |
0 commit comments