lru

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2025 License: MIT Imports: 1 Imported by: 1

README

Lru

Package lru implements an LRU cache.

Documentation

Overview

Package lru implements an LRU cache.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Elem

type Elem[K comparable, V any] struct {
	Next, Prev *Elem[K, V]
	K          K
	V          V
}

Elem is an element of a linked list.

type List

type List[K comparable, V any] struct {
	Root *Elem[K, V]
	Size int
}

List represents a doubly linked list.

func (*List[K, V]) Init

func (l *List[K, V]) Init() *List[K, V]

Init initializes or clears list l.

func (*List[K, V]) Insert

func (l *List[K, V]) Insert(e *Elem[K, V]) *Elem[K, V]

Insert inserts e after root, increments l's size, and returns e.

func (*List[K, V]) Remove

func (l *List[K, V]) Remove(e *Elem[K, V])

Remove removes e from its list, decrements l's size.

func (*List[K, V]) Update

func (l *List[K, V]) Update(e *Elem[K, V])

Update e to next to root.

type Lru

type Lru[K comparable, V any] struct {
	// Drop is called automatically when an elem is deleted.
	Drop func(k K, v V)
	// Size is the maximum number of cache entries before an item is evicted. Zero means no limit.
	Size int
	List *List[K, V]
	C    map[K]*Elem[K, V]
	M    *sync.Mutex
}

Lru cache. It is safe for concurrent access.

func New

func New[K comparable, V any](size int) *Lru[K, V]

New returns a new LRU cache. If size is zero, the cache has no limit.

func (*Lru[K, V]) Del

func (l *Lru[K, V]) Del(k K)

Del removes the provided key from the cache.

func (*Lru[K, V]) Get

func (l *Lru[K, V]) Get(k K) (v V)

Get looks up a key's value from the cache.

func (*Lru[K, V]) GetExists

func (l *Lru[K, V]) GetExists(k K) (v V, ok bool)

Get looks up a key's value from the cache.

func (*Lru[K, V]) Has

func (l *Lru[K, V]) Has(k K) bool

Has returns true if a key exists.

func (*Lru[K, V]) Len

func (l *Lru[K, V]) Len() int

Len returns the number of items in the cache.

func (*Lru[K, V]) Set

func (l *Lru[K, V]) Set(k K, v V)

Set adds a value to the cache.

Jump to

Keyboard shortcuts

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