Documentation
¶
Overview ¶
Package cache provides bounded, concurrent LRU storage. Callers own value isolation, key identity, configuration defaults, and persistence policies.
Index ¶
- type Cache
- func (c *Cache[T]) Clear() uint64
- func (c *Cache[T]) Delete(key string)
- func (c *Cache[T]) Get(key string, now time.Time) (T, bool)
- func (c *Cache[T]) PeekEntry(key string, now time.Time) (Entry[T], bool)
- func (c *Cache[T]) PruneExpired(now time.Time)
- func (c *Cache[T]) Restore(entries []Entry[T], now time.Time)
- func (c *Cache[T]) Set(key string, value T, now time.Time) bool
- func (c *Cache[T]) SetPreserveExpiry(key string, value T, now time.Time) (time.Time, bool)
- func (c *Cache[T]) SetWithExpiry(key string, value T, now, expiresAt time.Time) bool
- func (c *Cache[T]) SetWithExpiryAndWeight(key string, value T, weight int64, now, expiresAt time.Time) bool
- func (c *Cache[T]) Snapshot(now time.Time) Snapshot[T]
- func (c *Cache[T]) Stats() Stats
- type Config
- type Entry
- type Snapshot
- type Stats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[T any] struct { // contains filtered or unexported fields }
Cache stores values without cloning them. Values must remain immutable while shared; callers must copy mutable values before storage and before exposure. The zero value is disabled. Use New to configure a cache.
func (*Cache[T]) Clear ¶
Clear releases all entries and weight while retaining hit/miss counters. It returns the revision at the clear operation, before any concurrent writes.
func (*Cache[T]) Get ¶
Get retrieves and promotes a live entry. Expiry uses a strict After boundary.
func (*Cache[T]) PeekEntry ¶
PeekEntry returns a live entry's metadata without updating LRU order or hit/miss counters.
func (*Cache[T]) PruneExpired ¶
PruneExpired removes all expired entries without affecting hit/miss counters.
func (*Cache[T]) Restore ¶
Restore replaces storage with a snapshot in MRU order, retaining absolute expiries and capacity limits. Expired entries and empty keys are ignored. The first occurrence of a duplicate key wins. Hit/miss counters are retained.
func (*Cache[T]) Set ¶
Set stores or replaces an entry, renewing its TTL and promoting its recency. Empty keys and writes to disabled storage are ignored. Weight estimation runs outside the cache lock. It reports whether the entry remains stored after capacity eviction and optional expiration pruning.
func (*Cache[T]) SetPreserveExpiry ¶
SetPreserveExpiry updates a live entry's value without extending its ExpiresAt. If no live entry exists for key (absent or expired), it removes any expired entry, does not insert value, and returns (time.Time{}, false). Otherwise it returns the preserved ExpiresAt and whether the entry remained stored after capacity eviction and optional expiration pruning.
func (*Cache[T]) SetWithExpiry ¶
SetWithExpiry stores or replaces an entry with an explicit absolute expiry. Already-expired entries (now.After(expiresAt)) or zero expiries are not stored; if an expired entry already exists for key, it is removed. Weight estimation runs outside the cache lock. It reports whether the entry remains stored after capacity eviction and optional pruning.
func (*Cache[T]) SetWithExpiryAndWeight ¶
func (c *Cache[T]) SetWithExpiryAndWeight(key string, value T, weight int64, now, expiresAt time.Time) bool
SetWithExpiryAndWeight stores or replaces an entry with an explicit absolute expiry and a caller-precomputed non-negative weight without invoking Config.Weight under the cache lock. When neither Weight nor MaxBytes is configured, weight is treated as zero.
type Config ¶
type Config[T any] struct { // MaxEntries disables storage when non-positive. MaxEntries int // MaxBytes limits the sum of Weight values when positive. MaxBytes int64 // TTL is fixed from the last Set; reads do not extend it. TTL time.Duration // Weight estimates retained bytes. Nil means no weight accounting. // It must return a non-negative value and must not call back into the cache. Weight func(T) int64 // PruneOnSet scans expired entries after capacity eviction on each Set. // Leave false for constant-time writes apart from capacity eviction. PruneOnSet bool }
Config controls storage without applying domain-specific defaults.
type Snapshot ¶
Snapshot contains live entries in most-recently-used order and the revision captured under the same lock. Values are not cloned.
type Stats ¶
type Stats struct {
Hits int64
Misses int64
Entries int
UsedBytes int64
// Revision changes with stored content, including expiration and eviction.
// Reads that only change LRU order or counters do not change it.
Revision uint64
}
Stats describes storage including expired entries not yet pruned.