Documentation
¶
Overview ¶
Package lru implements cache with least recent used eviction policy.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] interface { Get(key K) *V Set(key K, value V) *Evicted[K, V] Len() int Remove(key K) *V Peek(key K) *V }
Cache defines minimal interface for all cache implementations.
type Evicted ¶ added in v1.2.0
type Evicted[K comparable, V any] struct { Key K Value V }
Evicted holds key/value pair that was evicted from cache.
type LRU ¶
type LRU[K comparable, V any] struct { // contains filtered or unexported fields }
LRU implements Cache interface with least recent used eviction policy.
Example ¶
package main import ( "fmt" "github.com/floatdrop/lru" ) func main() { cache := lru.New[string, int](256) cache.Set("Hello", 5) if e := cache.Get("Hello"); e != nil { fmt.Println(*cache.Get("Hello")) } }
Output: 5
func New ¶
func New[K comparable, V any](size int) *LRU[K, V]
New creates LRU cache with size capacity. Cache will preallocate size count of internal structures to avoid allocation in process.
func (*LRU[K, V]) Get ¶
func (L *LRU[K, V]) Get(key K) *V
Get returns pointer to value for key, if value was in cache (nil returned otherwise).
func (*LRU[K, V]) Peek ¶ added in v1.1.0
func (L *LRU[K, V]) Peek(key K) *V
Peek returns value for key (if key was in cache), but does not modify its recency.
type Nop ¶
type Nop[K comparable, V any] struct{}
Nop implements Cache interface, but does nothing inside (all sets are ignored, all get operations return nil).
func (*Nop[K, V]) Get ¶
func (n *Nop[K, V]) Get(key K) *V
Get method of Nop cache always returns nil.
func (*Nop[K, V]) Peek ¶ added in v1.1.0
func (n *Nop[K, V]) Peek(key K) *V
Peek method of Nop cache always returns nil.