cache

package
v0.0.0-...-9384ad0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBadger

func NewBadger(overridePath ...string) (*badger.DB, error)

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

func (c *LRU[T]) Get(key string) (*T, bool)

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

func (c *LRU[T]) GetAsync(key string, cb func(T))

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.

func (*LRU[T]) Len

func (c *LRU[T]) Len() int

Len returns the number of items currently in the hot cache.

func (*LRU[T]) Put

func (c *LRU[T]) Put(key string, item T)

Put stores item in both the hot cache and Badger. If hot is full the least-recently-used entry is evicted from hot (it remains in Badger and can be recovered via GetAsync).

Jump to

Keyboard shortcuts

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