cache

package
v0.1.8 Latest Latest
Warning

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

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

Documentation

Overview

Package cache contains tenant-aware cache abstractions.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoTenant reports cache access without tenant or allowed host context.
	ErrNoTenant = gotenancy.ErrNoTenant

	// ErrUnsafeKey reports a key that attempts to bypass tenant scoping.
	ErrUnsafeKey = errors.New("gotenancy/cache: unsafe key")

	// ErrHostGlobalKeyNotAllowed reports host global key access without explicit opt-in.
	ErrHostGlobalKeyNotAllowed = errors.New("gotenancy/cache: host global key not allowed")

	// ErrInvalidCacheSize reports an invalid bounded memory cache size.
	ErrInvalidCacheSize = errors.New("gotenancy/cache: invalid cache size")

	// ErrInvalidRedisConfig reports an invalid Redis cache adapter configuration.
	ErrInvalidRedisConfig = errors.New("gotenancy/cache: invalid redis config")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Get(ctx context.Context, key string) ([]byte, bool, error)
	Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
	Delete(ctx context.Context, key string) error
}

Cache stores byte values.

type KeyBuilder

type KeyBuilder struct {
	AllowHostGlobal bool
}

KeyBuilder creates scoped cache keys.

func (KeyBuilder) Build

func (builder KeyBuilder) Build(ctx context.Context, key string) (string, error)

Build returns a scoped key for ctx.

type Memory

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

Memory is a thread-safe in-memory cache.

func NewBoundedMemory

func NewBoundedMemory(maxEntries int) (*Memory, error)

NewBoundedMemory creates a memory cache with a maximum number of entries.

func NewMemory

func NewMemory() *Memory

NewMemory creates an empty memory cache.

func (*Memory) Delete

func (cache *Memory) Delete(ctx context.Context, key string) error

Delete removes a cache value.

func (*Memory) Get

func (cache *Memory) Get(ctx context.Context, key string) ([]byte, bool, error)

Get returns a cache value.

func (*Memory) Set

func (cache *Memory) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Set stores a cache value.

type Option

type Option func(*TenantCache)

Option configures TenantCache.

func WithHostGlobalKeys

func WithHostGlobalKeys(allow bool) Option

WithHostGlobalKeys allows host context to access global cache keys.

type Redis added in v0.1.7

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

Redis stores cache values in Redis through a go-redis client.

func NewRedis added in v0.1.7

func NewRedis(client RedisClient) (*Redis, error)

NewRedis creates a cache adapter from an existing go-redis client.

The caller owns the client configuration. Close closes the provided client.

func NewRedisFromClusterOptions added in v0.1.7

func NewRedisFromClusterOptions(options *redis.ClusterOptions) (*Redis, error)

NewRedisFromClusterOptions creates a Redis cache adapter from cluster client options.

func NewRedisFromOptions added in v0.1.7

func NewRedisFromOptions(options *redis.Options) (*Redis, error)

NewRedisFromOptions creates a Redis cache adapter from standalone client options.

func NewRedisFromURL added in v0.1.7

func NewRedisFromURL(rawURL string) (*Redis, error)

NewRedisFromURL creates a Redis cache adapter from a redis:// or rediss:// URL.

func (*Redis) Close added in v0.1.7

func (cache *Redis) Close() error

Close closes the underlying Redis client.

func (*Redis) Delete added in v0.1.7

func (cache *Redis) Delete(ctx context.Context, key string) error

Delete removes a cache value.

func (*Redis) Get added in v0.1.7

func (cache *Redis) Get(ctx context.Context, key string) ([]byte, bool, error)

Get returns a cache value.

func (*Redis) Ping added in v0.1.7

func (cache *Redis) Ping(ctx context.Context) error

Ping checks whether the underlying Redis client can reach the server.

func (*Redis) Set added in v0.1.7

func (cache *Redis) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Set stores a cache value.

type RedisClient added in v0.1.7

type RedisClient interface {
	Get(ctx context.Context, key string) *redis.StringCmd
	Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
	Del(ctx context.Context, keys ...string) *redis.IntCmd
	Close() error
}

RedisClient is the subset of go-redis used by the cache adapter.

type RedisPinger added in v0.1.7

type RedisPinger interface {
	Ping(ctx context.Context) *redis.StatusCmd
}

RedisPinger is implemented by go-redis clients that support PING health checks.

type TenantCache

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

TenantCache scopes all cache keys by tenant or explicitly allowed host global keys.

func NewTenantCache

func NewTenantCache(next Cache, opts ...Option) *TenantCache

NewTenantCache creates a scoped cache wrapper.

func (*TenantCache) Delete

func (cache *TenantCache) Delete(ctx context.Context, key string) error

Delete removes a scoped cache key.

func (*TenantCache) Get

func (cache *TenantCache) Get(ctx context.Context, key string) ([]byte, bool, error)

Get reads a scoped cache key.

func (*TenantCache) Set

func (cache *TenantCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Set writes a scoped cache key.

Jump to

Keyboard shortcuts

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