cache

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: Apache-2.0 Imports: 7 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")
)

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 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