cache

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 6 Imported by: 0

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

View Source
const Namespace = "gomodularauth"

Namespace is the root prefix of every key written by GoModularAuth.

Variables

View Source
var ErrMiss = errors.New("cache: miss")

ErrMiss is returned by Cache.Get when the key does not exist or expired.

Functions

func Key

func Key(parts ...string) string

Key builds a namespaced key: Key("session", h) => "gomodularauth:session:<h>". Plugins must use their own segment: Key("organizations", "member", org, user).

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

func NewUserCache(c Cache) UserCache

NewUserCache adapts a generic Cache into a UserCache using keys "gomodularauth:user:<id>".

Directories

Path Synopsis
Package memory provides an in-process cache.Cache for tests and local development.
Package memory provides an in-process cache.Cache for tests and local development.

Jump to

Keyboard shortcuts

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