cache

package
v0.0.0-...-a1393ee Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// UserContextTTL is how long a user context entry stays in cache.
	UserContextTTL = 10 * time.Minute
)

Variables

This section is empty.

Functions

func FormatKey

func FormatKey(prefix string, parts ...string) string

FormatKey builds a namespaced cache key. General-purpose helper for future cache entries beyond user context.

func UserContextKeyFor

func UserContextKeyFor(sub, clientID string) string

UserContextKeyFor returns the Redis key for a given sub and clientID. Exported so the middleware can set/get using the same key scheme.

Types

type Cache

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

Cache provides typed helpers around a Redis client for user-context caching and invalidation.

func New

func New(rdb *redis.Client) *Cache

New creates a Cache backed by the given Redis client.

func (*Cache) GetUserContext

func (c *Cache) GetUserContext(ctx context.Context, sub, clientID string) *UserContext

GetUserContext retrieves a cached user context. Returns nil when the key does not exist or cannot be deserialized (cache miss).

func (*Cache) InvalidateAllUsers

func (c *Cache) InvalidateAllUsers(ctx context.Context)

InvalidateAllUsers removes every user-context cache entry. Use this when a change potentially affects many users (e.g. role permission updates).

func (*Cache) InvalidateUser

func (c *Cache) InvalidateUser(ctx context.Context, sub, clientID string)

InvalidateUser removes the cached context for a specific user + client pair.

func (*Cache) InvalidateUserAll

func (c *Cache) InvalidateUserAll(ctx context.Context, sub string)

InvalidateUserAll removes every cached context entry for the given sub (across all client IDs) using an iterative SCAN to avoid blocking Redis.

func (*Cache) SetUserContext

func (c *Cache) SetUserContext(ctx context.Context, sub, clientID string, uc *UserContext)

SetUserContext caches a user context entry with the default TTL.

type Invalidator

type Invalidator interface {
	// InvalidateUser removes the cache entry for a specific sub + clientID.
	InvalidateUser(ctx context.Context, sub, clientID string)
	// InvalidateUserAll removes all cache entries for the given sub.
	InvalidateUserAll(ctx context.Context, sub string)
	// InvalidateAllUsers removes every user-context cache entry.
	InvalidateAllUsers(ctx context.Context)
}

Invalidator is the subset of Cache that services use to invalidate cached data after mutations. Keeping this as an interface allows services to be tested without a real Redis connection.

type NopInvalidator

type NopInvalidator struct{}

NopInvalidator is a no-op Invalidator for use in tests or when caching is disabled.

func (NopInvalidator) InvalidateAllUsers

func (NopInvalidator) InvalidateAllUsers(context.Context)

func (NopInvalidator) InvalidateUser

func (NopInvalidator) InvalidateUser(context.Context, string, string)

func (NopInvalidator) InvalidateUserAll

func (NopInvalidator) InvalidateUserAll(context.Context, string)

type UserContext

type UserContext struct {
	User     *model.User             `json:"user"`
	Tenant   *model.Tenant           `json:"tenant"`
	Provider *model.IdentityProvider `json:"provider"`
	Client   *model.Client           `json:"client"`
}

UserContext is the data stored in the user-context cache.

Jump to

Keyboard shortcuts

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