memory

package module
v0.4.1 Latest Latest
Warning

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

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

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

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 NewConfig

func NewConfig(options ...Option) Config

NewConfig returns configuration with provided options based on defaults.

func NewDefaultConfig

func NewDefaultConfig() Config

NewDefaultConfig returns default configuration.

func (*Config) LoadFromKoanf

func (c *Config) LoadFromKoanf(k *koanf.Koanf, path string) error

LoadFromKoanf loads configuration from koanf instance at the given path.

func (*Config) MergedCaches

func (c *Config) MergedCaches() map[string]Spec

MergedCaches combines code-registered and config-defined caches; config wins per name.

type Module

type Module struct {
	lakta.NamedBase
	// contains filtered or unexported fields
}

Module builds otter caches from MergedCaches and provides the core *cache.Registry via DI. Plain Module (not long-running) — no Start.

func NewModule

func NewModule(options ...Option) *Module

NewModule creates a new in-memory cache module.

func (*Module) ConfigPath

func (m *Module) ConfigPath() string

ConfigPath returns the koanf path for this module's configuration.

func (*Module) Dependencies

func (m *Module) Dependencies() ([]reflect.Type, []reflect.Type)

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

func (m *Module) Init(ctx context.Context) error

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

func (m *Module) LoadConfig(k *koanf.Koanf) error

LoadConfig loads configuration from koanf.

func (*Module) OnReload

func (m *Module) OnReload(k *koanf.Koanf)

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.

func (*Module) Provides

func (m *Module) Provides() []reflect.Type

Provides returns the types this module registers in DI.

func (*Module) Shutdown

func (m *Module) Shutdown(_ context.Context) error

Shutdown stops every built otter cache's background goroutines.

type Option

type Option func(m *Config)

Option configures the Module.

func WithCache

func WithCache(name string, spec Spec) Option

WithCache registers a cache in code (code-only); config with the same name takes precedence.

func WithName

func WithName(name string) Option

WithName sets the instance name for this 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.

Jump to

Keyboard shortcuts

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