DeviceReal.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package Device
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/astaxie/beego/cache"
  8. _ "github.com/astaxie/beego/cache/redis"
  9. "github.com/beego/beego/v2/core/logs"
  10. "time"
  11. )
  12. var redisCache_DeviceReal cache.Cache
  13. func init() {
  14. deviceRealConfig := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  15. "redis_DeviceReal", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  16. fmt.Println(deviceRealConfig)
  17. var err error
  18. redisCache_DeviceReal, err = cache.NewCache("redis", deviceRealConfig)
  19. if err != nil || redisCache_DeviceReal == nil {
  20. errMsg := "failed to init redis api"
  21. logs.Error(errMsg, err)
  22. panic(errMsg)
  23. }
  24. }
  25. // ---------------- Redis -------------------
  26. func Redis_DeviceReal_Set(sn string, num int) (err error) {
  27. err = redisCache_DeviceReal.Put(sn, num, 1*time.Minute)
  28. if err != nil {
  29. logs.Error("redis_DeviceReal_Set", "set key:", sn, ",value:", num, err)
  30. }
  31. return
  32. }
  33. func Redis_DeviceReal_Get(key string) (r int, is bool) {
  34. if redisCache_DeviceReal.IsExist(key) {
  35. //println("找到key:",key)
  36. v := redisCache_DeviceReal.Get(key)
  37. err := json.Unmarshal(v.([]byte), &r)
  38. if err != nil {
  39. logs.Error(lib.FuncName(), err)
  40. return r, false
  41. }
  42. return r, true
  43. }
  44. //println("没有 找到key:",key)
  45. return r, false
  46. }
  47. func Redis_DeviceReal_DelK(key string) (err error) {
  48. err = redisCache_DeviceReal.Delete(key)
  49. if err != nil {
  50. logs.Error(lib.FuncName(), err)
  51. }
  52. return
  53. }
  54. func Redis_DeviceRealWait_Set(key string) (err error) {
  55. err = redisCache_DeviceReal.Put(key, "", 10*time.Second)
  56. if err != nil {
  57. logs.Error("Redis_DeviceRealWait_Set", "set key:", key, err)
  58. }
  59. return
  60. }
  61. func Redis_DeviceRealWait_IsExist(key string) (is bool) {
  62. return redisCache_DeviceReal.IsExist(key)
  63. }