Documentation
¶
Overview ¶
Package redis wraps github.com/redis/go-redis/v9 with the lifecycle contract used across core: a typed Config, functional Options, and Shutdown / Healthcheck methods so a Client can be registered with app.App.
The Client is a lifecycle.Resource (not a Runner) — its only background work is the connection pool maintained by go-redis, which is an implementation detail. Callers use the typed Get/Set/Del helpers or call Unwrap to access the underlying *redis.Client for advanced operations (pipelines, pubsub, scripts).
Typical usage:
client, err := redis.New(ctx, cfg, redis.WithOtel())
if err != nil { log.Fatal(err) }
a := app.New()
a.Add(client) // registers Shutdown + Healthcheck
Index ¶
- type Client
- func (c *Client) Del(ctx context.Context, keys ...string) error
- func (c *Client) Get(ctx context.Context, key string) (string, error)
- func (c *Client) Healthcheck(ctx context.Context) error
- func (c *Client) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- func (c *Client) Shutdown(_ context.Context) error
- func (c *Client) Unwrap() *redis.Client
- type Config
- type Option
- func WithConnMaxIdleTime(d time.Duration) Option
- func WithConnMaxLifetime(d time.Duration) Option
- func WithConnectRetry(attempts int, backoff time.Duration) Option
- func WithDialTimeout(d time.Duration) Option
- func WithLogger(l *slog.Logger) Option
- func WithMaxRetries(n int) Option
- func WithMaxRetryBackoff(d time.Duration) Option
- func WithMinRetryBackoff(d time.Duration) Option
- func WithOtel() Option
- func WithPoolSize(n int) Option
- func WithPoolTimeout(d time.Duration) Option
- func WithReadTimeout(d time.Duration) Option
- func WithWriteTimeout(d time.Duration) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a thin wrapper around *redis.Client that adds lifecycle conformance and cheap convenience methods. For anything beyond Get/Set/Del, call Unwrap to get the underlying *redis.Client.
Implements lifecycle.Resource and lifecycle.Healthchecker.
func New ¶ added in v1.3.0
New connects to Redis and verifies the connection with a Ping. Returns a Client ready for use.
Returns an error if the initial Ping fails — callers should treat this as fatal and not continue startup.
func (*Client) Get ¶
Get returns the string value at key, or an error. Wraps redis.Client.Get for the common case.
func (*Client) Healthcheck ¶ added in v1.3.0
Healthcheck pings the Redis server. Implements lifecycle.Healthchecker.
Cheap (single PING command) and respects ctx deadlines via go-redis's per-command timeout pipeline.
func (*Client) Shutdown ¶ added in v1.3.0
Shutdown closes the underlying Redis client and releases the connection pool. Implements lifecycle.Resource.
Idempotent and concurrent-safe: the close runs exactly once; every subsequent call returns the same result.
The ctx parameter is currently ignored — go-redis's Close is synchronous and not context-aware. The signature matches lifecycle.Resource for uniform integration with app.App.
type Config ¶
type Config struct {
// Host is the Redis server hostname or IP. Required.
Host string
// Port is the Redis server TCP port. Required.
Port string
// Password is the AUTH password. Empty means no AUTH.
Password string
// DB is the Redis logical database number (0-15 by default). Default 0.
DB int
}
Config describes a single Redis instance to connect to. All fields are plain values with no struct tags — consumer apps map their viper keys to fields explicitly inside their own config.NewConfig().
type Option ¶ added in v1.3.0
type Option func(*options)
Option configures a new Client.
func WithConnMaxIdleTime ¶ added in v1.9.0
WithConnMaxIdleTime sets the maximum time a connection may sit idle before it is closed. Default: go-redis uses 30m. Zero leaves the go-redis default.
func WithConnMaxLifetime ¶ added in v1.9.0
WithConnMaxLifetime sets the maximum age of a connection before it is closed and replaced. Default: go-redis keeps connections forever. Use a finite value to encourage healthy rotation behind a load balancer / failover.
func WithConnectRetry ¶ added in v1.9.0
WithConnectRetry retries the initial connection Ping up to attempts times (fixed backoff, context-aware) before New returns an error. Unlike the millisecond-scale command retries (WithMaxRetries), this is coarse and meant to absorb a dependency that starts a few seconds after the pod boots, so the process comes up cleanly instead of relying on a container restart loop. Default: no retry (a single Ping).
func WithDialTimeout ¶ added in v1.9.0
WithDialTimeout sets the timeout for establishing new connections. Default: go-redis uses 5s. Zero leaves the go-redis default.
func WithLogger ¶ added in v1.3.0
WithLogger attaches a *slog.Logger used for lifecycle events. Defaults to slog.Default() when omitted.
func WithMaxRetries ¶ added in v1.9.0
WithMaxRetries sets how many times go-redis retries a command that fails with a transient network error, before giving up. Default: go-redis uses 3. Pass -1 to disable retries entirely. These are millisecond-scale command retries (see WithMinRetryBackoff / WithMaxRetryBackoff) — for smoothing a dependency that is slow to come up at boot, use WithConnectRetry instead.
func WithMaxRetryBackoff ¶ added in v1.9.0
WithMaxRetryBackoff sets the maximum backoff between command retries. Default: go-redis uses 512ms. Zero leaves the go-redis default.
func WithMinRetryBackoff ¶ added in v1.9.0
WithMinRetryBackoff sets the minimum backoff between command retries. Default: go-redis uses 8ms. Zero leaves the go-redis default.
func WithOtel ¶ added in v1.3.0
func WithOtel() Option
WithOtel enables OpenTelemetry tracing and metrics on every Redis command via go-redis's redisotel extension. Uses the global tracer and meter providers, so otel.Setup must run and be registered with app.App before New so the providers are non-noop.
One span per command is created with the command name, key (if available), and result/error status.
func WithPoolSize ¶ added in v1.3.0
WithPoolSize overrides the connection pool size. Default: go-redis uses 10*runtime.NumCPU() if unset.
func WithPoolTimeout ¶ added in v1.9.0
WithPoolTimeout sets how long a caller waits for a free connection from the pool before timing out. Default: go-redis uses ReadTimeout + 1s. Zero leaves the go-redis default.
func WithReadTimeout ¶ added in v1.3.0
WithReadTimeout overrides the per-command read timeout. Default: go-redis uses 3 seconds if unset.
func WithWriteTimeout ¶ added in v1.3.0
WithWriteTimeout overrides the per-command write timeout. Default: go-redis uses ReadTimeout if unset.