cache

package
v1.222.0-rc.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package cache provides a CI-provider-scoped remote build cache, modeled on the artifact subsystem (pkg/ci/artifact). It lets Atmos restore a well-known cache directory (the toolchain install path and anything else under the XDG cache root) at startup and save it at exit, using the same store that actions/cache uses when running inside a CI provider.

The Backend interface is key/restore-key oriented (write-once entries with prefix fallback), unlike the artifact Backend which is name-oriented. A concrete Backend is provided by the active CI provider (see pkg/ci/providers/github), so all operations are no-ops with errUtils.ErrCacheUnavailable when no cache-capable provider is detected.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheRoot

func CacheRoot() (string, error)

CacheRoot returns the absolute path of the well-known cache root that the CI cache archives. It defaults to the Atmos XDG cache directory (~/.cache/atmos) and is overridable via ATMOS_XDG_CACHE_HOME / XDG_CACHE_HOME. The toolchain install path is a sub-path of this root, so caching the root captures it.

func GetRegisteredTypes

func GetRegisteredTypes() []string

GetRegisteredTypes returns the list of registered cache backend types.

func Register

func Register(backendType string, factory BackendFactory)

Register registers a cache backend factory for the given type. Backends call this in their init() function. Both backendType and factory must be non-empty/non-nil.

Types

type Backend

type Backend interface {
	// Name returns the backend type name (e.g., "github/actions").
	Name() string

	// Save uploads a single data stream (a tar.gz archive) under the exact key.
	// Implementations return errUtils.ErrCacheAlreadyExists when the key exists.
	Save(ctx context.Context, key string, data io.Reader, size int64) error

	// Restore downloads the entry for the exact key, or the first restore-key
	// prefix match when the exact key is absent. It returns the key that was
	// actually matched and a reader the caller must close. When nothing
	// matches it returns errUtils.ErrCacheNotFound.
	Restore(ctx context.Context, key string, restoreKeys []string) (matchedKey string, rc io.ReadCloser, err error)

	// List returns cache entries, optionally filtered by key prefix.
	List(ctx context.Context, opts ListOptions) ([]Entry, error)

	// Delete removes a cache entry by exact key. Deleting a missing key is a
	// no-op (returns nil) so callers can treat delete as idempotent.
	Delete(ctx context.Context, key string) error
}

Backend defines the interface for a remote CI cache store.

Cache entries are immutable (write-once): saving a key that already exists returns errUtils.ErrCacheAlreadyExists. Restore performs an exact-key lookup first, then falls back to the supplied restore-keys (prefix matches, newest first), mirroring the semantics of actions/cache.

func NewBackend

func NewBackend(backendType string, opts Options) (Backend, error)

NewBackend creates a backend of the given type from the registered factory.

type BackendFactory

type BackendFactory func(opts Options) (Backend, error)

BackendFactory creates a Backend from Options.

type Config

type Config struct {
	// Enabled is the master switch (ci.cache.enabled).
	Enabled bool

	// Auto is the automatic mode: off | restore | save | both.
	Auto string

	// Root is the absolute well-known cache directory that is archived.
	Root string

	// Includes are root-relative subpaths to include; empty means the whole root.
	Includes []string

	// Key is the resolved exact cache key.
	Key string

	// RestoreKeys are prefix fallbacks tried (in order) when Key is absent.
	RestoreKeys []string

	// Compression is the archive compression (currently always gzip).
	Compression string
}

Config is the fully-resolved cache configuration used by the manager. It is derived from schema.CICacheConfig plus computed defaults, so both the CLI commands and the lifecycle hooks operate on identical inputs.

func ResolveConfig

func ResolveConfig(atmosConfig *schema.AtmosConfiguration) (*Config, error)

ResolveConfig builds a Config from the Atmos configuration, filling in defaults for any unset fields. It does not require the cache to be enabled; callers gate on Config.Enabled.

func (*Config) AutoRestoreEnabled

func (c *Config) AutoRestoreEnabled() bool

AutoRestoreEnabled reports whether automatic restore-on-start is configured.

func (*Config) AutoSaveEnabled

func (c *Config) AutoSaveEnabled() bool

AutoSaveEnabled reports whether automatic save-on-end is configured.

type Entry

type Entry struct {
	// Key is the cache key.
	Key string

	// Size is the entry size in bytes (0 if unknown).
	Size int64

	// CreatedAt is when the entry was created (zero if unknown).
	CreatedAt time.Time

	// ID is the provider-specific identifier for the entry (may be empty).
	ID string
}

Entry describes a single cache entry as reported by Backend.List.

type ListOptions

type ListOptions struct {
	// KeyPrefix restricts results to entries whose key starts with this value.
	// Empty matches all entries.
	KeyPrefix string
}

ListOptions filters the entries returned by Backend.List.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager orchestrates cache operations against a Backend using a resolved Config. It is the single source of truth shared by the CLI subcommands and the automatic lifecycle hooks, so "automatic" behavior is exactly the same idempotent operations invoked at startup/exit. A per-root state marker keeps manual and automatic invocations from double-executing.

func NewManager

func NewManager(backend Backend, cfg *Config) *Manager

NewManager creates a Manager from a backend and resolved config.

func (*Manager) Config

func (m *Manager) Config() *Config

Config returns the resolved configuration.

func (*Manager) Delete

func (m *Manager) Delete(ctx context.Context, key string) error

Delete removes a cache entry by exact key.

func (*Manager) List

func (m *Manager) List(ctx context.Context, prefix string) ([]Entry, error)

List returns cache entries matching the prefix.

func (*Manager) Restore

func (m *Manager) Restore(ctx context.Context) (*RestoreResult, error)

Restore restores the configured cache into the cache root. It is idempotent: once a key has been restored in this lifecycle, a subsequent Restore is a no-op.

func (*Manager) Save

func (m *Manager) Save(ctx context.Context) (*SaveResult, error)

Save archives the cache root and uploads it under the configured key. It is idempotent and respects write-once semantics: when the exact key was a hit at restore time (unchanged content) or has already been saved this lifecycle, Save is a no-op.

type Options

type Options struct {
	// AtmosConfig is the active Atmos configuration (may be nil).
	AtmosConfig *schema.AtmosConfiguration

	// Options carries backend-specific configuration (owner, repo, etc.).
	Options map[string]any
}

Options contains the inputs a BackendFactory uses to construct a Backend.

type RestoreResult

type RestoreResult struct {
	// Hit is true when an entry was restored (exact or prefix match).
	Hit bool
	// MatchedKey is the key that was actually restored.
	MatchedKey string
	// Exact is true when the exact key matched (vs a restore-key prefix).
	Exact bool
	// Skipped is true when restore was a no-op because this key was already
	// restored in this lifecycle.
	Skipped bool
}

RestoreResult describes the outcome of a restore.

type SaveResult

type SaveResult struct {
	// Saved is true when an entry was uploaded (or already present remotely).
	Saved bool
	// Skipped is true when save was a no-op.
	Skipped bool
	// Reason explains a skip (e.g. "exact cache hit", "already saved").
	Reason string
}

SaveResult describes the outcome of a save.

Directories

Path Synopsis
Package github implements the CI cache Backend against the GitHub Actions cache (Cache Service v2).
Package github implements the CI cache Backend against the GitHub Actions cache (Cache Service v2).

Jump to

Keyboard shortcuts

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