lru

package
v1.23.1 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: Apache-2.0, BSD-2-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package lru implements a 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
}

A Cache is a map that retains a limited number of items. It must be initialized with New, providing a maximum capacity for the cache. Only the least recently used items are retained.

func New

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

New must be used to create a new Cache.

func (*Cache[K, V]) ConsistencyCheck

func (c *Cache[K, V]) ConsistencyCheck() error

ConsistencyCheck runs a series of checks to ensure that the cache's data structures are consistent. It is not normally required, and it is primarily used in testing.

func (*Cache[K, V]) Get

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

Get returns the value stored in the cache. If the item isn't present, it will return false.

func (*Cache[K, V]) Has

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

Has returns true if the cache has an entry for k. It will not update the timestamp on the item.

func (*Cache[K, V]) Len

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

Len returns the number of items currently in the cache.

func (*Cache[K, V]) Store

func (c *Cache[K, V]) Store(k K, v V)

Store adds the value v to the cache under the k.

type StringKeyCache

type StringKeyCache[V any] struct {
	*Cache[string, V]
}

StringKeyCache is a convenience wrapper for cache keyed by string.

func NewStringKeyCache

func NewStringKeyCache[V any](icap int) *StringKeyCache[V]

NewStringKeyCache creates a new LRU cache keyed by string.

Jump to

Keyboard shortcuts

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