Documentation ¶
Overview ¶
Package lru implements generically-typed LRU caches.
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]) Capacity() int
- 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 ¶
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 ¶
func NewBasicLRU[K comparable, V any](capacity int) BasicLRU[K, V]
NewBasicLRU creates a new LRU cache.
func (*BasicLRU[K, V]) Add ¶
Add adds a value to the cache. Returns true if an item was evicted to store the new item.
func (*BasicLRU[K, V]) Get ¶
Get retrieves a value from the cache. This marks the key as recently used.
func (*BasicLRU[K, V]) GetOldest ¶
GetOldest retrieves the least-recently-used item. Note that this does not update the item's recency.
func (*BasicLRU[K, V]) Keys ¶
func (c *BasicLRU[K, V]) Keys() []K
Keys returns all keys in the cache.
func (*BasicLRU[K, V]) Peek ¶
Peek retrieves a value from the cache, but does not mark the key as recently used.
func (*BasicLRU[K, V]) Remove ¶
Remove drops an item from the cache. Returns true if the key was present in cache.
func (*BasicLRU[K, V]) RemoveOldest ¶
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 ¶
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]) Get ¶
Get retrieves a value from the cache. This marks the key as recently used.
func (*Cache[K, V]) Keys ¶
func (c *Cache[K, V]) Keys() []K
Keys returns all keys of items currently in the LRU.
type SizeConstrainedCache ¶
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 ¶
func NewSizeConstrainedCache[K comparable, V blobType](maxSize uint64) *SizeConstrainedCache[K, V]
NewSizeConstrainedCache creates a new size-constrained LRU cache.
func (*SizeConstrainedCache[K, V]) Add ¶
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 ¶
func (c *SizeConstrainedCache[K, V]) Get(key K) (V, bool)
Get looks up a key's value from the cache.