Documentation
¶
Overview ¶
Package version contains the lower-level primitives behind cascache's authoritative version state.
Most applications should use cascache.Version through the main cache API. This package is mainly for Store implementations and advanced integrations that need to work with Fence, Snapshot, or CacheKey values directly.
Index ¶
- type CacheKey
- type Fence
- func (f Fence) AppendBinary(dst []byte) []byte
- func (f Fence) AppendText(dst []byte) []byte
- func (f Fence) Equal(other Fence) bool
- func (f Fence) MarshalBinary() ([]byte, error)
- func (f Fence) MarshalText() ([]byte, error)
- func (f Fence) String() string
- func (f *Fence) UnmarshalBinary(b []byte) error
- func (f *Fence) UnmarshalText(text []byte) error
- type LocalStore
- func (s *LocalStore) Advance(_ context.Context, k CacheKey) (Snapshot, error)
- func (s *LocalStore) Cleanup(retention time.Duration)
- func (s *LocalStore) Close(_ context.Context) error
- func (s *LocalStore) CreateIfMissing(_ context.Context, k CacheKey) (Snapshot, bool, error)
- func (s *LocalStore) Snapshot(_ context.Context, k CacheKey) (Snapshot, error)
- func (s *LocalStore) SnapshotMany(_ context.Context, ks []CacheKey) (map[CacheKey]Snapshot, error)
- type Snapshot
- type Store
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CacheKey ¶
type CacheKey struct {
// contains filtered or unexported fields
}
CacheKey is an canonical single-key identity generated by cascache. It is not a logical user key and must be treated as exact bytes.
func NewCacheKey ¶
NewCacheKey constructs a canonical cache identity from its exact bytes. This is mainly for cascache internals, Store implementations, and tests. Application code should normally work with logical keys through CAS methods.
type Fence ¶
type Fence struct {
// contains filtered or unexported fields
}
Fence is the authoritative compare-only freshness token for one key.
Fence values are opaque. Callers and Store implementations may compare them for equality and may store/restore them through the canonical text or binary helpers below, but must not infer ordering or other semantics from the token contents.
For correctness, a live key must receive a fresh fence whenever authoritative state is created or bumped. This includes recreation after retention cleanup or TTL expiry so old cached values cannot become valid again through ABA.
func ParseFence ¶
ParseFence parses the canonical text form produced by Fence.String.
func ParseFenceBinary ¶
ParseFenceBinary parses the canonical binary form of a fence.
func (Fence) AppendBinary ¶
AppendBinary appends the canonical fixed-width binary form of f to dst. The appended suffix is always exactly fenceTokenSize bytes. Callers that encode fixed-layout frames may rely on this width remaining stable for the current wire format.
func (Fence) AppendText ¶
func (Fence) MarshalBinary ¶
MarshalBinary implements encoding.BinaryMarshaler.
func (Fence) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Fence) UnmarshalBinary ¶
UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (*Fence) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type LocalStore ¶
type LocalStore struct {
// contains filtered or unexported fields
}
LocalStore keeps authoritative fence state in-process (no network I/O). Optionally starts a background cleanup goroutine that periodically prunes keys whose authoritative state hasn't changed for at least `retention` duration.
- Reads take a shared RLock (Snapshot, SnapshotMany).
- Advances take an exclusive Lock and are O(1).
- Cleanup takes an exclusive Lock and scans the map.
Ctx parameters are accepted to satisfy the Store interface, but are ignored because all operations are local and non-blocking.
func NewLocal ¶
func NewLocal() *LocalStore
NewLocal constructs the in-process authoritative fence store used by the cache by default. It does not start background cleanup and skips timestamp tracking because strict mode never prunes metadata.
func NewLocalWithCleanup ¶
func NewLocalWithCleanup(cleanupInterval, retention time.Duration) *LocalStore
NewLocalWithCleanup constructs a LocalStore with optional background cleanup.
If both cleanupInterval > 0 and retention > 0, a background goroutine is started that calls Cleanup(retention) every cleanupInterval. If either is non-positive, no background cleanup runs and you may call Cleanup manually.
func (*LocalStore) Advance ¶
Advance moves the authoritative state for key k to a fresh fence and updates UpdatedAt. Missing keys are created with a fresh fence.
func (*LocalStore) Cleanup ¶
func (s *LocalStore) Cleanup(retention time.Duration)
Cleanup removes keys whose UpdatedAt is older than retention ago.
func (*LocalStore) Close ¶
func (s *LocalStore) Close(_ context.Context) error
Close stops the optional cleanup goroutine and releases the ticker.
func (*LocalStore) CreateIfMissing ¶
CreateIfMissing creates authoritative state with a fresh fence when the key is currently missing.
func (*LocalStore) Snapshot ¶
Snapshot returns the current authoritative state for key k.
Missing keys are reported as Exists=false.
func (*LocalStore) SnapshotMany ¶
SnapshotMany returns the current authoritative state for all requested keys.
type Snapshot ¶
Snapshot is the authoritative per-key version state. Exists=false means the version store has no current record for the key.
type Store ¶
type Store interface {
// Snapshot returns the current authoritative version state for a
// canonical single-key identity.
Snapshot(ctx context.Context, cacheKey CacheKey) (Snapshot, error)
// SnapshotMany returns authoritative version state for many canonical
// single-key identities. Missing keys must be represented as Exists=false.
SnapshotMany(ctx context.Context, cacheKeys []CacheKey) (map[CacheKey]Snapshot, error)
// CreateIfMissing creates explicit authoritative state only when the key is
// currently missing. A created record starts with a fresh fence.
CreateIfMissing(ctx context.Context, cacheKey CacheKey) (snap Snapshot, created bool, err error)
// Advance moves authoritative state for a canonical single-key identity to a
// fresh fence. It must replace any current live fence with a fresh unequal
// fence. If the key is missing, it creates authoritative state with a fresh
// fence.
Advance(ctx context.Context, cacheKey CacheKey) (Snapshot, error)
// Cleanup prunes old metadata if applicable (no-op for Redis).
Cleanup(retention time.Duration)
// Close releases resources (no-op ok).
Close(context.Context) error
}
Store abstracts where authoritative fences live. Use LocalStore (default) for in-process version state, or redis.NewVersionStore for distributed version state.
CacheKey values are opaque canonical identities generated by cascache. Implementations must treat them as exact bytes and must not try to parse, normalize, or reconstruct them from logical user keys.