Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type LRU ¶
type LRU[T any] struct { // contains filtered or unexported fields }
LRU is a generic in-memory LRU cache with Badger as L2 persistent store. Thread-safe via sync.Mutex.
func NewLRU ¶
func NewLRU[T any]( store *badger.DB, keyPrefix string, maxHot int, marshal func(T) []byte, unmarshal func([]byte) T, ) *LRU[T]
NewLRU creates a new LRU cache backed by the given Badger DB.
- store: opened Badger DB instance
- keyPrefix: namespaces keys written to Badger (e.g. "img:")
- maxHot: maximum number of items kept in the in-memory hot cache
- marshal: serialises T to bytes for Badger storage
- unmarshal: deserialises bytes from Badger back to T
func (*LRU[T]) Clear ¶
func (c *LRU[T]) Clear()
Clear evicts all entries from the in-memory hot cache. Badger L2 entries are not deleted (append-only design), but they will be ignored since keys are no longer indexed.
func (*LRU[T]) Get ¶
Get looks up key in the hot cache. On hit it promotes the entry to most-recently-used and returns a pointer to the value plus true. Returns nil, false on a miss (the caller should use GetAsync for L2).
func (*LRU[T]) GetAsync ¶
GetAsync attempts to find key in the hot cache first. If missing, it spawns a goroutine to read from Badger, promotes the result into hot, and then calls cb with the value. If the key is not in Badger either, cb is not called.
The caller is responsible for any UI thread marshaling needed before touching UI elements inside cb.