version

package
v3.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

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

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

func NewCacheKey(raw string) CacheKey

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.

func (CacheKey) String

func (k CacheKey) String() string

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 NewFence

func NewFence() (Fence, error)

NewFence returns a fresh random authoritative fence token.

func ParseFence

func ParseFence(raw string) (Fence, error)

ParseFence parses the canonical text form produced by Fence.String.

func ParseFenceBinary

func ParseFenceBinary(b []byte) (Fence, error)

ParseFenceBinary parses the canonical binary form of a fence.

func (Fence) AppendBinary

func (f Fence) AppendBinary(dst []byte) []byte

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 (f Fence) AppendText(dst []byte) []byte

func (Fence) Equal

func (f Fence) Equal(other Fence) bool

func (Fence) MarshalBinary

func (f Fence) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler.

func (Fence) MarshalText

func (f Fence) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Fence) String

func (f Fence) String() string

func (*Fence) UnmarshalBinary

func (f *Fence) UnmarshalBinary(b []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler.

func (*Fence) UnmarshalText

func (f *Fence) UnmarshalText(text []byte) error

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

func (s *LocalStore) Advance(_ context.Context, k CacheKey) (Snapshot, error)

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

func (s *LocalStore) CreateIfMissing(_ context.Context, k CacheKey) (Snapshot, bool, error)

CreateIfMissing creates authoritative state with a fresh fence when the key is currently missing.

func (*LocalStore) Snapshot

func (s *LocalStore) Snapshot(_ context.Context, k CacheKey) (Snapshot, error)

Snapshot returns the current authoritative state for key k.

Missing keys are reported as Exists=false.

func (*LocalStore) SnapshotMany

func (s *LocalStore) SnapshotMany(_ context.Context, ks []CacheKey) (map[CacheKey]Snapshot, error)

SnapshotMany returns the current authoritative state for all requested keys.

type Snapshot

type Snapshot struct {
	Fence  Fence
	Exists bool
}

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.

Jump to

Keyboard shortcuts

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