redisdb

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 9 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var ErrRedisMissingRedisConn = errors.New("redis connection is nil")

Functions

func FxModule

func FxModule(options ...Option) fx.Option

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

func WithAddress(addr ...string) Option

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 WithDB

func WithDB(db int) Option

WithDB allows to set the database to be selected after connecting to the server.

func WithMasterName

func WithMasterName(masterName string) Option

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

func WithMaxIdleConns(idleConnsLimit int) Option

WithMaxIdleConns allows to set a maximum of idle connections by the client.

func WithMaxOpenLimit

func WithMaxOpenLimit(openConnsLimit int) Option

WithMaxOpenLimit allows to set a maximum of open connections by the client.

func WithPassword

func WithPassword(password string) Option

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).

type Repo

type Repo struct {
	DB *Client
}

func NewRepo

func NewRepo(db *Client) (*Repo, error)

Directories

Path Synopsis
Package redistest contains helpers for the tests
Package redistest contains helpers for the tests

Jump to

Keyboard shortcuts

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