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
- func Hash(raw []byte) string
- func Key(scope, kind, reference string) string
- func Load[T any, P interface{ ... }](ctx context.Context, store *Store, source *Source[T, P], mode Mode) (P, bool, error)
- func Scope(parts ...any) (string, error)
- type Header
- type Mode
- type Option
- type Snapshot
- type Source
- type Store
Constants ¶
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 ¶
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 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.
Types ¶
type Header ¶
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 ¶
SetSnapshotHeader replaces the header.
func (*Header) SnapshotHeader ¶
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 WithMaxSize ¶
WithMaxSize sets the bound on a snapshot file, in bytes.
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 ¶
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) Fresh ¶
Fresh reports whether a snapshot fetched at the given time is still within the TTL.
func (*Store) WriteSnapshot ¶
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.