cache

package
v0.4.20 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package cache provides scoped Redis caching with per-app key prefix isolation.

Production: credentials injected per invocation via Lambda payload. Dev: REDIS_URL env var with localhost fallback.

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheMiss = errors.New("mirrorstack/cache: key not found")

ErrCacheMiss is returned by Get when the key does not exist.

Functions

func WithCredential

func WithCredential(ctx context.Context, cred Credential) context.Context

WithCredential returns a context with the cache credential set.

func WithCredentialProvider

func WithCredentialProvider(ctx context.Context, provider CredentialProvider) context.Context

WithCredentialProvider installs a renewable cache credential source.

Types

type Cacher

type Cacher interface {
	Set(ctx context.Context, key, value string, ttl time.Duration) error
	Get(ctx context.Context, key string) (string, error)
	Del(ctx context.Context, key string) error
}

Cacher is the interface module developers see when they call Module.Cache(ctx). Close is intentionally NOT exposed: the underlying *Client is owned by the SDK's ClientCache and must not be closed by handler code.

type Client

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

Client wraps a go-redis client with app-scoped key prefixing.

func New

func New(ctx context.Context, redisURL string) (*Client, error)

New creates a Client from a Redis URL string.

func NewFromCredential

func NewFromCredential(ctx context.Context, cred Credential) (*Client, error)

NewFromCredential creates a Client from platform-injected credentials. Enforces TLS for production ElastiCache connections.

func NewFromProvider

func NewFromProvider(ctx context.Context, provider CredentialProvider) (*Client, error)

NewFromProvider creates a Redis client that reacquires credentials whenever go-redis authenticates a new connection. Endpoint/username are pinned to the first credential so renewal cannot silently cross a resource boundary.

func Open

func Open(ctx context.Context) (*Client, error)

Open creates a Client from REDIS_URL env var, falling back to localhost. Cannot be used in Lambda — credentials are injected per invocation.

func (*Client) Close

func (c *Client) Close() error

Close closes the Redis connection.

func (*Client) Del

func (c *Client) Del(ctx context.Context, key string) error

Del removes a key.

func (*Client) ForApp

func (c *Client) ForApp(appID, moduleID string) *Client

ForApp returns a new Client sharing the same Redis connection but with an app-scoped key prefix. Key pattern: {appID}:{moduleID}:{key} Validates both IDs to prevent colon injection breaking prefix boundary.

func (*Client) Get

func (c *Client) Get(ctx context.Context, key string) (string, error)

Get retrieves a value. Returns ErrCacheMiss if the key does not exist.

func (*Client) Set

func (c *Client) Set(ctx context.Context, key, value string, ttl time.Duration) error

Set stores a value with the given TTL. TTL of 0 means no expiration.

type ClientCache

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

ClientCache manages per-(endpoint, username) Redis clients. It is a thin wrapper around refcache.Cache that adds credential validation, key derivation, and client construction. The refcount + LRU + double-checked-locking lifecycle is implemented in refcache.

func NewClientCache

func NewClientCache() *ClientCache

NewClientCache creates a ClientCache with default settings.

func (*ClientCache) Close

func (c *ClientCache) Close()

Close closes all cached clients.

func (*ClientCache) Get

func (c *ClientCache) Get(ctx context.Context, cred Credential) (*Client, func(), error)

Get returns a client for the given credential plus a release closure. The client is refcount-pinned until release runs, so concurrent eviction cannot close it. Pair every Get with a deferred release call.

func (*ClientCache) GetProvider

func (c *ClientCache) GetProvider(ctx context.Context, provider CredentialProvider) (*Client, func(), error)

GetProvider returns a client backed by renewable authentication.

type Credential

type Credential struct {
	Endpoint  string    `json:"endpoint"` // host:port
	Username  string    `json:"username"` // per-module ACL user (e.g., "mod_media")
	Token     string    `json:"token"`    // IAM auth token
	ExpiresAt time.Time `json:"expiresAt,omitzero"`
}

Credential holds Redis connection details injected by the platform per invocation.

func CredentialFrom

func CredentialFrom(ctx context.Context) *Credential

CredentialFrom reads the cache credential from the context.

type CredentialProvider

type CredentialProvider interface {
	Credential(context.Context) (Credential, error)
}

CredentialProvider supplies renewable Redis credentials. Endpoint and Username must remain stable for the provider's lifetime.

func CredentialProviderFrom

func CredentialProviderFrom(ctx context.Context) CredentialProvider

CredentialProviderFrom reads the renewable cache credential source.

type CredentialProviderKey

type CredentialProviderKey interface {
	CredentialProviderKey() string
}

CredentialProviderKey may be implemented by a job-scoped provider to allow safe client reuse within that provider's lifetime. An unkeyed provider is never shared with another task attempt.

Jump to

Keyboard shortcuts

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