Documentation ¶
Overview ¶
Package lru implements an LRU cache.
Package lru implements an LRU cache.
Index ¶
- type Cache
- type Key
- type TypedCache
- func (c *TypedCache[K, V]) Add(key K, value V)
- func (c *TypedCache[K, V]) Clear()
- func (c *TypedCache[K, V]) Get(key K) (value V, ok bool)
- func (c *TypedCache[K, V]) LeastRecent() *V
- func (c *TypedCache[K, V]) Len() int
- func (c *TypedCache[K, V]) MostRecent() *V
- func (c *TypedCache[K, V]) Remove(key K)
- func (c *TypedCache[K, V]) RemoveOldest()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache = TypedCache[Key, any]
Cache is an LRU cache. It is not safe for concurrent access. This is a compatibility alias for TypedCache[Key,any]
type Key ¶
type Key interface{}
A Key may be any value that is comparable. See http://golang.org/ref/spec#Comparison_operators
type TypedCache ¶
type TypedCache[K comparable, V any] struct { // MaxEntries is the maximum number of cache entries before // an item is evicted. Zero means no limit. MaxEntries int // OnEvicted optionally specificies a callback function to be // executed when an typedEntry is purged from the cache. OnEvicted func(key K, value V) // contains filtered or unexported fields }
TypedCache is an LRU cache. It is not safe for concurrent access.
func TypedNew ¶
func TypedNew[K comparable, V any](maxEntries int) *TypedCache[K, V]
TypedNew creates a new Cache (with types). If maxEntries is zero, the cache has no limit and it's assumed that eviction is done by the caller.
func (*TypedCache[K, V]) Add ¶
func (c *TypedCache[K, V]) Add(key K, value V)
Add adds a value to the cache.
func (*TypedCache[K, V]) Clear ¶
func (c *TypedCache[K, V]) Clear()
Clear purges all stored items from the cache.
func (*TypedCache[K, V]) Get ¶
func (c *TypedCache[K, V]) Get(key K) (value V, ok bool)
Get looks up a key's value from the cache.
func (*TypedCache[K, V]) LeastRecent ¶
func (c *TypedCache[K, V]) LeastRecent() *V
LeastRecent returns the least recently used element
func (*TypedCache[K, V]) Len ¶
func (c *TypedCache[K, V]) Len() int
Len returns the number of items in the cache.
func (*TypedCache[K, V]) MostRecent ¶
func (c *TypedCache[K, V]) MostRecent() *V
MostRecent returns the most recently used element
func (*TypedCache[K, V]) Remove ¶
func (c *TypedCache[K, V]) Remove(key K)
Remove removes the provided key from the cache.
func (*TypedCache[K, V]) RemoveOldest ¶
func (c *TypedCache[K, V]) RemoveOldest()
RemoveOldest removes the oldest item from the cache.