cache

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: BSD-3-Clause Imports: 6 Imported by: ? 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 a generic in-memory LRU cache with TTL expiry, single-flight loading, and stale-while-revalidate reads.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] struct {
	// Load fills misses for Get. The bool reports whether the key exists.
	Load func(K) (V, bool, error)
	// Revalidate reloads stale entries. Nil uses the loader of the read that found the entry.
	Revalidate func(K) (V, bool, error)
	// Clone copies values on the way in and out, so callers never share cached memory.
	Clone func(V) V

	// Capacity bounds the number of entries, evicting the least recently used first. Zero means unbounded.
	Capacity int
	// TTL is how long an entry stays fresh. Zero means entries never expire.
	TTL time.Duration
	// JanitorInterval sweeps expired entries in the background at this interval. Zero disables the sweep.
	JanitorInterval time.Duration
	// RevalidateAfter keeps an entry this long past its TTL. Reads in that window return the
	// stale value and reload it in the background; a failed reload keeps the stale value.
	RevalidateAfter time.Duration
	// CacheMisses remembers keys a loader reported as not found until their TTL passes.
	CacheMisses bool
	// contains filtered or unexported fields
}

Cache is an LRU cache configured by its exported fields. Build it with a keyed literal and always use it through a pointer. The fields must not change after first use; replace the whole cache to change its config.

func (*Cache[K, V]) Close

func (c *Cache[K, V]) Close()

Close stops the janitor and waits for it to exit. The cache stays usable without it.

func (*Cache[K, V]) Delete

func (c *Cache[K, V]) Delete(keys ...K)

Delete removes keys from the cache. A load in flight for a key still answers its callers but is not stored.

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(key K) (V, bool, error)

Get returns the value for key, filling a miss with Load when it is set.

func (*Cache[K, V]) GetOrLoad

func (c *Cache[K, V]) GetOrLoad(key K, load func(K) (V, bool, error)) (V, bool, error)

GetOrLoad returns the value for key, filling a miss with load. Concurrent misses for one key share a single load. A nil load only reads the cache.

func (*Cache[K, V]) Keys

func (c *Cache[K, V]) Keys() []K

Keys returns the keys of unexpired values, most recently used first.

func (*Cache[K, V]) Len

func (c *Cache[K, V]) Len() int

Len returns the number of entries, including cached misses and expired entries not yet swept.

func (*Cache[K, V]) Peek

func (c *Cache[K, V]) Peek(key K) (V, bool)

Peek returns the value for key without loading, refreshing its recency, or checking expiry.

func (*Cache[K, V]) Purge

func (c *Cache[K, V]) Purge()

Purge removes every entry. Loads in flight still answer their callers but are not stored.

func (*Cache[K, V]) Range

func (c *Cache[K, V]) Range(f func(K, V) bool)

Range calls f for each unexpired value, most recently used first, until f returns false. It iterates a snapshot, so f may use the cache.

func (*Cache[K, V]) Set

func (c *Cache[K, V]) Set(entries ...Entry[K, V])

Set stores entries, evicting the least recently used ones past Capacity.

func (*Cache[K, V]) Values

func (c *Cache[K, V]) Values() []V

Values returns the unexpired values, most recently used first.

type Entry

type Entry[K comparable, V any] struct {
	Key   K
	Value V
	TTL   time.Duration
}

Entry is a value written with Cache.Set. A zero TTL uses Cache.TTL.

Jump to

Keyboard shortcuts

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