cache

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package cache provides a generic, thread-safe LRU cache with O(log n) eviction.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRU

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

LRU is a generic least-recently-used cache with O(log n) eviction via a min-heap.

func New

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

New creates an LRU cache with the given maximum size.

func (*LRU[K, V]) Delete

func (c *LRU[K, V]) Delete(key K)

Delete removes a key from the cache.

func (*LRU[K, V]) DeleteFunc

func (c *LRU[K, V]) DeleteFunc(fn func(K, V) bool)

DeleteFunc removes all entries for which fn returns true.

func (*LRU[K, V]) Get

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

Get returns the value for the key and true, or the zero value and false. Accessing a key refreshes its position (most recently used).

func (*LRU[K, V]) Len

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

Len returns the number of entries in the cache.

func (*LRU[K, V]) Range

func (c *LRU[K, V]) Range(fn func(K, V) bool)

Range calls fn for each entry in the cache. If fn returns false, iteration stops. The callback receives the key and value; order is not guaranteed.

func (*LRU[K, V]) Set

func (c *LRU[K, V]) Set(key K, value V)

Set adds or updates a key-value pair. If at capacity, the least recently used entry is evicted.

Jump to

Keyboard shortcuts

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