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 ¶
Types ¶
type Builder ¶
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 ¶
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 (*Registry) Register ¶
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 ¶
Stats returns a per-name snapshot for actuator introspection. Only bound caches (already materialized via Named) report; unbound declarations are omitted.
func (*Registry) Unregister ¶
Unregister drops a cache name from the registry.