Documentation
¶
Index ¶
- Variables
- func ClearSiteCache(ctx context.Context) error
- func Init(cfg *config.CacheConfig, dbSessionFn DBSessionFunc) error
- func InvalidateAndDisableUserSessions(ctx context.Context, tx *gorm.DB, userID string)
- func InvalidateUserSessions(ctx context.Context, tx *gorm.DB, userID string)
- func RegisterCache(name string, cache CacheInterface)
- func ResetCacheRegistry()
- type Cache
- type CacheInterface
- type CacheManager
- type CachedSession
- type DBCache
- func (d *DBCache) Clear()
- func (d *DBCache) Close() error
- func (d *DBCache) Delete(ctx context.Context, key string) error
- func (d *DBCache) GC()
- func (d *DBCache) Get(ctx context.Context, key string) ([]byte, error)
- func (d *DBCache) GetWithTTL(ctx context.Context, key string) ([]byte, time.Duration, error)
- func (d *DBCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- type DBSessionFunc
- type LayeredCache
- func (c *LayeredCache) Clear()
- func (c *LayeredCache) Delete(ctx context.Context, key string) error
- func (c *LayeredCache) GC()
- func (c *LayeredCache) Get(ctx context.Context, key string) ([]byte, error)
- func (c *LayeredCache) GetWithTTL(ctx context.Context, key string) ([]byte, time.Duration, error)
- func (c *LayeredCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- type MemoryCache
- func (c *MemoryCache) Clear()
- func (c *MemoryCache) Delete(_ context.Context, key string) error
- func (c *MemoryCache) GC()
- func (c *MemoryCache) Get(ctx context.Context, key string) ([]byte, error)
- func (c *MemoryCache) GetWithTTL(_ context.Context, key string) ([]byte, time.Duration, error)
- func (c *MemoryCache) Set(_ context.Context, key string, value []byte, ttl time.Duration) error
- type RedisCache
- func (c *RedisCache) Clear()
- func (c *RedisCache) Delete(ctx context.Context, key string) error
- func (c *RedisCache) GC()
- func (c *RedisCache) Get(ctx context.Context, key string) ([]byte, error)
- func (c *RedisCache) GetWithTTL(ctx context.Context, key string) ([]byte, time.Duration, error)
- func (c *RedisCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- type TypedCache
- func (c *TypedCache[T]) Clear()
- func (c *TypedCache[T]) Delete(ctx context.Context, key string) error
- func (c *TypedCache[T]) GC()
- func (c *TypedCache[T]) Get(ctx context.Context, key string) (T, error)
- func (c *TypedCache[T]) GetOrLoad(ctx context.Context, key string, loader func() (T, error)) (T, error)
- func (c *TypedCache[T]) Set(ctx context.Context, key string, value T) error
Constants ¶
This section is empty.
Variables ¶
var ( // Store is the raw byte cache used for ephemeral data (OAuth state, MFA tokens, etc.). Store Cache // Sessions is a typed cache for authenticated sessions (hot path). // Key: tokenHash (sha256 hex of the raw JWT), Value: CachedSession. // L2 is backed by the t_cache_entry DB table so sessions survive restarts. Sessions *TypedCache[CachedSession] // Roles caches full Role objects (with Permissions & AIToolPermissions). // Key: roleID (ResourceID), Value: model.Role. In-memory only (no L2). Roles *TypedCache[model.Role] // Settings caches individual system settings by key. Settings *TypedCache[model.Setting] // AllSettings caches the full settings list under a single key. AllSettings *TypedCache[[]model.Setting] )
Global cache instances, initialised by Init.
var ErrCacheMiss = errors.New("cache: key not found")
ErrCacheMiss is returned when a key is not found in the cache.
Functions ¶
func ClearSiteCache ¶ added in v1.15.0
ClearSiteCache clears all registered application caches (raw Store, typed L1 caches, etc.).
func Init ¶
func Init(cfg *config.CacheConfig, dbSessionFn DBSessionFunc) error
Init creates the cache backend from config and wires up the global instances. Must be called once at startup before any cache access. dbSessionFn provides the DB connection for DB-backed external cache; pass nil when cache driver is not db (useful in tests).
func InvalidateAndDisableUserSessions ¶
InvalidateAndDisableUserSessions invalidates cache entries and marks all DB sessions for the user as invalid (used when a user is disabled or deleted).
func InvalidateUserSessions ¶
InvalidateUserSessions deletes all session cache entries for the given user. It queries the t_session table to discover the tokenHash keys, then removes each one from the Sessions cache (L1 + L2).
func RegisterCache ¶ added in v1.15.0
func RegisterCache(name string, cache CacheInterface)
func ResetCacheRegistry ¶ added in v1.15.0
func ResetCacheRegistry()
Types ¶
type Cache ¶
type Cache interface {
Get(ctx context.Context, key string) ([]byte, error)
GetWithTTL(ctx context.Context, key string) ([]byte, time.Duration, error)
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
Delete(ctx context.Context, key string) error
CacheInterface
}
Cache is the pluggable backend interface for raw byte storage. Implementations may be in-memory, Redis, database-backed, etc.
type CacheInterface ¶ added in v1.15.0
type CacheInterface interface {
Clear()
GC()
}
func GetCache ¶ added in v1.15.0
func GetCache(name string) CacheInterface
type CacheManager ¶ added in v1.15.0
type CacheManager struct {
// contains filtered or unexported fields
}
func NewCacheManager ¶ added in v1.15.0
func NewCacheManager() *CacheManager
func (*CacheManager) GetCache ¶ added in v1.15.0
func (c *CacheManager) GetCache(key string) CacheInterface
type CachedSession ¶
type CachedSession struct {
// Session metadata
SessionID string `json:"session_id"`
ExpiredAt time.Time `json:"expired_at"`
LastActiveAt time.Time `json:"last_active_at"`
IsValid bool `json:"is_valid"`
// User snapshot
UserID string `json:"user_id"`
Username string `json:"username"`
Email string `json:"email"`
FullName string `json:"full_name"`
Phone string `json:"phone"`
Avatar string `json:"avatar"`
Status string `json:"status"`
LockedUntil time.Time `json:"locked_until"`
PasswordChangedAt time.Time `json:"password_changed_at"`
LastLogin time.Time `json:"last_login"`
MFAEnabled bool `json:"mfa_enabled"`
MFAEnforced bool `json:"mfa_enforced"`
MFAType string `json:"mfa_type"`
Source model.UserSource `json:"source"`
OAuthProvider string `json:"oauth_provider"`
LDAPDN string `json:"ldap_dn"`
DisableChangePassword bool `json:"disable_change_password"`
// Role IDs (not full objects)
RoleIDs []string `json:"role_ids"`
// IsTemporaryRoles is true when RoleIDs were set from OAuth temporary
// role mapping rather than from the user_roles table. When true,
// rebuildSessionCache should restore roles from Session.OAuthRoleIDs
// instead of loading from the user's persisted roles.
IsTemporaryRoles bool `json:"is_temporary_roles,omitempty"`
// Organizations
Organizations []model.Organization `json:"organizations,omitempty"`
}
CachedSession is the value stored in the Sessions cache. It holds a snapshot of user attributes (at cache time) plus session metadata and lightweight role references (IDs only — full Role objects live in the Roles cache).
func NewCachedSession ¶
func NewCachedSession(session *model.Session, user *model.User) CachedSession
NewCachedSession builds a CachedSession from DB models.
type DBCache ¶
type DBCache struct {
// contains filtered or unexported fields
}
DBCache implements the Cache interface backed by the t_cache_entry table.
func NewDBCache ¶
func NewDBCache(sessionFn DBSessionFunc) *DBCache
NewDBCache creates a DB-backed cache.
func (*DBCache) Clear ¶ added in v1.15.0
func (d *DBCache) Clear()
Clear removes all entries from the DB cache table.
func (*DBCache) GC ¶ added in v1.15.0
func (d *DBCache) GC()
GC deletes expired entries from the DB cache table.
func (*DBCache) GetWithTTL ¶ added in v1.15.0
type DBSessionFunc ¶
DBSessionFunc returns a *gorm.DB scoped to the given context. This indirection avoids a direct import of the db package (which would create a circular dependency: cache -> db -> config, and db depends on cache during Init).
type LayeredCache ¶ added in v1.15.0
type LayeredCache struct {
// contains filtered or unexported fields
}
LayeredCache composes a fixed in-memory L1 cache with an optional external L2 cache.
func NewLayeredCache ¶ added in v1.15.0
func NewLayeredCache(l1 Cache, l2 Cache, fallbackTTL time.Duration) *LayeredCache
NewLayeredCache creates a two-layer raw cache.
func (*LayeredCache) Clear ¶ added in v1.15.0
func (c *LayeredCache) Clear()
func (*LayeredCache) Delete ¶ added in v1.15.0
func (c *LayeredCache) Delete(ctx context.Context, key string) error
func (*LayeredCache) GC ¶ added in v1.15.0
func (c *LayeredCache) GC()
func (*LayeredCache) GetWithTTL ¶ added in v1.15.0
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache is an in-memory Cache implementation with TTL-based expiration and a background garbage-collection goroutine.
func NewMemoryCache ¶
func NewMemoryCache() *MemoryCache
NewMemoryCache creates a MemoryCache with a background GC that runs every gcInterval.
func (*MemoryCache) Clear ¶ added in v1.15.0
func (c *MemoryCache) Clear()
Clear removes all entries from the in-memory store.
func (*MemoryCache) GC ¶ added in v1.15.0
func (c *MemoryCache) GC()
func (*MemoryCache) GetWithTTL ¶ added in v1.15.0
type RedisCache ¶ added in v1.15.0
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache is a Cache implementation backed by Redis.
func NewRedisCache ¶ added in v1.15.0
func NewRedisCache(cfg config.RedisConfig) (*RedisCache, error)
NewRedisCache creates a Redis-backed cache and verifies connectivity.
func (*RedisCache) Clear ¶ added in v1.15.0
func (c *RedisCache) Clear()
Clear removes all keys matching this cache prefix.
func (*RedisCache) Delete ¶ added in v1.15.0
func (c *RedisCache) Delete(ctx context.Context, key string) error
func (*RedisCache) GC ¶ added in v1.15.0
func (c *RedisCache) GC()
GC is handled by Redis TTL itself, so no-op.
func (*RedisCache) GetWithTTL ¶ added in v1.15.0
type TypedCache ¶
type TypedCache[T any] struct { // contains filtered or unexported fields }
TypedCache provides a strongly-typed, in-process L1 cache with an optional L2 backend (Cache interface). L1 hits return Go values directly with zero serialization overhead; L2 is consulted only on L1 miss.
func NewTypedCache ¶
func NewTypedCache[T any](prefix string, ttl time.Duration, remote Cache, gcInterval time.Duration) *TypedCache[T]
NewTypedCache creates a TypedCache.
- prefix: key prefix used when persisting to the remote L2 cache.
- ttl: default time-to-live for entries.
- remote: optional L2 Cache backend (pass nil for pure in-process caching).
- gcInterval: how often expired L1 entries are reaped.
func (*TypedCache[T]) Clear ¶
func (c *TypedCache[T]) Clear()
Clear removes all entries from L1. L2 is not affected.
func (*TypedCache[T]) Delete ¶
func (c *TypedCache[T]) Delete(ctx context.Context, key string) error
Delete removes a key from both L1 and L2.
func (*TypedCache[T]) GC ¶ added in v1.15.0
func (c *TypedCache[T]) GC()
func (*TypedCache[T]) Get ¶
func (c *TypedCache[T]) Get(ctx context.Context, key string) (T, error)
Get retrieves a value from the cache. It checks L1 first, then L2 (if configured). Returns ErrCacheMiss when the key is not found in either layer.