cache

package
v0.19.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheRecursiveCall = fmt.Errorf("recursive call detected")

Functions

func CurrentStorageKey added in v0.19.4

func CurrentStorageKey(ctx context.Context) string

Get the key that should be used (or mixed into) persistent cache storage We smuggle this around in the context for now since we have to incorporate it with buildkit's persistent cache for now.

Types

type Cache added in v0.16.3

type Cache[K KeyType, V any] interface {
	// Using the given key, either return an already cached value for that key or initialize
	// an entry in the cache with the given value for that key.
	GetOrInitializeValue(context.Context, CacheKey[K], V) (Result[K, V], error)

	// Using the given key, either return an already cached value for that key or initialize a
	// new value using the given function. If the function returns an error, the error is returned.
	GetOrInitialize(
		context.Context,
		CacheKey[K],
		func(context.Context) (V, error),
	) (Result[K, V], error)

	// Using the given key, either return an already cached value for that key or initialize a
	// new value using the given function. If the function returns an error, the error is returned.
	// The function returns a ValueWithCallbacks struct that contains the value and optionally
	// any additional callbacks for various parts of the cache lifecycle.
	GetOrInitializeWithCallbacks(
		context.Context,
		CacheKey[K],
		func(context.Context) (*ValueWithCallbacks[V], error),
	) (Result[K, V], error)

	// Returns the number of entries in the cache.
	Size() int

	// Run a blocking loop that periodically garbage collects expired entries from the cache db.
	GCLoop(context.Context)
}

func NewCache added in v0.16.3

func NewCache[K KeyType, V any](ctx context.Context, dbPath string) (Cache[K, V], error)

type CacheKey

type CacheKey[K KeyType] struct {
	// CallKey is identifies the the call. If a call has already been completed with this
	// CallKey and it is not expired, its cached result will be returned.
	CallKey K

	// ConcurrencyKey is used to determine whether *in-progress* calls should be deduplicated.
	// If a call with a given (ResultKey, ConcurrencyKey) pair is already in progress, and
	// another one comes in with the same pair, the second caller will wait for the first
	// to complete and receive the same result.
	//
	// If two calls have the same ResultKey but different ConcurrencyKeys, they will not be deduped.
	//
	// If ConcurrencyKey is the zero value for K, no deduplication of in-progress calls will be done.
	ConcurrencyKey K

	// TTL is the time-to-live for the cached result of this call, in seconds.
	TTL int64

	// DoNotCache indicates that this call should not be cached at all, simply ran.
	DoNotCache bool
}

type KeyType added in v0.19.4

type KeyType = interface {
	~string
}

type OnReleaseFunc added in v0.16.3

type OnReleaseFunc = func(context.Context) error

type PostCallFunc added in v0.16.3

type PostCallFunc = func(context.Context) error

type Result

type Result[K KeyType, V any] interface {
	Result() V
	Release(context.Context) error
	PostCall(context.Context) error
	HitCache() bool
}

type ValueWithCallbacks added in v0.16.3

type ValueWithCallbacks[V any] struct {
	// The actual value to cache
	Value V

	// If set, a function that should be called whenever the value is returned from the cache (whether newly initialized or not)
	PostCall PostCallFunc

	// If set, this function will be called when a result is removed from the cache
	OnRelease OnReleaseFunc

	// If true, indicates that it is safe to persist this value in the cache db (i.e. does not have
	// any in-memory only data).
	SafeToPersistCache bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL