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 ¶
- Variables
- func WithCredential(ctx context.Context, cred Credential) context.Context
- func WithCredentialProvider(ctx context.Context, provider CredentialProvider) context.Context
- type Cacher
- type Client
- func (c *Client) Close() error
- func (c *Client) Del(ctx context.Context, key string) error
- func (c *Client) ForApp(appID, moduleID string) *Client
- func (c *Client) Get(ctx context.Context, key string) (string, error)
- func (c *Client) Set(ctx context.Context, key, value string, ttl time.Duration) error
- type ClientCache
- type Credential
- type CredentialProvider
- type CredentialProviderKey
Constants ¶
This section is empty.
Variables ¶
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 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 ¶
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) ForApp ¶
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.
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) 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.