Documentation
¶
Overview ¶
Package redisdb provides a Redis client wrapper with connection pooling, configuration management, and FX integration.
The client supports both single-instance and cluster/sentinel configurations via redis.UniversalClient. Configuration is primarily driven by environment variables, but can be overridden using functional options.
Environment Variables:
- REDIS_ADDRESS: Comma-separated list of Redis server addresses (required)
- REDIS_PASSWORD: Password for authentication (optional)
- REDIS_MASTER_NAME: Sentinel master name for failover scenarios (optional)
Example usage:
client, err := redisdb.New(monitor)
if err != nil {
log.Fatal(err)
}
defer client.Close()
// Use client
err = client.Set(ctx, "key", "value", 0).Err()
Package redisdb contains the wrapper for the redis client
Index ¶
- Variables
- func FxModule(options ...Option) fx.Option
- type Client
- type ConnectionErr
- type Option
- func WithAddress(addr ...string) Option
- func WithAddressFromEnv() Option
- func WithDB(db int) Option
- func WithMasterName(masterName string) Option
- func WithMasterNameFromEnv() Option
- func WithMaxIdleConns(idleConnsLimit int) Option
- func WithMaxOpenLimit(openConnsLimit int) Option
- func WithPassword(password string) Option
- func WithPasswordFromEnv() Option
- type Repo
Constants ¶
This section is empty.
Variables ¶
var ErrRedisMissingRedisConn = errors.New("redis connection is nil")
Functions ¶
Types ¶
type Client ¶
type Client struct {
redis.UniversalClient
}
Client wraps redis.UniversalClient to provide a unified interface for both standalone and cluster Redis deployments. The embedding allows direct access to all redis.UniversalClient methods while enabling future extensions.
func New ¶
func New(m monitoring.Monitor, options ...Option) (*Client, error)
New creates a new Redis client with the given monitoring and options. It establishes a connection to Redis, verifies connectivity via PING, and configures keyspace notifications for pub/sub functionality.
The client uses connection pooling with default limits of 100 max open connections and 10 max idle connections. These can be customized using WithMaxOpenLimit and WithMaxIdleConns options.
Returns an error if connection fails, PING doesn't return PONG, or if keyspacenotifications configuration fails.
type ConnectionErr ¶
type ConnectionErr struct {
// contains filtered or unexported fields
}
ConnectionErr defines a database connection error.
func (ConnectionErr) Error ¶
func (err ConnectionErr) Error() string
Error implements the error interface with additional context.
func (ConnectionErr) Unwrap ¶
func (err ConnectionErr) Unwrap() error
Unwrap returns the child error (the specific error reason of the connection error).
type Option ¶
type Option func(*config)
Option defines the contract for options applied to a redis.Client.
func WithAddress ¶
WithAddress allows to set the host:port address
func WithAddressFromEnv ¶
func WithAddressFromEnv() Option
WithAddressFromEnv allows to set the host:port address by reading the REDIS_ADDRESS envvar (this is a default option).
func WithMasterName ¶
WithMasterName allows to set the master name for the sentinel.
func WithMasterNameFromEnv ¶
func WithMasterNameFromEnv() Option
WithMasterNameFromEnv allows to set the master name for the sentinel This option only works with NewFailoverClusterClient.
func WithMaxIdleConns ¶
WithMaxIdleConns allows to set a maximum of idle connections by the client.
func WithMaxOpenLimit ¶
WithMaxOpenLimit allows to set a maximum of open connections by the client.
func WithPassword ¶
WithPassword allows to set the password.
func WithPasswordFromEnv ¶
func WithPasswordFromEnv() Option
WithPasswordFromEnv allows to set the password by reading the REDIS_PASSWORD envvar (this is a default option).