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 ¶
- type Cache
- func (c *Cache[K, V]) Add(key K, value V) (evicted bool)
- func (c *Cache[K, V]) Contains(key K) bool
- func (c *Cache[K, V]) ContainsOrAdd(key K, value V) (ok, evicted bool)
- func (c *Cache[K, V]) Get(key K) (value V, ok bool)
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Peek(key K) (value V, ok bool)
- func (c *Cache[K, V]) PeekOrAdd(key K, value V) (previous V, ok, evicted bool)
- func (c *Cache[K, V]) Purge()
- func (c *Cache[K, V]) Remove(key K) (present bool)
- func (c *Cache[K, V]) Resize(size int) (evicted int)
- type ShardedCache
- func (c *ShardedCache[V]) Add(key string, value V) (evicted bool)
- func (c *ShardedCache[V]) Contains(key string) bool
- func (c *ShardedCache[V]) ContainsOrAdd(key string, value V) (ok, evicted bool)
- func (c *ShardedCache[V]) Get(key string) (value V, ok bool)
- func (c *ShardedCache[V]) Len() int
- func (c *ShardedCache[V]) Peek(key string) (value V, ok bool)
- func (c *ShardedCache[V]) PeekOrAdd(key string, value V) (previous V, ok, evicted bool)
- func (c *ShardedCache[V]) Purge()
- func (c *ShardedCache[V]) Remove(key string) (present bool)
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 a thread-safe fixed size LRU cache.
func New ¶
func New[K comparable, V any](size int) (*Cache[K, V], error)
New creates an LRU of the given size.
func NewWithEvict ¶
func NewWithEvict[K comparable, V any](size int, onEvicted func(key K, value V)) (*Cache[K, V], error)
NewWithEvict constructs a fixed size cache with the given eviction callback.
func (*Cache[K, V]) Contains ¶
Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.
func (*Cache[K, V]) 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[K, V]) Peek ¶
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
func (*Cache[K, V]) 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[K, V]) Purge ¶
func (c *Cache[K, V]) Purge()
Purge is used to completely clear the cache.
type ShardedCache ¶
type ShardedCache[V any] struct { // contains filtered or unexported fields }
Cache is a thread-safe fixed size LRU cache.
func NewSharded ¶
func NewSharded[V any](size, shardCount int) (*ShardedCache[V], error)
New creates an LRU of the given size.
func NewShardedWithEvict ¶
func NewShardedWithEvict[V any](size, shardCount int, onEvicted func(key string, value V)) (*ShardedCache[V], error)
NewWithEvict constructs a fixed size cache with the given eviction callback.
func (*ShardedCache[V]) Add ¶
func (c *ShardedCache[V]) Add(key string, value V) (evicted bool)
Add adds a value to the cache. Returns true if an eviction occurred.
func (*ShardedCache[V]) Contains ¶
func (c *ShardedCache[V]) Contains(key string) bool
Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.
func (*ShardedCache[V]) ContainsOrAdd ¶
func (c *ShardedCache[V]) ContainsOrAdd(key string, value V) (ok, evicted bool)
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 (*ShardedCache[V]) Get ¶
func (c *ShardedCache[V]) Get(key string) (value V, ok bool)
Get looks up a key's value from the cache.
func (*ShardedCache[V]) Len ¶
func (c *ShardedCache[V]) Len() int
Len returns the number of items in the cache.
func (*ShardedCache[V]) Peek ¶
func (c *ShardedCache[V]) Peek(key string) (value V, ok bool)
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
func (*ShardedCache[V]) PeekOrAdd ¶
func (c *ShardedCache[V]) PeekOrAdd(key string, value V) (previous V, ok, evicted bool)
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 (*ShardedCache[V]) Purge ¶
func (c *ShardedCache[V]) Purge()
Purge is used to completely clear the cache.
func (*ShardedCache[V]) Remove ¶
func (c *ShardedCache[V]) Remove(key string) (present bool)
Remove removes the provided key from the cache.