lrucache

package
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package lrucache is a small bounded cache keyed by string: least recently used entries are evicted at capacity, and a value that is missing is built once even when many goroutines ask for it at the same moment.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache holds up to max values. The zero value is not usable; call New.

func New

func New[V any](max int) *Cache[V]

New returns a cache that keeps at most max entries.

func (*Cache[V]) Add

func (c *Cache[V]) Add(key string, v V)

Add stores v under key, replacing any previous value, and drops the least recently used entries until the cache is within capacity.

func (*Cache[V]) Do

func (c *Cache[V]) Do(key string, build func() (V, error)) (V, error)

Do returns the value for key, building and storing it with build when it is missing. Concurrent callers for the same missing key share one build; a build that fails stores nothing and every waiting caller sees the error.

func (*Cache[V]) Get

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

Get returns the value for key and marks it recently used.

func (*Cache[V]) Len

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

Len is the number of entries held.

func (*Cache[V]) Remove

func (c *Cache[V]) Remove(key string)

Remove drops key if present.

func (*Cache[V]) Reset

func (c *Cache[V]) Reset()

Reset drops every entry.

Jump to

Keyboard shortcuts

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