cache

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 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 Cache

type Cache[V any] struct {
	// contains filtered or unexported fields
}

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

func (*Cache[V]) Add

func (c *Cache[V]) Add(key string, 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 (*Cache[V]) Bytes

func (c *Cache[V]) Bytes() int64

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

func (*Cache[V]) Get

func (c *Cache[V]) Get(key string) (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 (*Cache[V]) Items

func (c *Cache[V]) Items() int64

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

func (*Cache[V]) Remove added in v0.3.0

func (c *Cache[V]) Remove(key string)

Remove deletes an item from the cache by its key.

func (*Cache[V]) RemoveOldest

func (c *Cache[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