package lru

Import Path
	github.com/hashicorp/golang-lru/v2 (on go.dev)

Dependency Relation
	imports 3 packages, and imported by one package

Involved Source Files 2q.go Package lru provides three different LRU caches of varying sophistication. Cache is a simple LRU cache. It is based on the LRU implementation in groupcache: https://github.com/golang/groupcache/tree/master/lru TwoQueueCache tracks frequently used and recently used entries separately. This avoids a burst of accesses from taking out frequently used entries, at the cost of about 2x computational overhead and some extra bookkeeping. ARCCache is an adaptive replacement cache. It tracks recent evictions as well as recent usage in both the frequent and recent caches. Its computational overhead is comparable to TwoQueueCache, but the memory overhead is linear with the size of the cache. ARC has been patented by IBM, so do not use it if that is problematic for your program. For this reason, it is in a separate go module contained within this repository. All caches in this package take locks while operating, and are therefore thread-safe for consumers. lru.go
Package-Level Type Names (total 2)
/* sort by: | */
Type Parameters: K: comparable V: any Cache is a thread-safe fixed size LRU cache. Add adds a value to the cache. Returns true if an eviction occurred. Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale. ContainsOrAdd checks if a key is in the cache without updating the recent-ness or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred. Get looks up a key's value from the cache. GetOldest returns the oldest entry Keys returns a slice of the keys in the cache, from oldest to newest. Len returns the number of items in the cache. Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key. PeekOrAdd checks if a key is in the cache without updating the recent-ness or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred. Purge is used to completely clear the cache. Remove removes the provided key from the cache. RemoveOldest removes the oldest item from the cache. Resize changes the cache size. Values returns a slice of the values in the cache, from oldest to newest. func New[K, V](size int) (*Cache[K, V], error) func NewWithEvict[K, V](size int, onEvicted func(key K, value V)) (c *Cache[K, V], err error)
Type Parameters: K: comparable V: any TwoQueueCache is a thread-safe fixed size 2Q cache. 2Q is an enhancement over the standard LRU cache in that it tracks both frequently and recently used entries separately. This avoids a burst in access to new entries from evicting frequently used entries. It adds some additional tracking overhead to the standard LRU cache, and is computationally about 2x the cost, and adds some metadata over head. The ARCCache is similar, but does not require setting any parameters. Add adds a value to the cache. Contains is used to check if the cache contains a key without updating recency or frequency. Get looks up a key's value from the cache. Keys returns a slice of the keys in the cache. The frequently used keys are first in the returned slice. Len returns the number of items in the cache. Peek is used to inspect the cache value of a key without updating recency or frequency. Purge is used to completely clear the cache. Remove removes the provided key from the cache. Resize changes the cache size. Values returns a slice of the values in the cache. The frequently used values are first in the returned slice. func New2Q[K, V](size int) (*TwoQueueCache[K, V], error) func New2QParams[K, V](size int, recentRatio, ghostRatio float64) (*TwoQueueCache[K, V], error)
Package-Level Functions (total 4)
Type Parameters: K: comparable V: any New creates an LRU of the given size.
Type Parameters: K: comparable V: any New2Q creates a new TwoQueueCache using the default values for the parameters.
Type Parameters: K: comparable V: any New2QParams creates a new TwoQueueCache using the provided parameter values.
Type Parameters: K: comparable V: any NewWithEvict constructs a fixed size cache with the given eviction callback.
Package-Level Constants (total 3)
Default2QGhostEntries is the default ratio of ghost entries kept to track entries recently evicted
Default2QRecentRatio is the ratio of the 2Q cache dedicated to recently added entries that have only been accessed once.
DefaultEvictedBufferSize defines the default buffer size to store evicted key/val