Documentation ¶
Overview ¶
Package simplelru provides simple LRU implementation based on build-in container/list.
Index ¶
- Constants
- type EvictCallback
- type LRU
- func (c *LRU[K, V]) Add(key K, value V) (evicted bool)
- func (c *LRU[K, V]) Contains(key K) (ok bool)
- func (c *LRU[K, V]) Get(key K) (value V, ok bool)
- func (c *LRU[K, V]) Len() int
- func (c *LRU[K, V]) Peek(key K) (value V, ok bool)
- func (c *LRU[K, V]) Purge()
- func (c *LRU[K, V]) Remove(key K) (present bool)
- func (c *LRU[K, V]) Resize(size int) (evicted int)
- type LRUCache
Constants ¶
const LRUStructSize = 104
LRUStructSize is the size of the LRU struct -- there is a unit test to ensure this const matches the size measured with `unsafe.Sizeof`. TODO: move this to a file that is built only on 64-bit architectures and calculate the right size for 32-byte architectures
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EvictCallback ¶
type EvictCallback[K comparable, V any] func(key K, value V)
EvictCallback is used to get a callback when a cache entry is evicted
type LRU ¶
type LRU[K comparable, V any] struct { // contains filtered or unexported fields }
LRU implements a non-thread safe, fixed size, approximate LRU cache. Rather than a linked list encoding a strict LRU relationship, we approximate it by comparing 8 random entries and evicting the oldest.
func NewLRU ¶
func NewLRU[K comparable, V any](size int, onEvict EvictCallback[K, V]) (*LRU[K, V], error)
NewLRU constructs an LRU of the given size. Memory for the full capacity of the LRU cache is allocated upfront.
func (*LRU[K, V]) Contains ¶
Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.
func (*LRU[K, V]) Peek ¶
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
type LRUCache ¶
type LRUCache[K comparable, V 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 K, value V) bool // Returns key's value from the cache and // updates the "recently used"-ness of the key. #value, isFound Get(key K) (value V, ok bool) // Checks if a key exists in cache without updating the recent-ness. Contains(key K) (ok bool) // Returns key's value without updating the "recently used"-ness of the key. Peek(key K) (value V, ok bool) // Removes a key from the cache. Remove(key K) bool // 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.