Documentation
¶
Overview ¶
Package cache provides a generic in-memory LRU cache with TTL expiry, single-flight loading, and stale-while-revalidate reads.
Index ¶
- type Cache
- func (c *Cache[K, V]) Close()
- func (c *Cache[K, V]) Delete(keys ...K)
- func (c *Cache[K, V]) Get(key K) (V, bool, error)
- func (c *Cache[K, V]) GetOrLoad(key K, load func(K) (V, bool, error)) (V, bool, error)
- func (c *Cache[K, V]) Keys() []K
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Peek(key K) (V, bool)
- func (c *Cache[K, V]) Purge()
- func (c *Cache[K, V]) Range(f func(K, V) bool)
- func (c *Cache[K, V]) Set(entries ...Entry[K, V])
- func (c *Cache[K, V]) Values() []V
- type Entry
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]) GetOrLoad ¶
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 ¶
Len returns the number of entries, including cached misses and expired entries not yet swept.
func (*Cache[K, V]) Peek ¶
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 ¶
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.