Documentation
¶
Overview ¶
Package memory is an in-process cache backend for lakta built on maypok86/otter v2 (adaptive W-TinyLFU). It materializes config-declared named caches behind the backend-agnostic cache.Cache[K,V] seam, with dogpile-safe GetOrLoad, per-entry TTL, optional otel hit/miss/eviction stats, and hot-reload (live max-size resize; TTL change rebuilds and drops entries).
Caches are declared by sizing under modules.cache.memory.<inst>.caches (or via WithCache) and bound to concrete types on first cache.Named[K,V] call. Because Go cannot instantiate a generic otter cache from a runtime reflect.Type, each otter cache is otter.Cache[any, any] and the typed handle boxes keys/values; the registry's (K,V) identity guard keeps the boxed assertions safe.
Index ¶
- type Config
- type Module
- func (m *Module) ConfigPath() string
- func (m *Module) Dependencies() ([]reflect.Type, []reflect.Type)
- func (m *Module) Init(ctx context.Context) error
- func (m *Module) LoadConfig(k *koanf.Koanf) error
- func (m *Module) OnReload(k *koanf.Koanf)
- func (m *Module) Provides() []reflect.Type
- func (m *Module) Shutdown(_ context.Context) error
- type Option
- type Spec
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Instance name.
Name string `koanf:"-"`
// Caches holds config-declared caches. Prefer snake_case names; hyphens
// cannot be overridden via environment variables.
Caches map[string]Spec `koanf:"caches"`
// CodeCaches holds caches registered via WithCache (code-only); a config
// entry with the same name replaces it wholesale.
CodeCaches map[string]Spec `code_only:"WithCache" koanf:"-"`
}
Config mirrors pool.Config: config Caches overlay code-only CodeCaches by name.
func NewDefaultConfig ¶
func NewDefaultConfig() Config
NewDefaultConfig returns default configuration.
func (*Config) LoadFromKoanf ¶
LoadFromKoanf loads configuration from koanf instance at the given path.
func (*Config) MergedCaches ¶
MergedCaches combines code-registered and config-defined caches; config wins per name.
type Module ¶
Module builds otter caches from MergedCaches and provides the core *cache.Registry via DI. Plain Module (not long-running) — no Start.
func (*Module) ConfigPath ¶
ConfigPath returns the koanf path for this module's configuration.
func (*Module) Dependencies ¶
Dependencies declares the optional types this module needs from DI before Init. The otel module always provides a MeterProvider (noop when disabled), so declaring it orders otel before this module.
func (*Module) Init ¶
Init resolves the optional DI MeterProvider (noop when otel is absent), then registers a Builder per MergedCaches() entry into a fresh *cache.Registry. Otter needs concrete [K,V] at build time but config only knows sizing, so each Builder defers otter construction (as otter.Cache[any,any]) to the first Named[K,V] call. ProvideValue lets app modules Named[K,V] it during their Init.
func (*Module) LoadConfig ¶
LoadConfig loads configuration from koanf.
func (*Module) OnReload ¶
OnReload re-loads config then diffs MergedCaches() against the live caches: a max_size-only change resizes live (no entry loss, no warning); a ttl/ ttl_access/record_stats change rebuilds the otter cache and logs a loud warning that entries were dropped; added names register a lazy builder; removed names stop and drop the cache.
type Option ¶
type Option func(m *Config)
Option configures the Module.
type Spec ¶
type Spec struct {
MaxSize int `koanf:"max_size"` // entry-count bound -> otter MaximumSize
TTL time.Duration `koanf:"ttl"` // expire-after-write; 0 = none
TTLAccess time.Duration `koanf:"ttl_access"` // expire-after-access; 0 = none
RecordStats bool `koanf:"record_stats"` // attach the otel StatsRecorder
}
Spec declares one named cache's sizing. v1 keys only — refresh_after and max_weight are intentionally trimmed.