zhangqian
2023-09-21 290f2a28f42ef340db651901fa513c6beae8d899
service/cache_store.go
@@ -1,10 +1,13 @@
package service
import (
   "apsClient/constvar"
   "apsClient/model"
   "apsClient/model/response"
   "fmt"
   "github.com/spf13/cast"
   "sync"
   "time"
)
type CacheStore struct {
@@ -44,9 +47,10 @@
}
const (
   PlcCacheKey             = "plc:%v:%v"
   PlcCacheKey             = "plc:%v:%v" //plc:position:key
   CurrentTaskCacheKey     = "current_task"
   CurrentProgressCacheKey = "current_progress:%v"
   CurrentProgressCacheKey = "current_progress:%v"          //current_progress:position
   PlcCacheKeyUpdateTime   = "finish_number_update_time:%v" //finish_number_update_time:position
)
func PlcCacheGet(position int, key string) (interface{}, bool) {
@@ -54,9 +58,27 @@
}
func PlcCacheSet(position int, key string, value interface{}) {
   if key == constvar.PlcCacheKeyFinishNumber {
      oldFinishNumber, exists := PlcCacheGet(position, key)
      if !exists || cast.ToInt(oldFinishNumber) != cast.ToInt(value) { //finishNumber有了变化,设置更新时间缓存
         FinishUpdateTimeSet(position, time.Now().Unix())
      }
   }
   defaultCacheStore.Add(fmt.Sprintf(PlcCacheKey, position, key), value)
}
func FinishUpdateTimeGet(position int) interface{} {
   val, ok := defaultCacheStore.Get(fmt.Sprintf(PlcCacheKeyUpdateTime, position))
   if ok {
      return val
   }
   return 0
}
func FinishUpdateTimeSet(position int, value interface{}) {
   defaultCacheStore.Add(fmt.Sprintf(PlcCacheKeyUpdateTime, position), value)
}
func TaskCacheSet(value *response.TaskData) {
   defaultCacheStore.Add(CurrentTaskCacheKey, value)
}