redis.go 983 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package cache
  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. Password: cfg.Redis.Password,
  19. DB: 1, //指定 哪个数据库,不然找不到数据
  20. })
  21. _, err := Redis.Ping().Result()
  22. if err != nil {
  23. return err
  24. }
  25. return nil
  26. }
  27. const (
  28. DeviceStateKey = "dev_stat_"
  29. ONLINE = "online"
  30. TLast = "tlast"
  31. DeviceDataKey = "dev_data_"
  32. TIME = "time"
  33. )
  34. // 将token存入到redis中
  35. func SetToken(uid string, uuid string) {
  36. Redis.Set("login:"+uid, uuid, time.Hour*1)
  37. }
  38. // 从redis中取出token
  39. func GetToken(uid string) (string, error) {
  40. return Redis.Get("login:" + uid).Result()
  41. }