Documentation ¶
Overview ¶
Package simplelru provides simple LRU implementation based on build-in container/list.
Index ¶
- type EvictCallback
- type LRU
- func (c *LRU[Key, Value]) Add(key Key, value Value) (evicted bool)
- func (c *LRU[Key, Value]) Contains(key Key) (ok bool)
- func (c *LRU[Key, Value]) Get(key Key) (value Value, ok bool)
- func (c *LRU[Key, Value]) GetOldest() (key Key, value Value, ok bool)
- func (c *LRU[Key, Value]) Keys() []Key
- func (c *LRU[Key, Value]) Len() int
- func (c *LRU[Key, Value]) Peek(key Key) (value Value, ok bool)
- func (c *LRU[Key, Value]) Purge()
- func (c *LRU[Key, Value]) Remove(key Key) (present bool)
- func (c *LRU[Key, Value]) RemoveOldest() (key Key, value Value, ok bool)
- func (c *LRU[Key, Value]) Resize(size int) (evicted int)
- type LRUCache
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EvictCallback ¶
type EvictCallback[Key, Value any] func(key Key, value Value)
EvictCallback is used to get a callback when a cache entry is evicted
type LRU ¶
type LRU[Key comparable, Value any] struct { // contains filtered or unexported fields }
LRU implements a non-thread safe fixed size LRU cache
func NewLRU ¶
func NewLRU[Key comparable, Value any](size int, onEvict EvictCallback[Key, Value]) (*LRU[Key, Value], error)
NewLRU constructs an LRU of the given size
func (*LRU[Key, Value]) Contains ¶
Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.
func (*LRU[Key, Value]) Keys ¶
func (c *LRU[Key, Value]) Keys() []Key
Keys returns a slice of the keys in the cache, from oldest to newest.
func (*LRU[Key, Value]) Peek ¶
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
func (*LRU[Key, Value]) Purge ¶
func (c *LRU[Key, Value]) Purge()
Purge is used to completely clear the cache.
func (*LRU[Key, Value]) Remove ¶
Remove removes the provided key from the cache, returning if the key was contained.
func (*LRU[Key, Value]) RemoveOldest ¶
RemoveOldest removes the oldest item from the cache.
type LRUCache ¶
type LRUCache[Key, Value any] interface { // Adds a value to the cache, returns true if an eviction occurred and // updates the "recently used"-ness of the key. Add(key Key, value Value) bool // Returns key's value from the cache and // updates the "recently used"-ness of the key. #value, isFound Get(key Key) (value Value, ok bool) // Checks if a key exists in cache without updating the recent-ness. Contains(key Key) (ok bool) // Returns key's value without updating the "recently used"-ness of the key. Peek(key Key) (value Value, ok bool) // Removes a key from the cache. Remove(key Key) bool // Removes the oldest entry from cache. RemoveOldest() (Key, Value, bool) // Returns the oldest entry from the cache. #key, value, isFound GetOldest() (Key, Value, bool) // Returns a slice of the keys in the cache, from oldest to newest. Keys() []Key // Returns the number of items in the cache. Len() int // Clears all cache entries. Purge() // Resizes cache, returning number evicted Resize(int) int }
LRUCache is the interface for simple LRU cache.