cache

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package cache provides cache implementations for the application layer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRU

type LRU[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LRU is a thread-safe LRU (Least Recently Used) cache with a fixed capacity. It implements port.Cache[K, V].

When the cache reaches capacity, the least recently accessed entry is evicted to make room for new entries. Both Get and Set operations mark an entry as recently used.

func NewLRU

func NewLRU[K comparable, V any](capacity int) *LRU[K, V]

NewLRU creates a new LRU cache with the given capacity. Capacity must be positive; if zero or negative, a capacity of 1 is used.

func (*LRU[K, V]) Clear

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

Clear removes all items from the cache.

func (*LRU[K, V]) Get

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

Get retrieves a value by key and marks it as recently used. Returns the value and true if found, or the zero value and false if not found.

func (*LRU[K, V]) Len

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

Len returns the number of items currently in the cache.

func (*LRU[K, V]) Remove

func (c *LRU[K, V]) Remove(key K)

Remove deletes a key from the cache. If the key doesn't exist, this is a no-op.

func (*LRU[K, V]) Set

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

Set adds or updates a value in the cache. If the key already exists, its value is updated and it's marked as recently used. If the cache is at capacity, the least recently used entry is evicted.

Jump to

Keyboard shortcuts

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