redis

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package redis provides functionality for establishing connections to Redis servers using the go-redis package "github.com/redis/go-redis/v9". It allows you to connect to specific Redis servers, configure connection parameters, and reuse existing connections. The package also handles configurations such as server addresses derived from the application configuration.

Index

Constants

This section is empty.

Variables

View Source
var Nil = redis.Nil

Functions

func IsNil

func IsNil(err error) bool

IsNil reports whether the error represents a missing key.

func SetConnectionConfig

func SetConnectionConfig(clusterName string, config *ConnectionConfig)

SetConnectionConfig sets the configuration for a Redis server.

Types

type Client

type Client = redis.Client

Type aliases and helpers to avoid leaking go-redis into downstream packages.

type ConnectionConfig

type ConnectionConfig struct {
	Address  string //  The Redis server address in "host:port" format (e.g., "localhost:6379").
	Username string // Username for authentication (used when Redis ACLs are enabled). Leave empty if not applicable.
	Password string // Password for authentication. Leave empty if no password is set.
	Database int    // Redis logical database number (default is 0).
	PoolSize int    // Maximum number of socket connections to maintain in the pool.
}

ConnectionConfig holds the configuration parameters required to establish a connection to a Redis server. It includes details such as server address, authentication credentials, target database index, and connection pool settings.

func GetConnectionConfig

func GetConnectionConfig(clusterName string) *ConnectionConfig

GetConnectionConfig retrieves the configuration for a Redis server

type IntCmd

type IntCmd = redis.IntCmd

func NewIntResult

func NewIntResult(val int64, err error) *IntCmd

type Redis

type Redis struct {
	// contains filtered or unexported fields
}

Redis encapsulates the client instance for a Redis server.

func (*Redis) Close

func (r *Redis) Close() error

Close shuts down the connection to Redis.

func (*Redis) GetClient

func (r *Redis) GetClient() RedisClientInterface

GetClient returns the underlying client for the Redis instance. This is not the real raw client, and rather still a client wrapped in an interface. Use GetRawClient() for real raw client that Redis itself provides.

func (*Redis) GetRawClient

func (r *Redis) GetRawClient() *redis.Client

GetRawClient returns the underlying client for Redis instance. This is the real raw connection without any wrapper interface (unlike `GetClient()`).

func (*Redis) SetClient

func (r *Redis) SetClient(client RedisClientInterface)

type RedisClientInterface

