-
Notifications
You must be signed in to change notification settings - Fork 1
Improve template caches invalidation #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| --- | ||
| title: "Caching" | ||
| description: "How the caching process works" | ||
| icon: "layer-group" | ||
| --- | ||
|
|
||
| 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. | ||
| Each layer is cached based on the command and its inputs (e.g., files copied, command executed, environment variables set). | ||
| 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. | ||
|
|
||
| This significantly speeds up the build process, especially for large templates with many layers. | ||
| 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. | ||
|
|
||
| ## Invalidation | ||
| You can invalidate the caches only partially, or for the whole template. | ||
|
|
||
| ### Partial | ||
| Force rebuild from the next instruction, use the method: | ||
|
|
||
| <CodeGroup dropdown> | ||
|
|
||
| ```typescript highlight={3} | ||
| const template = Template() | ||
| .fromBaseImage() | ||
| .skipCache() | ||
| .runCmd("echo 'Hello, World!'") | ||
| ``` | ||
|
|
||
| ```python highlight={4} | ||
| template = ( | ||
| Template() | ||
| .from_base_image() | ||
| .skip_cache() | ||
| .run_cmd("echo 'Hello, World!'") | ||
| ) | ||
| ``` | ||
|
|
||
| </CodeGroup> | ||
|
|
||
| This will force rebuild from the next instruction, invalidating the cache for all subsequent instructions in the template. | ||
|
|
||
| ### Whole Template | ||
| To force rebuild the whole template, you can use also `skipCache`/`skip_cache` parameter in the `Template.build` method: | ||
|
|
||
| <CodeGroup dropdown> | ||
|
|
||
| ```typescript wrap highlight={3} | ||
| Template.build(template, { | ||
| alias: 'my-template', | ||
| skipCache: true, // Configure cache skip (except for files) | ||
| }) | ||
| ``` | ||
|
|
||
| ```python wrap highlight={4} | ||
| Template.build( | ||
| template, | ||
| alias="my-template", | ||
| skip_cache=True, # Configure cache skip (except for files) | ||
mishushakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| ``` | ||
|
|
||
| </CodeGroup> | ||
|
|
||
| This will skip the cache usage for the whole template build. | ||
|
|
||
| ## Files Caching | ||
| 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. | ||
|
|
||
| We differ from Docker here. Because we build the template on our infrastructure, we use improved caching on files level. | ||
| Even if you invalidate the layer before `.copy()` (e.g., by changing ENV variables), we'll reuse the already uploaded files. | ||
| 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. | ||
|
|
||
| To invalidate the cache for all subsequent instructions in the template **AND** the layer files cache, use the `forceUpload`/`force_upload` parameter. | ||
|
|
||
| <CodeGroup dropdown> | ||
|
|
||
| ```typescript highlight={3} | ||
| const template = Template() | ||
| .fromBaseImage() | ||
| .copy("config.json", "/app/config.json", { forceUpload: true }) | ||
| ``` | ||
|
|
||
| ```python highlight={4} | ||
| template = ( | ||
| Template() | ||
| .from_base_image() | ||
| .copy("config.json", "/app/config.json", force_upload=True) | ||
| ) | ||
| ``` | ||
|
|
||
| </CodeGroup> | ||
|
|
||
| ## Use Case for Caching | ||
| You can leverage caching to create templates with multiple variants (e.g., different RAM or CPU) while reusing the common layers. | ||
| 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. | ||
|
|
||
| ## Optimize Build Times | ||
| 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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.