Skip to content

Commit b7d13eb

Browse files
authored
Improve template caches invalidation (#11)
1 parent b944008 commit b7d13eb

File tree

4 files changed

+102
-43
lines changed

4 files changed

+102
-43
lines changed

docs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"pages": [
2020
"template/quickstart",
2121
"template/how-it-works",
22+
"template/caching",
2223
{
2324
"group": "Customization",
2425
"icon": "wand-magic-sparkles",

template/caching.mdx

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.

template/customization/defining-template.mdx

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ template.copy([
7474
{ src: "package.json", dest: "/app/package.json" },
7575
]);
7676

77-
// Force upload (bypass cache)
78-
template.copy("config.json", "/app/config.json", { forceUpload: true });
79-
8077
// Copy with user and mode options
8178
template.copy("config.json", "/app/config.json", {
8279
user: "appuser",
@@ -94,8 +91,8 @@ template.copy([
9491
{"src": "package.json", "dest": "/app/package.json"},
9592
])
9693

97-
# Force upload (bypass cache)
98-
template.copy("config.json", "/app/config.json", force_upload=True)
94+
# Copy with user and mode options
95+
template.copy("config.json", "/app/config.json", user="appuser", mode=0o644)
9996
```
10097

10198
</CodeGroup>
@@ -239,24 +236,6 @@ template.set_envs({
239236

240237
</CodeGroup>
241238

242-
### Invalidating caches
243-
244-
Force rebuild from the next instruction:
245-
246-
<CodeGroup dropdown>
247-
248-
```typescript
249-
template.skipCache()
250-
```
251-
252-
```python
253-
template.skip_cache()
254-
```
255-
256-
</CodeGroup>
257-
258-
This will invalidate the cache for all subsequent instructions in the template.
259-
260239
### Running commands
261240

262241
Execute shell commands during template build:

template/how-it-works.mdx

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,4 @@ We call this sandbox snapshot a _sandbox template_.
2525
</Note>
2626

2727
## Caching
28-
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.
29-
Each layer is cached based on the command and its inputs (e.g., files copied, command executed, environment variables set).
30-
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.
31-
32-
This significantly speeds up the build process, especially for large templates with many layers.
33-
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.
34-
35-
### Files Caching
36-
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.
37-
38-
We differ from Docker here. Because we build the template on our infrastructure, we use improved caching on files level.
39-
Even if you invalidate the layer before `.copy()` (e.g., by changing ENV variables), we'll reuse the already uploaded files.
40-
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.
41-
42-
### Use Case for Caching
43-
You can leverage caching to create templates with multiple variants (e.g., different RAM or CPU) while reusing the common layers.
44-
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.
45-
46-
### Optimize Build Times
47-
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.
28+
To learn more about caching, please refer to the [Caching](/template/caching) section.

0 commit comments

Comments
 (0)