12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package Device
- import (
- "Cold_Api/conf"
- "Cold_Api/controllers/lib"
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- "github.com/beego/beego/v2/core/logs"
- "time"
- )
- var redisCache_DeviceReal cache.Cache
- func init() {
- deviceRealConfig := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_DeviceReal", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- fmt.Println(deviceRealConfig)
- var err error
- redisCache_DeviceReal, err = cache.NewCache("redis", deviceRealConfig)
- if err != nil || redisCache_DeviceReal == nil {
- errMsg := "failed to init redis api"
- logs.Error(errMsg, err)
- panic(errMsg)
- }
- }
- // ---------------- Redis -------------------
- func Redis_DeviceReal_Set(sn string, num int) (err error) {
- err = redisCache_DeviceReal.Put(sn, num, 1*time.Minute)
- if err != nil {
- logs.Error("redis_DeviceReal_Set", "set key:", sn, ",value:", num, err)
- }
- return
- }
- func Redis_DeviceReal_Get(key string) (r int, is bool) {
- if redisCache_DeviceReal.IsExist(key) {
- //println("找到key:",key)
- v := redisCache_DeviceReal.Get(key)
- err := json.Unmarshal(v.([]byte), &r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return r, false
- }
- return r, true
- }
- //println("没有 找到key:",key)
- return r, false
- }
- func Redis_DeviceReal_DelK(key string) (err error) {
- err = redisCache_DeviceReal.Delete(key)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- func Redis_DeviceRealWait_Set(key string) (err error) {
- err = redisCache_DeviceReal.Put(key, "", 10*time.Second)
- if err != nil {
- logs.Error("Redis_DeviceRealWait_Set", "set key:", key, err)
- }
- return
- }
- func Redis_DeviceRealWait_IsExist(key string) (is bool) {
- return redisCache_DeviceReal.IsExist(key)
- }
|