redis.go 500 B

1234567891011121314151617181920212223242526
  1. package rdb
  2. import (
  3. "context"
  4. "github.com/go-redis/redis/v8"
  5. "github.com/spf13/viper"
  6. "time"
  7. )
  8. func NewRedis(conf *viper.Viper) error {
  9. rdb := redis.NewClient(&redis.Options{
  10. Addr: conf.GetString("data.redis.addr"),
  11. Password: conf.GetString("data.redis.password"),
  12. DB: conf.GetInt("data.redis.db"),
  13. })
  14. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  15. defer cancel()
  16. _, err := rdb.Ping(ctx).Result()
  17. if err != nil {
  18. return err
  19. }
  20. return nil
  21. }