cache

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cache provides bounded, concurrent LRU storage. Callers own value isolation, key identity, configuration defaults, and persistence policies.

Index

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 New

func New[T any](config Config[T]) *Cache[T]

New constructs empty storage with independent capacity and expiry policies.

func (*Cache[T]) Clear

func (c *Cache[T]) Clear() uint64

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]) Delete

func (c *Cache[T]) Delete(key string)

Delete removes an entry without affecting hit/miss counters.

func (*Cache[T]) Get

func (c *Cache[T]) Get(key string, now time.Time) (T, bool)

Get retrieves and promotes a live entry. Expiry uses a strict After boundary.

func (*Cache[T]) PeekEntry

func (c *Cache[T]) PeekEntry(key string, now time.Time) (Entry[T], bool)

PeekEntry returns a live entry's metadata without updating LRU order or hit/miss counters.

func (*Cache[T]) PruneExpired

func (c *Cache[T]) PruneExpired(now time.Time)

PruneExpired removes all expired entries without affecting hit/miss counters.

func (*Cache[T]) Restore

func (c *Cache[T]) Restore(entries []Entry[T], now time.Time)

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

func (c *Cache[T]) Set(key string, value T, now time.Time) bool

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

func (c *Cache[T]) SetPreserveExpiry(key string, value T, now time.Time) (time.Time, bool)

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

func (c *Cache[T]) SetWithExpiry(key string, value T, now, expiresAt time.Time) bool

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.

func (*Cache[T]) Snapshot

func (c *Cache[T]) Snapshot(now time.Time) Snapshot[T]

Snapshot copies live entry metadata in MRU order without changing storage.

func (*Cache[T]) Stats

func (c *Cache[T]) Stats() Stats

Stats returns a consistent view without expiring or promoting entries.

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 Entry

type Entry[T any] struct {
	Key       string
	Value     T
	ExpiresAt time.Time
}

Entry is a shallow snapshot of a cached value and its absolute expiry.

type Snapshot

type Snapshot[T any] struct {
	Entries  []Entry[T]
	Revision uint64
}

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.

Jump to

Keyboard shortcuts

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