lrucache

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package lrucache is the one bounded, least-recently-used map the SDK's caching tiers share.

Both callers key on a host an incoming offer named, so the key space is open-ended and caller-influenced — an unbounded map over one is somewhere an authenticated caller can make the process grow without limit. Both therefore need the same structure, and having written it twice the second copy carried the first's rationale paragraph verbatim. One implementation is what stops an eviction fix landing in one and not the other.

Least-recently-used and not drop-the-whole-map: dropping empties the cache exactly when it is under most pressure, and it makes which entries survive a function of the order a caller names hosts — a property the caller controls.

It is internal on purpose. A bounded map is not a protocol concept, so it has no cross-language counterpart to mirror and no business on the SDK's public surface.

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 holds at most cap entries, evicting the least recently used to make room. The zero value is not usable; build one with New.

Safe for concurrent use. Every method takes the same lock, including the loader GetOrCreate runs — so a value is constructed exactly once per key even when two callers race for it.

func New

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

New returns a cache bounded at cap entries. A cap below one is treated as one: a zero-capacity cache would evict what it just stored, which no caller wants and which would make every lookup a miss.

func (*Cache[K, V]) Get

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

Get returns the value stored for key and promotes it to most-recently-used.

func (*Cache[K, V]) GetOrCreate

func (c *Cache[K, V]) GetOrCreate(key K, make func(K) V) V

GetOrCreate returns the value stored for key, building it with make on a miss.

make runs under the cache's lock, so a key is built once however many callers race. That is deliberate for the callers here — both build cheap in-process plumbing, never anything that dials — and it is why this is not a general-purpose memoizer.

func (*Cache[K, V]) Has

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

Has reports whether key is currently held, without promoting it.

func (*Cache[K, V]) Len

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

Len reports how many entries the cache holds, and Has whether one is present without disturbing recency.

Both exist for the eviction tests. The bound has no observable behaviour of its own other than a dial or fetch count, and counting those would mean mocking the very transport the suites deliberately never mock.

func (*Cache[K, V]) Put

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

Put stores val for key, promoting it and evicting the least-recently-used entry once the cache is full. An existing key is updated in place rather than consuming a second slot.

Jump to

Keyboard shortcuts

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