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 ¶
- type CacheEntry
- type LRUCache
- func (c *LRUCache) Get(key string) (interface{}, bool)
- func (c *LRUCache) GetOrLoad(key string, loader func() (interface{}, error)) (interface{}, error)
- func (c *LRUCache) GetOrLoadWithFallback(key string, loader func() (interface{}, error), fallback interface{}) (interface{}, error)
- func (c *LRUCache) LoadFromFile(filename string) error
- func (c *LRUCache) SaveToFile(filename string) error
- func (c *LRUCache) Set(key string, value interface{})
- func (c *LRUCache) StopCleanup()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CacheEntry ¶
CacheEntry stores key, value, and expiry time
type LRUCache ¶
type LRUCache struct {
// contains filtered or unexported fields
}
LRUCache is mainstructure
func (*LRUCache) GetOrLoad ¶
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 ¶
LoadFromFile loads cache content from JSON file
func (*LRUCache) SaveToFile ¶
SaveToFile stores the cache as JSON
func (*LRUCache) StopCleanup ¶
func (c *LRUCache) StopCleanup()
StopCleanup ends the cleanup routine.