redis

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package redis provides a Redis client wrapper (github.com/redis/go-redis/v9) for standalone and cluster modes, configured from config package.

Role in architecture:

  • Infrastructure: connects to Redis, exposes Cmdable; used by rate limiter and application cache/session code.

Responsibilities:

  • Setup: create standard or cluster client from config; no-op if Redis.Enabled is false.
  • Available: TCP reachability check for host:port without using the Redis client (no connection logs).
  • IsAlive: ping check. GetRedis/GetRedisCluster/GetUniversalClient: access the client.
  • Close: close the active client and release connections; safe to call when not enabled.

Constraints:

  • Single client per process; no provider switching or multi-instance.
  • Cluster mode is determined by config at Setup; no runtime switch.
  • common.go provides helpers (Get, Set, HGet, etc.) that call GetUniversalClient().

This package must NOT:

  • Contain business logic; only connection and command delegation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcquireLock

func AcquireLock(ctx context.Context, key string, value interface{}, expiration time.Duration) (bool, error)

func Available added in v0.3.5

func Available(host, port string, timeout time.Duration) bool

Available reports whether a Redis server is reachable at host:port within timeout. It uses a TCP dial only (no Redis client), so it does not trigger go-redis connection logs. Useful for tests or health checks before calling Setup().

func BGSave

func BGSave(ctx context.Context) error

func Close added in v0.3.0

func Close() error

Close closes the active Redis client and releases all connections. Safe to call even if Redis was never set up (no-op when not enabled).

func Delete

func Delete(ctx context.Context, key string) error

func ExtendLock

func ExtendLock(ctx context.Context, key string, expiration time.Duration) error

func Get

func Get(ctx context.Context, key string) (string, error)

func GetRedis

func GetRedis() *redis.Client

GetRedis returns the standard Redis client. Panics if cluster mode is enabled or Setup was not called. Panics if cluster mode is enabled or Setup was not called.

func GetRedisCluster added in v0.1.9

func GetRedisCluster() *redis.ClusterClient

GetRedisCluster returns the cluster Redis client. Panics if not in cluster mode or Setup was not called.

func GetUniversalClient added in v0.1.9

func GetUniversalClient() redis.Cmdable

GetUniversalClient returns a universal client interface that works with both standard and cluster clients.

func HGet

func HGet(ctx context.Context, key, field string) (string, error)

func HGetAll

func HGetAll(ctx context.Context, key string) (map[string]string, error)

func HSet

func HSet(ctx context.Context, key string, field string, value interface{}) error

func HSetMap

func HSetMap(ctx context.Context, key string, fields map[string]interface{}) error

func IsAlive

func IsAlive() bool

IsAlive returns true if the active client responds to Ping; false if not initialized or ping fails.

func LPush

func LPush(ctx context.Context, key string, values ...interface{}) error

func LRange

func LRange(ctx context.Context, key string, start, stop int64) ([]string, error)

func MGet

func MGet(ctx context.Context, keys ...string) ([]string, error)

func MSet

func MSet(ctx context.Context, pairs map[string]interface{}) error

func Pipeline

func Pipeline(ctx context.Context, f func(pipe redis.Pipeliner)) error

func PipelineSet

func PipelineSet(ctx context.Context, keyValues map[string]interface{}, expiration time.Duration) error

func PublishMessage

func PublishMessage(ctx context.Context, channel, message string) error

func RPop

func RPop(ctx context.Context, key string) (string, error)

func ReleaseLock

func ReleaseLock(ctx context.Context, key string) error

func SAdd

func SAdd(ctx context.Context, key string, members ...interface{}) error

func SMembers

func SMembers(ctx context.Context, key string) ([]string, error)

func SRem

func SRem(ctx context.Context, key string, members ...interface{}) error

func Save

func Save(ctx context.Context) error

func ScanKeys

func ScanKeys(ctx context.Context, pattern string, count int64) ([]string, error)

func Set

func Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error

func Setup

func Setup() error

Setup initializes the Redis client from config: standard client or cluster client when Redis.ClusterMode is true. No-op if Redis.Enabled is false. Returns error on connect or ping failure.

func SubscribeToChannel

func SubscribeToChannel(ctx context.Context, channel string, handler func(message string)) error

SubscribeToChannel subscribes to a channel and calls handler for each message until ctx is cancelled.

Types

This section is empty.

Jump to

Keyboard shortcuts

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