redis.go 573 B

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