Documentation
¶
Index ¶
- Constants
- Variables
- func CacheGet[T any](ctx context.Context, c *Client, key string) (T, bool)
- func CacheGetMany[T any](ctx context.Context, c *Client, keys ...string) map[string]T
- func CacheGetManyT[T any](ctx context.Context, m *MultiTenantClient, tenantID string, keys ...string) map[string]T
- func CacheGetOrFetch[T any](ctx context.Context, c *Client, key string, ttl time.Duration, ...) (T, error)
- func CacheGetOrFetchT[T any](ctx context.Context, m *MultiTenantClient, tenantID, key string, ...) (T, error)
- func CacheGetT[T any](ctx context.Context, m *MultiTenantClient, tenantID, key string) (T, bool)
- func Get[T any](ctx context.Context, c *Client, key string, ttl time.Duration, ...) (T, error)
- func GetT[T any](ctx context.Context, m *MultiTenantClient, tenantID, key string, ...) (T, error)
- func HashRequest(req any) string
- type Client
- func (c *Client) CacheSet(ctx context.Context, key string, value any, ttl time.Duration, tags ...string) error
- func (c *Client) CacheSetMany(ctx context.Context, items map[string]any, ttl time.Duration, tags ...string) error
- func (c *Client) CleanTag(ctx context.Context, tag string) error
- func (c *Client) HashRequest(req any) (string, error)
- func (c *Client) InvalidateByTags(ctx context.Context, tags ...string) error
- func (c *Client) InvalidatePattern(ctx context.Context, pattern string) error
- func (c *Client) InvalidatePrefix(ctx context.Context, prefix string) error
- type Codec
- type JSONCodec
- type Metrics
- type MultiTenantClient
- func (m *MultiTenantClient) CacheSet(ctx context.Context, tenantID, key string, value any, ttl time.Duration, ...) error
- func (m *MultiTenantClient) CacheSetMany(ctx context.Context, tenantID string, items map[string]any, ttl time.Duration, ...) error
- func (m *MultiTenantClient) CleanTag(ctx context.Context, tenantID, tag string) error
- func (m *MultiTenantClient) HashRequest(ctx context.Context, tenantID string, req any) (string, error)
- func (m *MultiTenantClient) InvalidateByTags(ctx context.Context, tenantID string, tags ...string) error
- func (m *MultiTenantClient) InvalidatePattern(ctx context.Context, tenantID, pattern string) error
- func (m *MultiTenantClient) InvalidatePrefix(ctx context.Context, tenantID, prefix string) error
- func (m *MultiTenantClient) Tenant(tenantID string) *Client
- type Option
Constants ¶
const DefaultNamespace = "goredis"
Variables ¶
var ErrCacheDisabled = errors.New("redis client is nil, caching disabled")
Functions ¶
func CacheGetMany ¶ added in v0.1.0
CacheGetMany retrieves multiple values at once via MGET.
func CacheGetManyT ¶ added in v0.1.0
func CacheGetManyT[T any](ctx context.Context, m *MultiTenantClient, tenantID string, keys ...string) map[string]T
CacheGetMany retrieves multiple values at once for a tenant.
func CacheGetOrFetch ¶
func CacheGetOrFetch[T any]( ctx context.Context, c *Client, key string, ttl time.Duration, fetch func() (T, error), tags ...string, ) (T, error)
CacheGetOrFetch retrieves a value by key, calling fetch on miss. Concurrent requests for the same key are coalesced using singleflight to prevent cache stampedes.
func CacheGetOrFetchT ¶ added in v0.1.0
func CacheGetOrFetchT[T any]( ctx context.Context, m *MultiTenantClient, tenantID, key string, ttl time.Duration, fetch func() (T, error), tags ...string, ) (T, error)
CacheGetOrFetch retrieves a value by key for a tenant, calling fetch on miss.
func GetT ¶ added in v0.1.2
func GetT[T any]( ctx context.Context, m *MultiTenantClient, tenantID, key string, ttl time.Duration, fetch func() (T, error), tags ...string, ) (T, error)
GetT retrieves a value by key for a tenant, calling fetch on miss.
func HashRequest ¶
Types ¶
type Client ¶ added in v0.1.0
type Client struct {
// contains filtered or unexported fields
}
Client is a tag-based cache invalidation layer over go-redis.
func (*Client) CacheSet ¶ added in v0.1.0
func (c *Client) CacheSet( ctx context.Context, key string, value any, ttl time.Duration, tags ...string, ) error
CacheSet stores a value and registers it under the given tags. Uses MULTI/EXEC (TxPipeline) for atomicity across SET, SADD, EXPIRE.
func (*Client) CacheSetMany ¶ added in v0.1.0
func (c *Client) CacheSetMany( ctx context.Context, items map[string]any, ttl time.Duration, tags ...string, ) error
CacheSetMany stores multiple values atomically.
func (*Client) CleanTag ¶ added in v0.1.0
CleanTag removes stale key references from a tag set by checking whether each member still exists in the cache. Useful as a periodic maintenance task to prevent tag sets from accumulating garbage.
func (*Client) HashRequest ¶ added in v0.1.0
HashRequest generates a deterministic hex hash from a request struct.
func (*Client) InvalidateByTags ¶ added in v0.1.0
InvalidateByTags deletes all cache keys associated with the given tags. Keys are deduplicated across tags, and all deletions are batched in a pipeline.
func (*Client) InvalidatePattern ¶ added in v0.1.0
InvalidatePattern deletes all cache keys matching the given glob pattern.
type Metrics ¶ added in v0.1.0
type Metrics interface {
Hit(key string)
Miss(key string)
Set(key string)
Invalidate(tags []string)
FetchDuration(key string, d time.Duration)
}
Metrics hooks for observability.
type MultiTenantClient ¶ added in v0.1.0
type MultiTenantClient struct {
// contains filtered or unexported fields
}
MultiTenantClient manages per-tenant cache clients with isolated namespaces. Each tenant gets its own *Client with namespace <base>:<tenantID>, ensuring key and tag isolation across tenants while sharing the underlying Redis connection pool.
func NewMultiTenant ¶ added in v0.1.0
func NewMultiTenant(rdb *redis.Client, base string, opts ...Option) *MultiTenantClient
NewMultiTenant creates a MultiTenantClient. base is the environment prefix (e.g. "prod", "staging"). opts are shared across all tenant clients (codec, metrics, etc.).
func (*MultiTenantClient) CacheSet ¶ added in v0.1.0
func (m *MultiTenantClient) CacheSet( ctx context.Context, tenantID, key string, value any, ttl time.Duration, tags ...string, ) error
CacheSet stores a value and registers it under tags for a tenant.
func (*MultiTenantClient) CacheSetMany ¶ added in v0.1.0
func (m *MultiTenantClient) CacheSetMany( ctx context.Context, tenantID string, items map[string]any, ttl time.Duration, tags ...string, ) error
CacheSetMany stores multiple values atomically for a tenant.
func (*MultiTenantClient) CleanTag ¶ added in v0.1.0
func (m *MultiTenantClient) CleanTag(ctx context.Context, tenantID, tag string) error
CleanTag removes stale key references from a tag set for a tenant.
func (*MultiTenantClient) HashRequest ¶ added in v0.1.0
func (m *MultiTenantClient) HashRequest(ctx context.Context, tenantID string, req any) (string, error)
HashRequest generates a deterministic hex hash from a request struct for a tenant.
func (*MultiTenantClient) InvalidateByTags ¶ added in v0.1.0
func (m *MultiTenantClient) InvalidateByTags(ctx context.Context, tenantID string, tags ...string) error
InvalidateByTags deletes all cache keys associated with the given tags for a tenant.
func (*MultiTenantClient) InvalidatePattern ¶ added in v0.1.0
func (m *MultiTenantClient) InvalidatePattern(ctx context.Context, tenantID, pattern string) error
InvalidatePattern deletes all cache keys matching the given glob pattern for a tenant.
func (*MultiTenantClient) InvalidatePrefix ¶ added in v0.1.0
func (m *MultiTenantClient) InvalidatePrefix(ctx context.Context, tenantID, prefix string) error
InvalidatePrefix deletes all cache keys with the given key prefix for a tenant.
func (*MultiTenantClient) Tenant ¶ added in v0.1.0
func (m *MultiTenantClient) Tenant(tenantID string) *Client
Client returns the per-tenant *Client for the given tenantID, creating it lazily.