Skip to content

generalized function caching - #10975

Merged
sipsma merged 21 commits into
dagger:mainfrom
sipsma:func-caching
Oct 25, 2025
Merged

sipsma merged 21 commits into
dagger:mainfrom
sipsma:func-caching

Conversation

@sipsma

@sipsma sipsma commented Sep 2, 2025 •

Copy link
Copy Markdown
Contributor

Go example:

package main

import (
	"crypto/rand"
)

type Test struct{}

// My cool doc on TestTtl
// +cache="10s"
func (m *Test) TestTtl() string {
	return rand.Text()
}

// My dope doc on TestCachePerSession
// +cache="session"
func (m *Test) TestCachePerSession() string {
	return rand.Text()
}

// My darling doc on TestNeverCache
// +cache="never"
func (m *Test) TestNeverCache() string {
	return rand.Text()
}

// My rad doc on TestAlwaysCache
func (m *Test) TestAlwaysCache() string {
	return rand.Text()
}

Python example:

import dagger
import random
import string

@dagger.object_type
class Test:
    @dagger.function(cache="10s")
    def test_ttl(self) -> str:
        return ''.join(random.choices(string.ascii_lowercase + string.digits, k=10))

    @dagger.function(cache="session")
    def test_cache_per_session(self) -> str:
            return ''.join(random.choices(string.ascii_lowercase + string.digits, k=10))

    @dagger.function
    def test_always_cache(self) -> str:
            return ''.join(random.choices(string.ascii_lowercase + string.digits, k=10))

Typescript:

import crypto from "crypto"

import {  object, func } from "@dagger.io/dagger"

@object()
export class Test {
	@func({ cache: "20s"})
	testTtl(): string {
		return crypto.randomBytes(16).toString("hex")
	}

	@func({ cache: "session" })
	testCachePerSession(): string {
		return crypto.randomBytes(16).toString("hex")
	}

	@func()
	testAlwaysCache(): string {
		return crypto.randomBytes(16).toString("hex")
	}
}

Got the TTL to behave as an actual TTL now, it starts when the function starts executing.

Also persisted in sqlite, with periodic (every 10m) gc of expired TTL metadata.

Caveats for now:

  • If a function does not have a cache TTL and is not marked as session or never, it defaults to a TTL of 7 days. This is to make pruning logic as simple as possible. Something more intelligent would obviously be preferable but would take much longer and require much further progress on the new theseus caching system.
    • This is the main tradeoff of short-cutting the function caching; still seems very worth it overall to me

If a function makes a SetSecret call (i.e. the "inline" api call, not using a provider) and includes the result in its return value, the result will never be persistently cached, no matter the function cache settings. Per-session cache will still apply (backwards compatible).

  • Use of secrets via secret providers are totally fine and compatible with caching (since the plaintext never needs to be persisted).

Newly init'd modules will start with function caching by default.

Already init'd modules upgrading to the new engine (with dagger develop) will have a setting automatically placed in dagger.json disableDefaultFunctionCaching: true. Once the author is ready to upgrade, they can remove that setting.

  • This is to prevent instant confusing breakage for users w/ functions that rely on not being persistently cached

TODO:

  • update telemetry tests (lots cached that wasn't previously)
  • sdks (and associated integ tests)
    • go
    • python
    • ts
  • don't cache if returned value has ID w/ a "inlined" SetSecret (secret not reproducible, can't cache)
  • handle back compat (via dagger.json flags)
  • more integ tests
    • SetSecret behavior
  • store cache metadata on disk
  • basic validation: max/min limit on cache-ttl, etc.

Followups:

  • docs
  • investigate other SDKs (for now they just will not have per-function cache control)
    • elixir
    • java
    • php
    • rust
    • dotnet
  • integ tests
    • GC of expiration cache

@sipsma
sipsma force-pushed the func-caching branch 6 times, most recently from 8213b87 to b486dd7 Compare September 20, 2025 00:58
@sipsma
sipsma force-pushed the func-caching branch 5 times, most recently from 02a0f22 to 05d9d15 Compare September 24, 2025 21:53
@shykes

