lru

package
v0.0.0-...-12e214a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewcLRU

func NewcLRU[K comparable, V any](size int, onEvict EvictCallback[K, V]) (*cLRU[K, V], error)

NewcLRU constructs an LRU of the given size

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] interface {
	// Add a key/value, bool if evicted
	Add(key K, value V) bool
	// Get key/value, bool if found
	Get(key K) (value V, ok bool)
	// Contains checks for key
	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
	// Removes the oldest entry from cache.
	RemoveOldest() (K, V, bool)
	// Returns the oldest entry from the cache. #key, value, isFound
	GetOldest() (K, V, bool)
	// Returns a slice of the keys in the cache, from oldest to newest.
	Keys() []K
	// Returns the number of items in the cache.
	Len() int
	// Clears all cache entries.
	Purge()
	// Resizes cache, returning number evicted
	Resize(int) int
	// ContainsOrAdd
	ContainsOrAdd(K, V) (bool, bool)
	// PeekOrAdd
	PeekOrAdd(K, V) (V, bool, bool)
}

LRU is the interface for simple LRU cache.

func New

func New[K comparable, V any](size int, onEvicted func(key K, value V)) (LRU[K, V], error)

New constructs a fixed size cache with the given eviction callback.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL