cache

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 7 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLRUCache

func NewLRUCache[K comparable, V any](size, buffer int, onEvict func(map[K]V)) *evictionCache[K, V]

NewLRUCache creates a cache with limited size with LRU eviction policy. It is guaranteed that at least size elements can be kept in the cache. The cache is cleaned up when the cache is full, i.e. it contains size + buffer elements

func NewLRUEviction

func NewLRUEviction[K comparable](size, buffer int, evict func([]K)) *lruEviction[K]

func NewMapCache

func NewMapCache[K comparable, V any]() *mapCache[K, V]

NewMapCache creates a dummy implementation of the Cache interface. It is backed by a map with unlimited capacity.

func NewSecondChanceCache added in v0.25.0

func NewSecondChanceCache[K comparable, V any](size, buffer int, onEvict func(map[K]V)) *evictionCache[K, V]

NewSecondChanceCache creates a cache with limited size with second-chance (CLOCK) eviction, an approximation of LRU that promotes an entry on every read rather than on insertion.

Unlike NewLRUCache, size is the number of entries the cache holds, not a guaranteed minimum: once it is full, the next insertion scans for victims and evicts buffer of them. buffer is clamped to [1, size]. A larger buffer runs the scan, and onEvict, less often. onEvict runs under the cache's write lock and must not call back into the cache.

func NewTimeoutCache

func NewTimeoutCache[K comparable, V any](ctx context.Context, evictionTimeout time.Duration, onEvict func(map[K]V)) *evictionCache[K, V]

NewTimeoutCache creates a cache that keeps elements for evictionTimeout time. An element might return even if it is marked stale. The background cleanup goroutine stops when ctx is cancelled. A non-positive evictionTimeout disables eviction; see NewTimeoutEviction.

func NewTimeoutEviction

func NewTimeoutEviction[K comparable](ctx context.Context, timeout time.Duration, evict func([]K)) *timeoutEviction[K]

NewTimeoutEviction returns an eviction policy that evicts entries older than timeout, driven by a background goroutine that stops when ctx is cancelled.

A non-positive timeout disables eviction entirely and no goroutine is started. Note that time.NewTicker panics on a non-positive duration, so this guard is what keeps a misconfigured timeout from taking the process down from inside the goroutine.

Types

type EvictionPolicy

type EvictionPolicy[K comparable] interface {
	// Push adds a key and must be invoked under write-lock
	Push(K)
	// Touch marks an existing key as recently used. It is invoked from Get,
	// which holds only a read lock, so unlike Push it may run concurrently
	// with itself and must be safe for that. Policies whose eviction order
	// does not depend on reads implement it as a no-op.
	Touch(K)
}

EvictionPolicy decides which keys an evictionCache evicts. The cache calls Push under its write lock, and calls Touch and formats the policy (String) under its read lock, so Push never runs concurrently with either or itself.

type Map

type Map[K comparable, V any] interface {
	Get(K) (V, bool)
	Put(K, V)
	Update(K, func(bool, V) (bool, V)) bool
	Delete(...K)
	Len() int
}

Jump to

Keyboard shortcuts

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