cache

package
v1.235.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 15 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cache provides neutral, source-scoped discovery snapshots shared by plugin discovery and artifact discovery.

A Store owns one private directory. Each discovery source (a repository listing, a catalog) has one snapshot file whose name is derived from a scope hash plus the source kind and reference, so snapshots are isolated by effective configuration, authentication realm, and host bindings without ever storing a credential value. Load applies the shared policy: a fresh snapshot is served without contacting the source, an explicit refresh or a stale snapshot fetches a new one, offline mode serves only an existing valid snapshot, and a fetched snapshot replaces the old file atomically only after the fetch completed.

The package knows nothing about plugin or artifact schemas. Callers embed Header first in their snapshot struct (so the historical JSON keys stay in order), supply a decoder that validates the content, and supply the fetch and policy callbacks.

Index

Constants

View Source
const (
	// DefaultTTL is how long a snapshot stays fresh.
	DefaultTTL = 15 * time.Minute

	// DefaultMaxSize bounds the bytes read from, and written to, one snapshot file.
	DefaultMaxSize = 32 << 20

	// DirectoryMode is the permission of the cache directory.
	DirectoryMode = 0o700

	// FileMode is the permission of a snapshot file.
	FileMode = 0o600
)

Variables

This section is empty.

Functions

func Hash

func Hash(raw []byte) string

Hash returns the lowercase hexadecimal SHA-256 of raw. The digest only names cache files and scopes, and it must stay plain SHA-256 so snapshot files written by the plugin discovery cache keep their names. It is not a password hash and never protects a credential.

func Key

func Key(scope, kind, reference string) string

Key derives the file stem of a source within a scope.

func Load

func Load[T any, P interface {
	*T
	Snapshot
}](ctx context.Context, store *Store, source *Source[T, P], mode Mode) (P, bool, error)

Load returns the snapshot of a source and whether it came from the cache. Concurrent loads of the same source (in this or another process) are serialized on a lock file, so a cold cache is filled once and the others read the result.

func Scope

func Scope(parts ...any) (string, error)

Scope hashes the parts that define a cache scope: the effective configuration, the authentication realm, and host bindings. Callers pass configuration, never credential values. Equal parts always produce the same scope.

Types

type Header struct {
	// Version is the snapshot schema version.
	Version int `json:"version"`

	// Source is the reference the snapshot describes.
	Source string `json:"source"`

	// Kind is the source kind, for example "repository" or "catalog".
	Kind string `json:"kind"`

	// FetchedAt is when the snapshot was fetched, in UTC.
	FetchedAt time.Time `json:"fetched_at"`
}

Header is the metadata every snapshot starts with. Embed it first in a snapshot struct so the serialized keys keep their order: version, source, kind, fetched_at.

func (*Header) SetSnapshotHeader

func (h *Header) SetSnapshotHeader(value Header)

SetSnapshotHeader replaces the header.

func (*Header) SnapshotHeader

func (h *Header) SnapshotHeader() Header

SnapshotHeader returns the header itself.

type Mode

type Mode struct {
	// Refresh bypasses snapshot freshness and fetches again. It fails when Offline is set.
	Refresh bool

	// Offline serves an existing valid snapshot of any age and never fetches.
	Offline bool
}

Mode selects how Load may use the network.

type Option

type Option func(*Store)

Option customizes a Store.

func WithClock

func WithClock(now func() time.Time) Option

WithClock injects the clock; tests use a deterministic one.

func WithMaxSize

func WithMaxSize(size int64) Option

WithMaxSize sets the bound on a snapshot file, in bytes.

func WithTTL

func WithTTL(ttl time.Duration) Option

WithTTL sets the freshness window. Zero makes every online load a refresh.

type Snapshot

type Snapshot interface {
	// SnapshotHeader returns the header of the snapshot.
	SnapshotHeader() Header

	// SetSnapshotHeader replaces the header of the snapshot.
	SetSnapshotHeader(Header)
}

Snapshot is implemented by pointers to snapshot structs. A struct that embeds Header satisfies it through the embedded methods; a struct that keeps the four fields itself implements both methods.

type Source

type Source[T any, P interface {
	*T
	Snapshot
}] struct {
	// Kind and Reference identify the source and select its snapshot file.
	Kind, Reference string

	// Version is the snapshot schema version a readable snapshot must carry.
	Version int

	// Decode parses and validates the content of a snapshot file. A snapshot that fails decoding
	// is treated as absent: it is replaced online and rejected offline.
	Decode func(raw []byte) (P, error)

	// Fetch returns a fresh snapshot, given the previous valid one (nil when none). It may reuse
	// the previous snapshot's content when nothing changed. Returning nil without an error means
	// the source is unavailable but optional: nothing is cached.
	Fetch func(ctx context.Context, previous P) (P, error)

	// Authorize runs the current policy before a snapshot is served from cache (cached true) and
	// before the source is contacted (cached false). It may be nil.
	Authorize func(ctx context.Context, cached bool) error

	// ErrOffline is returned in offline mode when no valid snapshot exists or a refresh is requested.
	ErrOffline error

	// ErrTooLarge is returned when a fetched snapshot exceeds the size bound.
	ErrTooLarge error
}

Source describes one discovery source and the callbacks Load needs. P is a pointer to the caller's snapshot struct T, which embeds Header.

type Store

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

Store is one private snapshot directory plus the scope that isolates its files.

func New

func New(directory, scope string, opts ...Option) *Store

New returns a store rooted at directory. The scope is the hash from Scope; it separates the snapshots of different effective configurations that share the directory.

func (*Store) Directory

func (s *Store) Directory() string

Directory returns the snapshot directory.

func (*Store) Fresh

func (s *Store) Fresh(fetched time.Time) bool

Fresh reports whether a snapshot fetched at the given time is still within the TTL.

func (*Store) Path

func (s *Store) Path(kind, reference string) string

Path returns the snapshot file of a source.

func (*Store) TTL

func (s *Store) TTL() time.Duration

TTL returns the freshness window.

func (*Store) WriteSnapshot

func (s *Store) WriteSnapshot(path string, value any, errTooLarge error) error

WriteSnapshot serializes value and atomically replaces the file at path; a reader sees the old or the new file, never a mix. A value larger than the size bound fails with errTooLarge and leaves the previous file untouched.

Jump to

Keyboard shortcuts

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