permissioncache

package
v0.45.13 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

README

Permissions Cache

This package contains helpers for caching permissions, such as enabled features for an organization. Cache is a small wrapper around Redis that can store things such as a set of feature names keyed by organization ID, user roles within an organization, etc.. Entries expire independently from session data and can be fetched with GetX or written with SetX.

c := features.NewCache(redisClient, permissioncache.WithCacheTTL(time.Minute))
features := []models.OrgModule{models.OrgModule("evidence"), models.OrgModule("search")}
err := c.SetFeatures(ctx, "org1", features)
if err != nil {
    return err
}

feats, err := c.GetFeatures(ctx, "org1")
if err != nil {
    return err
}

Use permissioncache.WithCache and permissioncache.CacheFromContext to make the cache available throughout a request lifecycle.

Documentation

Overview

Package permissioncache handles permissioncache for things like features

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetCacheContext

func SetCacheContext(c echo.Context, redisClient *redis.Client, opts ...CacheOptions)

SetCacheContext sets the cache context in the echo context, defaults to a ttl of 5 minutes if not specified

func WithCache

func WithCache(ctx context.Context, c *Cache) context.Context

WithCache stores the feature cache on the context for downstream use.

Types

type Cache

type Cache struct {
	Redis *redis.Client
	TTL   time.Duration
}

Cache stores enabled feature sets in Redis keyed by organization ID. It reuses the same Redis instance as the session store but keeps feature data separate from user session values so entries can expire independently.

func CacheFromContext

func CacheFromContext(ctx context.Context) (*Cache, bool)

CacheFromContext retrieves the feature cache from context if present.

func NewCache

func NewCache(r *redis.Client, opts ...CacheOptions) *Cache

NewCache returns a Cache using the provided redis client.

func (*Cache) GetFeatures

func (c *Cache) GetFeatures(ctx context.Context, orgID string) ([]models.OrgModule, error)

GetFeatures retrieves the feature list for an organization.

func (*Cache) GetRoles

func (c *Cache) GetRoles(ctx context.Context, subjectID, orgID string) ([]string, error)

GetRoles retrieves the role for a subject ID.

func (*Cache) GetUserOrgs

func (c *Cache) GetUserOrgs(ctx context.Context, subjectID string) ([]string, error)

GetUserOrgs retrieves the organizations a user belongs to.

func (*Cache) HasOrgAccess

func (c *Cache) HasOrgAccess(ctx context.Context, subjectID, orgID string) (bool, error)

func (*Cache) HasRole

func (c *Cache) HasRole(ctx context.Context, subjectID, orgID string, role string) (bool, error)

HasRole checks if a role exists for a subject ID.

func (*Cache) SetFeatures

func (c *Cache) SetFeatures(ctx context.Context, orgID string, values []models.OrgModule) error

SetFeatures stores the feature list for an organization.

func (*Cache) SetRole

func (c *Cache) SetRole(ctx context.Context, subjectID, orgID string, value string) error

SetRole stores the role for a subject ID.

type CacheOptions

type CacheOptions func(*Cache)

CacheOptions is a functional option type for configuring the Cache.

func WithCacheTTL

func WithCacheTTL(ttl time.Duration) CacheOptions

WithCacheTTL sets the time-to-live for cache entries. If ttl is zero, the default TTL will be used.

Jump to

Keyboard shortcuts

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