type RedisClientInterface interface {
	// Basic Key operations
	Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
	Get(ctx context.Context, key string) *redis.StringCmd
	Del(ctx context.Context, keys ...string) *redis.IntCmd
	MGet(ctx context.Context, keys ...string) *redis.SliceCmd
	MSet(ctx context.Context, values ...interface{}) *redis.StatusCmd
	Exists(ctx context.Context, keys ...string) *redis.IntCmd
	Expire(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd
	TTL(ctx context.Context, key string) *redis.DurationCmd
	Persist(ctx context.Context, key string) *redis.BoolCmd
	Keys(ctx context.Context, pattern string) *redis.StringSliceCmd

	// Increment/Decrement
	Incr(ctx context.Context, key string) *redis.IntCmd
	IncrBy(ctx context.Context, key string, value int64) *redis.IntCmd
	Decr(ctx context.Context, key string) *redis.IntCmd
	DecrBy(ctx context.Context, key string, value int64) *redis.IntCmd

	// String operations
	Append(ctx context.Context, key, value string) *redis.IntCmd
	GetSet(ctx context.Context, key string, value interface{}) *redis.StringCmd
	StrLen(ctx context.Context, key string) *redis.IntCmd

	// Hash operations
	HSet(ctx context.Context, key string, values ...interface{}) *redis.IntCmd
	HGet(ctx context.Context, key, field string) *redis.StringCmd
	HDel(ctx context.Context, key string, fields ...string) *redis.IntCmd
	HGetAll(ctx context.Context, key string) *redis.MapStringStringCmd
	HExists(ctx context.Context, key, field string) *redis.BoolCmd
	HLen(ctx context.Context, key string) *redis.IntCmd
	HMGet(ctx context.Context, key string, fields ...string) *redis.SliceCmd
	HMSet(ctx context.Context, key string, values ...interface{}) *redis.BoolCmd
	HIncrBy(ctx context.Context, key, field string, incr int64) *redis.IntCmd

	// List operations
	LPush(ctx context.Context, key string, values ...interface{}) *redis.IntCmd
	RPush(ctx context.Context, key string, values ...interface{}) *redis.IntCmd
	LPop(ctx context.Context, key string) *redis.StringCmd
	RPop(ctx context.Context, key string) *redis.StringCmd
	LRange(ctx context.Context, key string, start, stop int64) *redis.StringSliceCmd
	LRem(ctx context.Context, key string, count int64, value interface{}) *redis.IntCmd
	LTrim(ctx context.Context, key string, start, stop int64) *redis.StatusCmd
	LLen(ctx context.Context, key string) *redis.IntCmd

	// Set operations
	SAdd(ctx context.Context, key string, members ...interface{}) *redis.IntCmd
	SRem(ctx context.Context, key string, members ...interface{}) *redis.IntCmd
	SMembers(ctx context.Context, key string) *redis.StringSliceCmd
	SIsMember(ctx context.Context, key string, member interface{}) *redis.BoolCmd

	// Sorted Set operations
	ZAdd(ctx context.Context, key string, members ...redis.Z) *redis.IntCmd
	ZRem(ctx context.Context, key string, members ...interface{}) *redis.IntCmd
	ZRange(ctx context.Context, key string, start, stop int64) *redis.StringSliceCmd
	ZRemRangeByScore(ctx context.Context, key, min, max string) *redis.IntCmd

	// Pub/Sub
	Publish(ctx context.Context, channel string, message interface{}) *redis.IntCmd
	Subscribe(ctx context.Context, channels ...string) *redis.PubSub

	// Scripting
	Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd

	// Locking/Concurrency (Optional helpers)
	SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.BoolCmd

	// Ping
	Ping(ctx context.Context) *redis.StatusCmd

	// Connection/Client operations
	Close() error
}

RedisClientInterface defines a unified interface over the go-redis Client.

type RedisConnector

type RedisConnector struct{}

RedisConnector represents a connector to a Redis instance

func (*RedisConnector) New

New attempts to establish a connection to a Redis instance.

Parameters: - opt: Redis connection options

Returns: - An instance of RedisInterface, representing the connection

type RedisConnectorInterface

type RedisConnectorInterface interface {

	// New establishes a connection to the Redis server
	New(opt *redis.Options) RedisInterface
}

RedisConnectorInterface defines methods to establish a connection to a Redis cluster and retrieve a client interface.

type RedisInterface

type RedisInterface interface {
	GetClient() RedisClientInterface
	GetRawClient() *redis.Client
	SetClient(client RedisClientInterface)
	Close() error
}

RedisInterface defines methods to interact with a Redis server.

func Connect

func Connect(connector RedisConnectorInterface, serverName string) (RedisInterface, error)

Connect establishes a connection to a Redis server specified by the given serverName. If a connection to the server already exists, the existing connection is reused. This method uses a singleton pattern to ensure only one connection instance per serverName.

Parameters:

  • serverName: The name of the Redis server to connect to.

Returns:

  • RedisInterface: A pointer to the established Redis client connection.
  • error: An error object that describes the reason for any connection failures. It returns nil if the connection was successful.

Note: This method is thread-safe and uses mutexes to handle concurrent access.

func ConnectContext

func ConnectContext(ctx context.Context, connector RedisConnectorInterface, serverName string) (RedisInterface, error)

ConnectContext establishes or reuses a Redis connection for serverName. The context is checked before creating a new connection so startup callers can bind connection acquisition to their own cancellation/deadline policy.

type StatusCmd

type StatusCmd = redis.StatusCmd

func NewStatusResult

func NewStatusResult(val string, err error) *StatusCmd

type StringCmd

type StringCmd = redis.StringCmd

func NewStringResult

func NewStringResult(val string, err error) *StringCmd

Jump to

Keyboard shortcuts

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