Documentation ¶
Overview ¶
Package lru implements generically-typed LRU caches.
Index ¶
- type BasicLRU
- func (c *BasicLRU[K, V]) Add(key K, value V) (evicted bool)
- func (c *BasicLRU[K, V]) Contains(key K) bool
- func (c *BasicLRU[K, V]) Get(key K) (value V, ok bool)
- func (c *BasicLRU[K, V]) GetOldest() (key K, value V, ok bool)
- func (c *BasicLRU[K, V]) Keys() []K
- func (c *BasicLRU[K, V]) Len() int
- func (c *BasicLRU[K, V]) Peek(key K) (value V, ok bool)
- func (c *BasicLRU[K, V]) Purge()
- func (c *BasicLRU[K, V]) Remove(key K) bool
- func (c *BasicLRU[K, V]) RemoveOldest() (key K, value V, ok bool)
- 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]) Get(key K) (value V, ok bool)
- func (c *Cache[K, V]) Keys() []K
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Peek(key K) (value V, ok bool)
- func (c *Cache[K, V]) Purge()
- func (c *Cache[K, V]) Remove(key K) bool
- type SizeConstrainedCache
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BasicLRU ¶ added in v1.10.35
type BasicLRU[K comparable, V any] struct { // contains filtered or unexported fields }
BasicLRU is a simple LRU cache.
This type is not safe for concurrent use. The zero value is not valid, instances must be created using NewCache.
func NewBasicLRU ¶ added in v1.10.35
func NewBasicLRU[K comparable, V any](capacity int) BasicLRU[K, V]
NewBasicLRU creates a new LRU cache.
func (*BasicLRU[K, V]) Add ¶ added in v1.10.35
Add adds a value to the cache. Returns true if an item was evicted to store the new item.
func (*BasicLRU[K, V]) Contains ¶ added in v1.10.35
Contains reports whether the given key exists in the cache.
func (*BasicLRU[K, V]) Get ¶ added in v1.10.35
Get retrieves a value from the cache. This marks the key as recently used.
func (*BasicLRU[K, V]) GetOldest ¶ added in v1.10.35
GetOldest retrieves the least-recently-used item. Note that this does not update the item's recency.
func (*BasicLRU[K, V]) Keys ¶ added in v1.10.35
func (c *BasicLRU[K, V]) Keys() []K
Keys returns all keys in the cache.
func (*BasicLRU[K, V]) Len ¶ added in v1.10.35
Len returns the current number of items in the cache.
func (*BasicLRU[K, V]) Peek ¶ added in v1.10.35
Peek retrieves a value from the cache, but does not mark the key as recently used.
func (*BasicLRU[K, V]) Purge ¶ added in v1.10.35
func (c *BasicLRU[K, V]) Purge()
Purge empties the cache.
func (*BasicLRU[K, V]) Remove ¶ added in v1.10.35
Remove drops an item from the cache. Returns true if the key was present in cache.
func (*BasicLRU[K, V]) RemoveOldest ¶ added in v1.10.35
RemoveOldest drops the least recently used item.
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
Cache is a LRU cache. This type is safe for concurrent use.
func NewCache ¶ added in v1.10.35
func NewCache[K comparable, V any](capacity int) *Cache[K, V]
NewCache creates an LRU cache.
func (*Cache[K, V]) Add ¶
Add adds a value to the cache. Returns true if an item was evicted to store the new item.
func (*Cache[K, V]) Contains ¶ added in v1.10.35
Contains reports whether the given key exists in the cache.
func (*Cache[K, V]) Get ¶
Get retrieves a value from the cache. This marks the key as recently used.
func (*Cache[K, V]) Keys ¶ added in v1.10.35
func (c *Cache[K, V]) Keys() []K
Keys returns all keys of items currently in the LRU.
func (*Cache[K, V]) Peek ¶ added in v1.10.35
Peek retrieves a value from the cache, but does not mark the key as recently used.
type SizeConstrainedCache ¶ added in v1.10.35
type SizeConstrainedCache[K comparable, V blobType] struct { // contains filtered or unexported fields }
SizeConstrainedCache is a cache where capacity is in bytes (instead of item count). When the cache is at capacity, and a new item is added, older items are evicted until the size constraint is met.
OBS: This cache assumes that items are content-addressed: keys are unique per content. In other words: two Add(..) with the same key K, will always have the same value V.
func NewSizeConstrainedCache ¶ added in v1.10.35
func NewSizeConstrainedCache[K comparable, V blobType](maxSize uint64) *SizeConstrainedCache[K, V]
NewSizeConstrainedCache creates a new size-constrained LRU cache.
func (*SizeConstrainedCache[K, V]) Add ¶ added in v1.10.35
func (c *SizeConstrainedCache[K, V]) Add(key K, value V) (evicted bool)
Add adds a value to the cache. Returns true if an eviction occurred. OBS: This cache assumes that items are content-addressed: keys are unique per content. In other words: two Add(..) with the same key K, will always have the same value V. OBS: The value is _not_ copied on Add, so the caller must not modify it afterwards.
func (*SizeConstrainedCache[K, V]) Get ¶ added in v1.10.35
func (c *SizeConstrainedCache[K, V]) Get(key K) (V, bool)
Get looks up a key's value from the cache.