lru

package
v1.3.10 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package lru provides a generic least-recently-used cache.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Cache is a generic LRU cache that is NOT goroutine-safe. Callers must provide their own synchronization.

func New

func New[K comparable, V any](maxSize int, onEvict func(K, V)) *Cache[K, V]

New creates a new LRU cache with the given maximum size. If onEvict is non-nil, it is called whenever an entry is evicted.

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(key K) (V, bool)

Get returns the value for key and marks it as recently used. The second return value reports whether the key was found.

func (*Cache[K, V]) Keys

func (c *Cache[K, V]) Keys() []K

Keys returns all keys in order from most recently used to least.

func (*Cache[K, V]) Len

func (c *Cache[K, V]) Len() int

Len returns the number of items in the cache.

func (*Cache[K, V]) MaxSize

func (c *Cache[K, V]) MaxSize() int

MaxSize returns the configured maximum size.

func (*Cache[K, V]) Put

func (c *Cache[K, V]) Put(key K, value V)

Put adds or updates a key-value pair, evicting the LRU entry if at capacity.

func (*Cache[K, V]) Remove

func (c *Cache[K, V]) Remove(key K) bool

Remove removes a key from the cache. Returns true if the key was present.

Jump to

Keyboard shortcuts

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