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 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.
func (*LRU[K, V]) Remove ¶ added in v1.1.0
func (L *LRU[K, V]) Remove(key K) *V
Remove method removes entry associated with key and returns pointer to removed value (or nil if entry was not in cache).