Documentation
¶
Index ¶
- Constants
- func FormatKey(prefix string, parts ...string) string
- func UserContextKeyFor(sub, clientID string) string
- type Cache
- func (c *Cache) GetUserContext(ctx context.Context, sub, clientID string) *UserContext
- func (c *Cache) InvalidateAllUsers(ctx context.Context)
- func (c *Cache) InvalidateUser(ctx context.Context, sub, clientID string)
- func (c *Cache) InvalidateUserAll(ctx context.Context, sub string)
- func (c *Cache) SetUserContext(ctx context.Context, sub, clientID string, uc *UserContext)
- type Invalidator
- type NopInvalidator
- type UserContext
Constants ¶
const ( // UserContextTTL is how long a user context entry stays in cache. UserContextTTL = 10 * time.Minute )
Variables ¶
This section is empty.
Functions ¶
func FormatKey ¶
FormatKey builds a namespaced cache key. General-purpose helper for future cache entries beyond user context.
func UserContextKeyFor ¶
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 (*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 ¶
InvalidateAllUsers removes every user-context cache entry. Use this when a change potentially affects many users (e.g. role permission updates).
func (*Cache) InvalidateUser ¶
InvalidateUser removes the cached context for a specific user + client pair.
func (*Cache) InvalidateUserAll ¶
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.