cache

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2025 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package cache provides a generic in-memory LRU (Least Recently Used) cache implementation. The cache stores any Go value by serializing it to bytes using MessagePack encoding, making it suitable for a wide variety of data types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Key added in v0.3.1

type Key interface {
	comparable
	~string | ~[]byte | ~[12]byte | ~[16]byte
}

type LRU added in v0.3.1

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

LRU is a generic LRU cache that can store any value type.

func (*LRU[K, V]) Add added in v0.3.1

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

Add inserts a key-value pair into the cache. If the key already exists, its value is updated. Returns an error if the value cannot be serialized.

func (*LRU[K, V]) Bytes added in v0.3.1

func (c *LRU[K, V]) Bytes() int64

Bytes returns the approximate memory usage of the cache in bytes, including both keys and serialized values.

func (*LRU[K, V]) Get added in v0.3.1

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

Get retrieves a value from the cache by its key. Returns the value and a boolean indicating whether the key was found.

func (*LRU[K, V]) Items added in v0.3.1

func (c *LRU[K, V]) Items() int64

Items returns the number of items currently stored in the cache.

func (*LRU[K, V]) Remove added in v0.3.1

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

Remove deletes an item from the cache by its key.

func (*LRU[K, V]) RemoveOldest added in v0.3.1

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

RemoveOldest removes the least recently used item from the cache.

Jump to

Keyboard shortcuts

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