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 ¶
- func AcquireLock(ctx context.Context, key string, value interface{}, expiration time.Duration) (bool, error)
- func Available(host, port string, timeout time.Duration) bool
- func BGSave(ctx context.Context) error
- func Close() error
- func Delete(ctx context.Context, key string) error
- func ExtendLock(ctx context.Context, key string, expiration time.Duration) error
- func Get(ctx context.Context, key string) (string, error)
- func GetRedis() *redis.Client
- func GetRedisCluster() *redis.ClusterClient
- func GetUniversalClient() redis.Cmdable
- func HGet(ctx context.Context, key, field string) (string, error)
- func HGetAll(ctx context.Context, key string) (map[string]string, error)
- func HSet(ctx context.Context, key string, field string, value interface{}) error
- func HSetMap(ctx context.Context, key string, fields map[string]interface{}) error
- func IsAlive() bool
- func LPush(ctx context.Context, key string, values ...interface{}) error
- func LRange(ctx context.Context, key string, start, stop int64) ([]string, error)
- func MGet(ctx context.Context, keys ...string) ([]string, error)
- func MSet(ctx context.Context, pairs map[string]interface{}) error
- func Pipeline(ctx context.Context, f func(pipe redis.Pipeliner)) error
- func PipelineSet(ctx context.Context, keyValues map[string]interface{}, ...) error
- func PublishMessage(ctx context.Context, channel, message string) error
- func RPop(ctx context.Context, key string) (string, error)
- func ReleaseLock(ctx context.Context, key string) error
- func SAdd(ctx context.Context, key string, members ...interface{}) error
- func SMembers(ctx context.Context, key string) ([]string, error)
- func SRem(ctx context.Context, key string, members ...interface{}) error
- func Save(ctx context.Context) error
- func ScanKeys(ctx context.Context, pattern string, count int64) ([]string, error)
- func Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
- func Setup() error
- func SubscribeToChannel(ctx context.Context, channel string, handler func(message string)) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AcquireLock ¶
func Available ¶ added in v0.3.5
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 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 GetRedis ¶
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
GetUniversalClient returns a universal client interface that works with both standard and cluster clients.
func IsAlive ¶
func IsAlive() bool
IsAlive returns true if the active client responds to Ping; false if not initialized or ping fails.
func PipelineSet ¶
Types ¶
This section is empty.