| 12345678910111213141516171819202122232425 |
- package config
- import (
- "context"
- "fmt"
- "github.com/redis/go-redis/v9"
- "github.com/spf13/viper"
- "go.uber.org/zap"
- )
- var ctx = context.Background()
- var Rdb *redis.Client
- func InitRedis() {
- Rdb = redis.NewClient(&redis.Options{
- Addr: fmt.Sprintf("%s:%s", viper.GetString("redis.host"), viper.GetString("redis.port")),
- Password: viper.GetString("redis.password"), // no password set
- DB: viper.GetInt("redis.db"), // use default DB
- })
- if err = Rdb.Ping(ctx).Err(); err != nil {
- zap.L().Error("Redis连接出错 " + err.Error())
- panic("Redis连接出错,请检查参数: " + err.Error())
- }
- }
|