redis.go 497 B

123456789101112131415161718192021222324252627
  1. package utils
  2. import (
  3. "github.com/go-redis/redis"
  4. "iot_manager_service/config"
  5. "time"
  6. )
  7. var Redis *redis.Client
  8. func InitRedis() error {
  9. cfg := config.Instance()
  10. addr := cfg.Redis.Host
  11. Redis = redis.NewClient(&redis.Options{
  12. Addr: addr,
  13. DialTimeout: 10 * time.Second,
  14. ReadTimeout: 30 * time.Second,
  15. WriteTimeout: 30 * time.Second,
  16. PoolSize: 10,
  17. PoolTimeout: 30 * time.Second,
  18. })
  19. _, err := Redis.Ping().Result()
  20. if err != nil {
  21. return err
  22. }
  23. return nil
  24. }