Documentation
¶
Overview ¶
Package cache defines the Cache Adapter contracts of GoModularAuth.
Store = source of truth. Cache = performance / ephemeral state.
A cache is always optional: the core works identically without one and degrades to the Store when the cache fails. Every entry must carry an explicit TTL, keys must be namespaced (see Key) and secrets must never be used as keys: derive keys from hashes instead.
Index ¶
Constants ¶
const Namespace = "gomodularauth"
Namespace is the root prefix of every key written by GoModularAuth.
Variables ¶
var ErrMiss = errors.New("cache: miss")
ErrMiss is returned by Cache.Get when the key does not exist or expired.
Functions ¶
Types ¶
type Cache ¶
type Cache interface {
Get(ctx context.Context, key string, value any) error
Set(ctx context.Context, key string, value any, ttl time.Duration) error
Delete(ctx context.Context, key string) error
}
Cache is the generic infrastructure contract implemented by cache adapters (memory, Redis, Valkey, Memcached, ...).
Implementations serialize values; Get decodes into the pointer value. ttl must be > 0: entries without expiration are not allowed.
type Counter ¶
type Counter interface {
Increment(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)
}
Counter is an optional capability for atomic counters (e.g. distributed rate limiting). Increment adds delta to key, creating it with ttl when it does not exist, and returns the new value. The ttl is not refreshed by subsequent increments (fixed window semantics).
type SessionCache ¶
type SessionCache interface {
Get(ctx context.Context, tokenHash string) (*session.Session, error)
Set(ctx context.Context, s *session.Session, ttl time.Duration) error
Delete(ctx context.Context, tokenHash string) error
}
SessionCache is the semantic cache for sessions, keyed by token hash. It keeps arbitrary keys out of the core.
func NewSessionCache ¶
func NewSessionCache(c Cache) SessionCache
NewSessionCache adapts a generic Cache into a SessionCache using keys "gomodularauth:session:<tokenHash>".
type UserCache ¶
type UserCache interface {
Get(ctx context.Context, id string) (*user.User, error)
Set(ctx context.Context, u *user.User, ttl time.Duration) error
Delete(ctx context.Context, id string) error
}
UserCache is the semantic cache for users, keyed by user ID.
func NewUserCache ¶
NewUserCache adapts a generic Cache into a UserCache using keys "gomodularauth:user:<id>".