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
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 WithLogger ¶ added in v1.3.0
WithLogger attaches a *slog.Logger used for lifecycle events. Defaults to slog.Default() when omitted.
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 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.