lrucache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package lrucache provides a high-performance, thread-safe LRU (Least Recently Used) cache implementation with TTL support and optional persistence.

The cache combines a map—enabling O(1) access—with a doubly linked list to manage the order of usage. Expired entries are removed either upon access (lazily) or by a configurable background routine.

A standout feature is the set of helper methods for "lazy loading" (GetOrLoad), which drastically reduce the boilerplate code required for cache-miss scenarios.

Creation example:

cache := lrucache.New(1000, 15*time.Minute, 1*time.Minute)
defer cache.StopCleanup()

Storage is handled via empty interfaces (interface{}), making the cache flexible enough to accommodate any data type.

----------- German translation ------ Package lrucache bietet eine performante, thread-sichere LRU (Least Recently Used) Cache-Implementierung mit TTL-Unterstützung und optionaler Persistenz.

Der Cache kombiniert eine Map für O(1) Zugriffe mit einer doppelt verketteten Liste, um die Nutzungsreihenfolge zu verwalten. Abgelaufene Einträge werden entweder beim Zugriff (Lazy) oder durch eine konfigurierbare Hintergrund-Routine entfernt.

Ein besonderes Merkmal sind die Helper-Methoden für das "Lazy Loading" (GetOrLoad), die den Boilerplate-Code für Cache-Miss-Szenarien drastisch reduzieren.

Beispiel für die Erstellung:

cache := lrucache.New(1000, 15*time.Minute, 1*time.Minute)
defer cache.StopCleanup()

Die Speicherung erfolgt über leere Interfaces (interface{}), was den Cache flexibel für beliebige Datentypen macht.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheEntry

type CacheEntry struct {
	Key       string
	Value     interface{}
	ExpiresAt time.Time
}

CacheEntry stores key, value, and expiry time

type LRUCache

type LRUCache struct {
	// contains filtered or unexported fields
}

LRUCache is mainstructure

func New

func New(capacity int, ttl time.Duration, cleanupInterval time.Duration) *LRUCache

New creates a new LRU cache

func (*LRUCache) Get

func (c *LRUCache) Get(key string) (interface{}, bool)

Get retrieves a value or false if nothing is found or the date has expired.

func (*LRUCache) GetOrLoad

func (c *LRUCache) GetOrLoad(key string, loader func() (interface{}, error)) (interface{}, error)

GetOrLoad: Retrieves a value from the cache or calls the loader. Only successful loader results are saved.

func (*LRUCache) GetOrLoadWithFallback

func (c *LRUCache) GetOrLoadWithFallback(
	key string,
	loader func() (interface{}, error),
	fallback interface{},
) (interface{}, error)

GetOrLoadWithFallback: like GetOrLoad, but provides a fallback in case of error

func (*LRUCache) LoadFromFile

func (c *LRUCache) LoadFromFile(filename string) error

LoadFromFile loads cache content from JSON file

func (*LRUCache) SaveToFile

func (c *LRUCache) SaveToFile(filename string) error

SaveToFile stores the cache as JSON

func (*LRUCache) Set

func (c *LRUCache) Set(key string, value interface{})

Set stores a value in the cache

func (*LRUCache) StopCleanup

func (c *LRUCache) StopCleanup()

StopCleanup ends the cleanup routine.

Jump to

Keyboard shortcuts

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