shykes commented Sep 25, 2025 •

Copy link
Copy Markdown
Contributor

Dropping my thoughts on DX, since we just discussed it live...

Option 1: two pragmas

One pragma to disable, another to configure.

Feature Go pragma
Disable caching +nocache
Configure caching +ttl=session|DURATION
Desired behavior How to configure it
Maximum caching - (default)
No caching +nocache
Cache for the current session +ttl=session
Cache for 1 hour +ttl=1h

Option 2: one pragma

One pragma to rule them all.

Feature Go pragma
Configure caching +cache=never|session|DURATION
Desired behavior How to configure it
Maximum caching - (default)
No caching +cache=never
Cache for the current session +cache=session
Cache for 1 hour +cache=1h

I like option 2 best, because you can't beat the simplicity of a single pragma called cache :) I'm slightly sad that the word ttl doesn't show up anywhere, but that might be for the best.

cc @sipsma @vito @jedevc @marcosnils

@sipsma

sipsma commented Sep 25, 2025 •

Copy link
Copy Markdown
Contributor Author

@shykes Yeah I agree having a single pragma is appealing and simple. Even for hypothetical cases like "cache per session and also with a ttl" you could still easily support something like +cache=session,1m

EDIT: or I suppose just multiple //+cache pragmas like

// +cache=session
// +cache=1m

@shykes

shykes commented Sep 25, 2025

Copy link
Copy Markdown
Contributor

@sipsma if we go with option 2 (one pragma), I'm OK with not allowing multiple +cache directives, at least initially. This means you won't be able to express "cache this function up to 1mn or until the end of the session, whichever comes first". But a) I can live with that, and b) we can always add it later if people ask for it.

(yes I know I was asking for the opposite just an hour ago :) But I didn't expect a single pragma to work so well)

@sipsma
sipsma force-pushed the func-caching branch 9 times, most recently from c888826 to d82d3df Compare October 16, 2025 00:32
@sipsma
sipsma force-pushed the func-caching branch 4 times, most recently from 9fd8660 to 1956429 Compare October 21, 2025 18:59
sipsma added 16 commits October 24, 2025 15:17
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma requested a review from vito October 25, 2025 00:15

@vito vito left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit: :shipit: :shipit: :shipit:

Comment thread dagql/objects.go
return r
}

func (r Result[T]) WithSafeToPersistCache(safe bool) AnyResult {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat how dynamic this is 👍

@sipsma
sipsma merged commit 8791e2d into dagger:main Oct 25, 2025
@shykes

shykes commented Oct 26, 2025

Copy link
Copy Markdown
Contributor

!!!!!!!!!!!!!! 🥳

@sipsma sipsma added this to the v0.19.4 milestone Oct 28, 2025
sipsma added a commit that referenced this pull request Nov 18, 2026
A module function with no cache setting had its cached result expire
after one week, and a declared time to live (TTL) could be at most one
week. #10975 added that limit when the function cache was a table whose
rows were only ever deleted once they expired. The engine now deletes
cached results by size, so the limit only made such functions run again
every week, and put a one-week expiry on the results they returned,
including results shipped in engine images.

A function with no cache setting now has no expiry: Dagger keeps its
cached result until its inputs change or the engine needs the disk
space. A declared TTL may be longer than one week.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit that referenced this pull request Nov 18, 2026
A module function with no cache setting had its cached result expire
after one week, and a declared time to live (TTL) could be at most one
week. #10975 added that limit when the function cache was a table whose
rows were only ever deleted once they expired. The engine now deletes
cached results by size, so the limit only made such functions run again
every week, and put a one-week expiry on the results they returned,
including results shipped in engine images.

A function with no cache setting now has no expiry: Dagger keeps its
cached result until its inputs change or the engine needs the disk
space. A declared TTL may be longer than one week.

Signed-off-by: Erik Sipsma <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants