package rdb import ( "context" "time" "github.com/go-redis/redis/v8" "github.com/spf13/viper" ) var ( rdb *redis.Client ) // 初始化连接 func InitClient() (err error) { rdb = redis.NewClient(&redis.Options{ Addr: viper.GetString("rdb.addr"), Password: viper.GetString("rdb.password"), // no password set DB: viper.GetInt("rdb.db"), // use default DB PoolSize: viper.GetInt("rdb.pool_size"), // 连接池大小 }) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() _, err = rdb.Ping(ctx).Result() return err } func GetRDB() *redis.Client { return rdb }