redis.go 631 B

12345678910111213141516171819202122232425
  1. package config
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/redis/go-redis/v9"
  6. "github.com/spf13/viper"
  7. "go.uber.org/zap"
  8. )
  9. var ctx = context.Background()
  10. var Rdb *redis.Client
  11. func InitRedis() {
  12. Rdb = redis.NewClient(&redis.Options{
  13. Addr: fmt.Sprintf("%s:%s", viper.GetString("redis.host"), viper.GetString("redis.port")),
  14. Password: viper.GetString("redis.password"), // no password set
  15. DB: viper.GetInt("redis.db"), // use default DB
  16. })
  17. if err = Rdb.Ping(ctx).Err(); err != nil {
  18. zap.L().Error("Redis连接出错 " + err.Error())
  19. panic("Redis连接出错,请检查参数: " + err.Error())
  20. }
  21. }