Documentation
¶
Overview ¶
Package lru provides a least-recently-used cache bounded by entry count and total cost.
Index ¶
- type Cache
- func (c *Cache[K, V]) Add(key K, val V) bool
- func (c *Cache[K, V]) All() iter.Seq2[K, V]
- func (c *Cache[K, V]) Clear()
- func (c *Cache[K, V]) Contains(key K) bool
- func (c *Cache[K, V]) Cost() int64
- func (c *Cache[K, V]) Get(key K) (V, bool)
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Oldest() (K, V, bool)
- func (c *Cache[K, V]) Peek(key K) (V, bool)
- func (c *Cache[K, V]) Remove(key K) bool
- func (c *Cache[K, V]) RemoveFunc(drop func(K, V) bool) int
- type Options
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 { // contains filtered or unexported fields }
Cache is an LRU map. It is not safe for concurrent use; callers guard it with the lock that protects their related state.
func (*Cache[K, V]) Add ¶
Add stores val as the most recently used entry for key, then evicts least-recently-used entries until both bounds hold. A value whose cost alone exceeds MaxCost is not stored, and any previous value is removed; Add reports whether val was stored.
func (*Cache[K, V]) All ¶
All yields the entries from most to least recently used without changing their recency. The cache must not be modified during iteration.
func (*Cache[K, V]) Contains ¶
Contains reports whether key is cached, without changing its recency.
func (*Cache[K, V]) RemoveFunc ¶
RemoveFunc deletes every entry for which drop returns true, visiting from least to most recently used, and returns how many it deleted.
type Options ¶
type Options[K comparable, V any] struct { MaxEntries int MaxCost int64 // Cost weighs an entry against MaxCost; nil weighs every entry 0. Cost func(K, V) int64 // OnEvict is called for each entry removed to satisfy a bound, never // for Remove, RemoveFunc, Clear or a replaced value. OnEvict func(K, V) }
Options bound a Cache. A zero bound is unlimited.