ocicache

package
v0.0.73 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package ocicache manages the on-disk klaus-oci registry response cache for klausctl.

The cache accelerates repeated klausctl invocations by remembering the results of catalog lookups, tag-list queries, reference resolutions and manifest/blob fetches. It is a wrapper around the klaus-oci CacheStore rooted at a persistent directory derived from the XDG base directory spec.

Global configuration (directory override and bypass) lives at package scope so the cobra root command can apply --cache-dir / --no-cache once and every oci.Client construction site picks them up transparently via Options().

Index

Constants

View Source
const EnvBypass = "KLAUSCTL_NO_CACHE" // #nosec G101 -- constant identifier, not a credential

EnvBypass is the env var that, when truthy, disables the cache for a single invocation without persistent effect.

View Source
const EnvCacheDir = "KLAUSCTL_CACHE_DIR"

EnvCacheDir is an optional env var that overrides the cache directory. It is the env counterpart of the --cache-dir flag.

Variables

View Source
var Layers = []string{"catalog", "tags", "refs", "blobs"}

Layers is the set of index sub-directories the klaus-oci disk cache uses. Kept here as a constant so our management commands (info, prune) can reason about the layout without re-implementing it.

Functions

func Configure

func Configure(dir string, disabled bool)

Configure sets process-wide cache settings. Passing an empty dir keeps the default XDG-derived location. disabled=true bypasses the cache.

It is safe to call multiple times; later calls replace the previous settings. cmd/root.go calls this from cobra.OnInitialize after global flags are parsed. OnInitialize is used rather than PersistentPreRun because any subcommand that defines its own PreRun would otherwise shadow the root's hook and skip cache configuration.

func Dir

func Dir() (string, error)

Dir returns the resolved cache directory, consulting overrides and env vars in order. Returns an empty string (and no error) when no suitable directory can be determined, which callers should treat as "caching disabled".

func Disabled

func Disabled() bool

Disabled reports whether the cache is bypassed for this process.

func Options

func Options() []klausoci.ClientOption

Options returns the klaus-oci client options required to wire up the on-disk cache. When caching is disabled (via --no-cache or missing environment) it returns nil. Errors resolving the cache directory are returned as nil options to preserve network-only behavior rather than failing unrelated commands.

func Reset

func Reset()

Reset clears process-wide cache configuration, including the memoised env-var read. Intended for tests.

Types

type Info

type Info struct {
	// Dir is the resolved cache directory. Empty when caching is
	// disabled for this process.
	Dir string `json:"dir"`
	// Disabled reports whether the cache is bypassed for this invocation.
	Disabled bool `json:"disabled"`
	// Exists reports whether Dir exists on disk.
	Exists bool `json:"exists"`
	// TotalBytes is the summed size of all files under Dir.
	TotalBytes int64 `json:"total_bytes"`
	// Layers lists per-subdirectory statistics for the four klaus-oci
	// index layers (catalog, tags, refs, blobs).
	Layers []LayerInfo `json:"layers"`
	// NewestEntry is the modification time of the most recently written
	// cache entry, or zero when the cache is empty.
	NewestEntry time.Time `json:"newest_entry"`
	// FreshTTL is the window within which entries are served with zero
	// network traffic.
	FreshTTL time.Duration `json:"fresh_ttl"`
	// StaleTTL is the outer bound for ref/tag entries.
	StaleTTL time.Duration `json:"stale_ttl"`
	// CatalogStaleTTL is the outer bound for catalog entries.
	CatalogStaleTTL time.Duration `json:"catalog_stale_ttl"`
}

Info describes the on-disk cache for the `cache info` command.

func Stat

func Stat() (*Info, error)

Stat returns an Info snapshot for the current cache configuration. The returned error is non-nil only for unexpected filesystem problems; a missing cache directory returns an Info with Exists=false.

type LayerInfo

type LayerInfo struct {
	Name    string `json:"name"`
	Entries int    `json:"entries"`
	Bytes   int64  `json:"bytes"`
}

LayerInfo is a per-subdirectory summary of the cache.

type PruneOptions

type PruneOptions struct {
	// All removes everything under the cache directory, including fresh
	// entries. When false, only entries that the klaus-oci cache would
	// treat as stale (older than the relevant stale TTL) are removed.
	All bool
	// Now is the reference time used to evaluate entry freshness.
	// Defaults to time.Now().
	Now time.Time
}

PruneOptions controls which entries `Prune` removes.

type PruneResult

type PruneResult struct {
	// Dir is the cache directory that was pruned.
	Dir string `json:"dir"`
	// FilesRemoved is the number of files deleted.
	FilesRemoved int `json:"files_removed"`
	// BytesRemoved is the summed size of deleted files.
	BytesRemoved int64 `json:"bytes_removed"`
	// Removed is the cache-relative path of each deleted file. Populated
	// only for --all prunes so large stale sweeps do not produce unwieldy
	// output.
	Removed []string `json:"removed,omitempty"`
}

PruneResult reports what Prune removed.

func Prune

func Prune(opts PruneOptions) (*PruneResult, error)

Prune deletes cached entries. When opts.All is true the entire cache directory tree is removed, leaving only the root directory. Otherwise only stale index entries are removed -- content-store blobs are kept under the klaus-oci LRU-size policy.

type RefreshOptions

type RefreshOptions struct {
	// Registry is an optional registry base ("host" or "host/prefix"). When
	// non-empty, only catalog entries for this base are invalidated.
	Registry string
	// Repo is an optional repository ("host/name"). When non-empty, only
	// tag and ref entries for this repository are invalidated.
	Repo string
}

RefreshOptions controls which entries Refresh forces a revalidation of.

type RefreshResult

type RefreshResult struct {
	// Dir is the cache directory that was refreshed.
	Dir string `json:"dir"`
	// FilesRemoved is the count of index files invalidated.
	FilesRemoved int `json:"files_removed"`
	// Scope describes what was refreshed (all, registry, repo, ...).
	Scope string `json:"scope"`
}

RefreshResult reports the outcome of a foreground refresh.

func Refresh

func Refresh(_ context.Context, opts RefreshOptions) (*RefreshResult, error)

Refresh forces revalidation by invalidating the matching index entries. On the next client call the klaus-oci store will synchronously refetch the data from the registry. Blob content is kept because blobs are content-addressed and never become stale.

Refresh does not itself issue any network requests. It prepares the cache so that subsequent reads hit the registry.

Jump to

Keyboard shortcuts

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