redisx

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 15 Imported by: 0

README

redisx

redisx 是对 go-redis/v9 单节点客户端的生产级封装。它把连接建立、启动探活、日志、Prometheus 指标和 OpenTelemetry trace 边界整理干净,同时保留原生 go-redis API。

设计原则

  • 不在 init() 中注册 Prometheus 指标,指标统一按需懒注册。
  • 默认在创建客户端后主动 PING,尽早暴露地址、认证和网络问题。
  • 启动探活和连接管理命令(如 HELLOAUTHCLIENT SETINFO)不进入业务日志、指标和 trace,避免把连接噪音混进业务观测面。
  • 成功命令默认走 Debug,慢命令走 Warn,失败命令走 Error,避免把日志面打爆。
  • 日志不记录命令参数和值,只记录命令名、参数数量、耗时和结果,避免泄露 token、session、业务键值。
  • Trace 默认不写入完整命令参数,避免把 key/value 带进 span。

快速开始

disableIdentity := true

client, err := redisx.Open(context.Background(), &redisx.Config{
    Name:           "cache-primary",
    Addr:           "127.0.0.1:6379",
    Password:       "",
    DB:             0,
    Trace:          true,
    EnableLogger:   true,
    DisableIdentity: &disableIdentity,
})
if err != nil {
    panic(err)
}
defer client.Close()

rdb := client.Redis()

if err := rdb.Set(context.Background(), "key", "value", 0).Err(); err != nil {
    panic(err)
}

val, err := rdb.Get(context.Background(), "key").Result()
fmt.Println(val, err)

API 摘要

type Client interface {
    Redis() *redis.Client
    Options() *redis.Options
    Ping(context.Context) error
    Stats() redis.PoolStats
    AddHook(redis.Hook) error
    Close() error
}

默认行为

  • Open 默认会 PING;如果你明确要延迟连接,可以设置 SkipPing
  • DisableIdentity 默认开启;如果你要显式关闭,需要传入一个值为 false 的指针,这样配置语义才不会和零值冲突
  • Trace 默认不包含完整命令参数;如果你确实需要,可以设置 TraceIncludeCommandArgs
  • 如果你需要更底层控制,可以直接传 Options

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilConfig       = errors.New("redisx: config is required")
	ErrContextRequired = errors.New("redisx: context is required")
	ErrAddrRequired    = errors.New("redisx: addr is required")
	ErrNilHook         = errors.New("redisx: hook is required")
)

Functions

This section is empty.

Types

type Client added in v1.2.2

type Client interface {
	Redis() *redis.Client
	Options() *redis.Options
	Ping(context.Context) error
	Stats() redis.PoolStats
	AddHook(redis.Hook) error
	Close() error
}

func New

func New(conf *Config) (Client, error)

func Open added in v1.2.2

func Open(ctx context.Context, conf *Config) (Client, error)

type Config

type Config struct {
	Name string

	Options *redis.Options

	Network               string
	Addr                  string
	Username              string
	Password              string
	DB                    int
	ClientName            string
	Protocol              int
	Dialer                func(context.Context, string, string) (net.Conn, error)
	OnConnect             func(context.Context, *redis.Conn) error
	MaxRetries            int
	MinRetryBackoff       time.Duration
	MaxRetryBackoff       time.Duration
	DialTimeout           time.Duration
	ReadTimeout           time.Duration
	WriteTimeout          time.Duration
	ContextTimeoutEnabled bool
	ReadBufferSize        int
	WriteBufferSize       int
	PoolFIFO              bool
	PoolSize              int
	MinIdleConns          int
	MaxIdleConns          int
	MaxActiveConns        int
	PoolTimeout           time.Duration
	ConnMaxIdleTime       time.Duration
	ConnMaxLifetime       time.Duration
	TLSConfig             *tls.Config
	DisableIdentity       *bool
	IdentitySuffix        string

	SkipPing      bool
	PingTimeout   time.Duration
	SlowThreshold time.Duration

	Trace                   bool
	TraceProvider           trace.TracerProvider
	TraceAttributes         []attribute.KeyValue
	TraceIncludeCommandArgs bool
	TraceCaller             bool

	Logger            *logger.Logger
	EnableLogger      bool
	DisableMetrics    bool
	MetricsRegisterer prometheus.Registerer
}

Jump to

Keyboard shortcuts

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