Documentation
¶
Overview ¶
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.
All caches in this package take locks while operating, and are therefore thread-safe for consumers.
Index ¶
- Constants
- type ARCCache
- func (c *ARCCache[Key, Value]) Add(key Key, value Value)
- func (c *ARCCache[Key, Value]) Contains(key Key) bool
- func (c *ARCCache[Key, Value]) Get(key Key) (value Value, ok bool)
- func (c *ARCCache[Key, Value]) Keys() []Key
- func (c *ARCCache[Key, Value]) Len() int
- func (c *ARCCache[Key, Value]) Peek(key Key) (value Value, ok bool)
- func (c *ARCCache[Key, Value]) Purge()
- func (c *ARCCache[Key, Value]) Remove(key Key)
- type Cache
- func (c *Cache[Key, Value]) Add(key Key, value Value) (evicted bool)
- func (c *Cache[Key, Value]) Contains(key Key) bool
- func (c *Cache[Key, Value]) ContainsOrAdd(key Key, value Value) (ok, evicted bool)
- func (c *Cache[Key, Value]) Get(key Key) (value Value, ok bool)
- func (c *Cache[Key, Value]) GetOldest() (key Key, value Value, ok bool)
- func (c *Cache[Key, Value]) Keys() []Key
- func (c *Cache[Key, Value]) Len() int
- func (c *Cache[Key, Value]) Peek(key Key) (value Value, ok bool)
- func (c *Cache[Key, Value]) PeekOrAdd(key Key, value Value) (previous Value, ok, evicted bool)
- func (c *Cache[Key, Value]) Purge()
- func (c *Cache[Key, Value]) Remove(key Key) (present bool)
- func (c *Cache[Key, Value]) RemoveOldest() (key Key, value Value, ok bool)
- func (c *Cache[Key, Value]) Resize(size int) (evicted int)
- type TwoQueueCache
- func (c *TwoQueueCache[Key, Value]) Add(key Key, value Value)
- func (c *TwoQueueCache[Key, Value]) Contains(key Key) bool
- func (c *TwoQueueCache[Key, Value]) Get(key Key) (value Value, ok bool)
- func (c *TwoQueueCache[Key, Value]) Keys() []Key
- func (c *TwoQueueCache[Key, Value]) Len() int
- func (c *TwoQueueCache[Key, Value]) Peek(key Key) (value Value, ok bool)
- func (c *TwoQueueCache[Key, Value]) Purge()
- func (c *TwoQueueCache[Key, Value]) Remove(key Key)
Constants ¶
const ( // Default2QRecentRatio is the ratio of the 2Q cache dedicated // to recently added entries that have only been accessed once. Default2QRecentRatio = 0.25 // Default2QGhostEntries is the default ratio of ghost // entries kept to track entries recently evicted Default2QGhostEntries = 0.50 )
const (
// DefaultEvictedBufferSize defines the default buffer size to store evicted key/val
DefaultEvictedBufferSize = 16
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ARCCache ¶
type ARCCache[Key, Value any] struct { // contains filtered or unexported fields }
ARCCache is a thread-safe fixed size Adaptive Replacement Cache (ARC). ARC is an enhancement over the standard LRU cache in that tracks both frequency and recency of use. This avoids a burst in access to new entries from evicting the frequently used older entries. It adds some additional tracking overhead to a standard LRU cache, computationally it is roughly 2x the cost, and the extra memory overhead is linear with the size of the cache. ARC has been patented by IBM, but is similar to the TwoQueueCache (2Q) which requires setting parameters.
func NewARC ¶
func NewARC[Key comparable, Value any](size int) (*ARCCache[Key, Value], error)
NewARC creates an ARC of the given size
func (*ARCCache[Key, Value]) Add ¶
func (c *ARCCache[Key, Value]) Add(key Key, value Value)
Add adds a value to the cache.
func (*ARCCache[Key, Value]) Contains ¶
Contains is used to check if the cache contains a key without updating recency or frequency.
func (*ARCCache[Key, Value]) Keys ¶
func (c *ARCCache[Key, Value]) Keys() []Key
Keys returns all the cached keys
func (*ARCCache[Key, Value]) Peek ¶
Peek is used to inspect the cache value of a key without updating recency or frequency.
type Cache ¶
type Cache[Key comparable, Value any] struct { // contains filtered or unexported fields }
Cache is a thread-safe fixed size LRU cache.
func New ¶
func New[Key comparable, Value any](size int) (*Cache[Key, Value], error)
New creates an LRU of the given size.
func NewWithEvict ¶
func NewWithEvict[Key comparable, Value any](size int, onEvicted func(key Key, value Value)) (c *Cache[Key, Value], err error)
NewWithEvict constructs a fixed size cache with the given eviction callback.
func (*Cache[Key, Value]) Add ¶
Add adds a value to the cache. Returns true if an eviction occurred.
func (*Cache[Key, Value]) Contains ¶
Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.
func (*Cache[Key, Value]) ContainsOrAdd ¶
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.
func (*Cache[Key, Value]) Keys ¶
func (c *Cache[Key, Value]) Keys() []Key
Keys returns a slice of the keys in the cache, from oldest to newest.
func (*Cache[Key, Value]) Peek ¶
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
func (*Cache[Key, Value]) PeekOrAdd ¶
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.
func (*Cache[Key, Value]) Purge ¶
func (c *Cache[Key, Value]) Purge()
Purge is used to completely clear the cache.
func (*Cache[Key, Value]) RemoveOldest ¶
RemoveOldest removes the oldest item from the cache.
type TwoQueueCache ¶
type TwoQueueCache[Key, Value any] struct { // contains filtered or unexported fields }
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.
func New2Q ¶
func New2Q[Key comparable, Value any](size int) (*TwoQueueCache[Key, Value], error)
New2Q creates a new TwoQueueCache using the default values for the parameters.
func New2QParams ¶
func New2QParams[Key comparable, Value any](size int, recentRatio, ghostRatio float64) (*TwoQueueCache[Key, Value], error)
New2QParams creates a new TwoQueueCache using the provided parameter values.
func (*TwoQueueCache[Key, Value]) Add ¶
func (c *TwoQueueCache[Key, Value]) Add(key Key, value Value)
Add adds a value to the cache.
func (*TwoQueueCache[Key, Value]) Contains ¶
func (c *TwoQueueCache[Key, Value]) Contains(key Key) bool
Contains is used to check if the cache contains a key without updating recency or frequency.
func (*TwoQueueCache[Key, Value]) Get ¶
func (c *TwoQueueCache[Key, Value]) Get(key Key) (value Value, ok bool)
Get looks up a key's value from the cache.
func (*TwoQueueCache[Key, Value]) Keys ¶
func (c *TwoQueueCache[Key, Value]) Keys() []Key
Keys returns a slice of the keys in the cache. The frequently used keys are first in the returned slice.
func (*TwoQueueCache[Key, Value]) Len ¶
func (c *TwoQueueCache[Key, Value]) Len() int
Len returns the number of items in the cache.
func (*TwoQueueCache[Key, Value]) Peek ¶
func (c *TwoQueueCache[Key, Value]) Peek(key Key) (value Value, ok bool)
Peek is used to inspect the cache value of a key without updating recency or frequency.
func (*TwoQueueCache[Key, Value]) Purge ¶
func (c *TwoQueueCache[Key, Value]) Purge()
Purge is used to completely clear the cache.
func (*TwoQueueCache[Key, Value]) Remove ¶
func (c *TwoQueueCache[Key, Value]) Remove(key Key)
Remove removes the provided key from the cache.