Documentation
¶
Overview ¶
Package cache — hosts the compile-time interface assertions, keeping them out of the production source so the runtime binary carries no diagnostic-only declarations.
Package cache — the L1/L2 chain.
Package cache — the NewChain constructor configuration.
Package cache provides the concrete cache stores implementing internal/core/data/cache: a tagged, stampede-protected memory store over the kernel LRU+TTL primitive, and the chain that puts one store in front of another. Stdlib-only, cross-OS. ADR 0049.
Package cache — the tagged, stampede-protected in-memory store.
Package cache — what the underlying primitive actually stores.
Package cache — the refusals every store shares, and the wrapping rule for a caller's fill error.
Package cache — the two-way tag index behind InvalidateTag.
Package cache — one level of a chained store.
Index ¶
- func NewChain[V any](cfg ChainConfig, stores ...corecache.Store[V]) (chain corecache.Store[V], err error)
- func NewMemory[V any](cfg MemoryConfig) (store corecache.Store[V], err error)
- func NewWeighedMemory[V any](cfg MemoryConfig, weigh func(value V) int64) (store corecache.Store[V], err error)
- type ChainConfig
- type MemoryConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewChain ¶
func NewChain[V any](cfg ChainConfig, stores ...corecache.Store[V]) (chain corecache.Store[V], err error)
NewChain puts each store in front of the next: tiers[0] is the nearest and is consulted first, the last is the authority.
Every tier MUST implement corecache.EntryFetcher and corecache.Tagger. That is checked here, once, and refused with corecache.CacheChainMisconfigured rather than discovered per call — see that sentinel for the reasoning.
The returned value is a corecache.Store and also implements corecache.EntryFetcher, corecache.Tagger, corecache.Loader, corecache.Expirer, corecache.BulkFetcher, corecache.BulkSetter, corecache.Refresher and corecache.Leaser. A negative LeasePoll is refused with corecache.CacheMisconfigured.
IFACE-PLUGIN: the concrete chain stays unexported behind this constructor.
func NewMemory ¶
func NewMemory[V any](cfg MemoryConfig) (store corecache.Store[V], err error)
NewMemory builds an in-process cache store from cfg.
The returned value is a corecache.Store. It also implements corecache.EntryFetcher, corecache.Tagger, corecache.Loader, corecache.Expirer, corecache.BulkFetcher, corecache.BulkSetter, corecache.Refresher and corecache.Leaser; reach them by type assertion, which is the ADR 0039 way a port grows without breaking anyone who implemented it.
A non-positive MaxEntries is REFUSED, not defaulted. ADR 0031 splits on whether the SDK can supply a value without inventing the caller's requirement, and a cache capacity is the requirement: it is the whole statement of how much memory the caller is willing to spend. Worse, the value the primitive gives a zero — unbounded — is the dangerous reading, and it is dangerous twice over here, because an unbounded store also grows an unbounded tag index.
IFACE-PLUGIN: the concrete store stays unexported behind this constructor.
func NewWeighedMemory ¶ added in v0.27.0
func NewWeighedMemory[V any](cfg MemoryConfig, weigh func(value V) int64) (store corecache.Store[V], err error)
NewWeighedMemory builds an in-process cache store bounded by MaxBytes as well as MaxEntries, each value measured by weigh when it is stored (#839). It is NewMemory in every other respect. A nil weigh, or a MaxBytes that is not positive, is refused with corecache.CacheMisconfigured: a weigher with no budget, or a budget with nothing to measure it, is a half-stated bound.
Types ¶
type ChainConfig ¶
type ChainConfig struct {
// OnPromoteError, if non-nil, is called when a value served from a FAR
// tier could not be written into a nearer one.
//
// A failed promotion is not a failed read: the caller already has the
// value, and failing the call would turn a degraded cache into a degraded
// service. But it is also not nothing — a near tier that rejects every
// promotion is a tier that will never be used again, and with this hook nil
// that is completely invisible. Wire it.
OnPromoteError func(key string, err error)
// Clock is what LoadLeased waits on between two reads while another
// process holds a key's lease. nil means clock.System. Nothing else in
// the chain reads time: every tier keeps its own clock.
Clock clock.Timed
// LeasePoll is how often LoadLeased reads the chain again while another
// process holds a key's lease. 0 means 25ms, the file locker's own poll;
// negative is refused.
LeasePoll time.Duration
}
ChainConfig parameterises NewChain. Its zero value builds a working chain: an observation hook, and the clock and step LoadLeased waits on.
type MemoryConfig ¶
type MemoryConfig struct {
// MaxEntries caps the live entry count; the least-recently-used entry is
// evicted above it. It MUST be positive.
MaxEntries int
// DefaultTTL applies to every entry whose own TTL is zero. Zero means the
// default is "no deadline", so entries leave only by eviction, by Delete,
// or by tag invalidation. Negative is refused.
DefaultTTL time.Duration
// Clock is the time source, injectable so TTL is testable. nil means
// clock.System. Only the reading half of the port is required — a cache
// reads time (ADR 0039); LoadLeased alone waits, on Clock when it can wait
// (clock.System, a clock.ManualClock) and on clock.System otherwise. A
// clock.CoarseClock makes a TTL'd hit's clock read an atomic load, and
// every expiry late by up to its resolution.
Clock clock.Clock
// Stripes splits the store into independent stripes, each with its own
// lock, its own share of MaxEntries and MaxBytes and its own tag index, so
// goroutines working on keys of different stripes never wait for one
// another. 0 means 1, one store as before; above MaxEntries (or a positive
// MaxBytes) it is held there, and above 1024 at 1024. Negative is refused.
Stripes int
// MaxBytes bounds the summed weight of the live values, as measured by the
// weigher handed to [NewWeighedMemory]; entries are evicted above it as
// above MaxEntries, both bounds applying. 0 means no byte bound. An entry
// heavier than one stripe's share of it is refused with
// CACHE_ENTRY_REJECTED at Set. Negative is refused, and so is a positive
// MaxBytes given to [NewMemory], which has no weigher to measure it with.
MaxBytes int64
// Policy is the eviction policy: least recently used (the zero), or
// S3-FIFO, which keeps a hot set through a scan.
Policy kcache.Policy
// LeasePoll is how often LoadLeased reads the store again while another
// process holds a key's lease. 0 means 25ms, the file locker's own poll;
// negative is refused.
LeasePoll time.Duration
}
MemoryConfig parameterises NewMemory.
Its zero value is deliberately NOT a working cache: MaxEntries must be stated. See NewMemory for why that refusal, and not a default, is the right ADR 0031 half here.