cache

package
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: 6 Imported by: 0

Documentation

Overview

Package cache is the backend-agnostic caching seam for lakta. It defines the typed Cache[K,V] interface, a Registry of config-declared named caches, and the Named[K,V] resolver that binds concrete key/value types to a declared cache on first use.

The core lives in the root module and pulls no heavy dependencies (stdlib + reflect only), so consumers get cache.Named/cache.Memoize without importing a backend. A backend (e.g. pkg/cache/memory on otter) registers Builders into a Registry during its Init and provides the Registry via DI.

Lazy type binding: config declares a cache's sizing by name, but the concrete (K,V) is unknown until an app module calls Named[K,V]. The first Named call records the (K,V) reflect identity and materializes the cache; a later call for the same name with a different K or V returns an error rather than silently rebuilding, so a config-declared cache stays type-safe.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Memoize

func Memoize[K comparable, V any](c Cache[K, V], load Loader[K, V]) func(context.Context, K) (V, error)

Memoize adapts a Cache + Loader into a plain func that calls GetOrLoad.

Types

type Builder

type Builder func(keyType, valType reflect.Type) (any, error)

Builder materializes a backend box for a declared cache name. The backend registers one per cache; the (K,V) reflect types recorded at the first Named call are passed through for backends that want them (memory boxes as any/any).

type Cache

type Cache[K comparable, V any] interface {
	Get(key K) (V, bool)
	Set(key K, value V)
	Delete(key K)
	// GetOrLoad returns the cached value or invokes load exactly once across
	// concurrent callers for the same key (dogpile-safe / singleflight).
	GetOrLoad(ctx context.Context, key K, load Loader[K, V]) (V, error)
	Stats() Stats
}

Cache is the backend-agnostic seam. memory (otter) and future backends back it.

func Named

func Named[K comparable, V any](reg *Registry, name string) (Cache[K, V], error)

Named lazily binds (K,V) to a declared cache name and memoizes the result. The first call records reflect.TypeFor[K]/[V] and materializes the cache via the entry's Builder; repeat calls with the same name return the same handle. A call with the same name but a different K or V returns a type-mismatch error naming both the recorded and requested (K,V). An unknown name returns a diagnostic not-found error listing the known names.

type Loader

type Loader[K comparable, V any] func(ctx context.Context, key K) (V, error)

Loader loads a value for a key on cache miss. Used by GetOrLoad and Memoize.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry holds declared/built caches by name with a reflect type-identity guard. Populated by a backend module's Init; consumed by Named during app modules' Init (topo-sort trick, as pool/scheduler do).

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty registry.

func (*Registry) Register

func (r *Registry) Register(name string, build Builder)

Register declares a cache name with its Builder. A second Register for the same name replaces the declaration (unbinding any previous build).

func (*Registry) Stats

func (r *Registry) Stats() map[string]Stats

Stats returns a per-name snapshot for actuator introspection. Only bound caches (already materialized via Named) report; unbound declarations are omitted.

func (*Registry) Unregister

func (r *Registry) Unregister(name string)

Unregister drops a cache name from the registry.

type Stats

type Stats struct {
	Hits      uint64
	Misses    uint64
	Evictions uint64
	Size      int
}

Stats is the flat, backend-free introspection record read by Registry.Stats.

Directories

Path Synopsis
memory module

Jump to

Keyboard shortcuts

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