lru

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package lru provides a thread‑safe LRU (Least Recently Used) cache implementation with generics. It supports O(1) Get, Set and Clear operations.

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 is a thread‑safe LRU cache that stores key‑value pairs up to a fixed capacity. When the cache is full, the least recently used entry is evicted automatically. The zero value is not usable; use NewCache to create a cache.

func NewCache

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

NewCache creates a new LRU cache with the given capacity. The capacity must be positive; if capacity is less than or equal to zero, the cache will never store any items.

func (*Cache[K, V]) Clear

func (c *Cache[K, V]) Clear()

Clear removes all entries from the cache, leaving it empty. The capacity remains unchanged.

func (*Cache[K, V]) Get

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

Get retrieves the value for a given key. It returns the value and true if the key exists; otherwise it returns the zero value of V and false. When the key exists, the entry is marked as the most recently used.

func (*Cache[K, V]) Set

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

Set adds or updates a key‑value pair in the cache. If the key already exists, its value is updated and the entry becomes the most recently used. If the cache is at capacity, the least recently used entry is evicted before inserting the new one.

Jump to

Keyboard shortcuts

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