package service
|
|
import (
|
"apsClient/model"
|
"apsClient/model/response"
|
"fmt"
|
"sync"
|
)
|
|
type CacheStore struct {
|
cache map[string]interface{}
|
mu sync.Mutex
|
}
|
|
var defaultCacheStore *CacheStore
|
|
func init() {
|
defaultCacheStore = newCacheManager()
|
}
|
func newCacheManager() *CacheStore {
|
return &CacheStore{
|
cache: make(map[string]interface{}),
|
}
|
}
|
|
func (cm *CacheStore) Get(key string) (interface{}, bool) {
|
cm.mu.Lock()
|
defer cm.mu.Unlock()
|
|
conn, ok := cm.cache[key]
|
return conn, ok
|
}
|
|
func (cm *CacheStore) Add(key string, value interface{}) {
|
cm.mu.Lock()
|
defer cm.mu.Unlock()
|
cm.cache[key] = value
|
}
|
|
func (cm *CacheStore) Remove(key string) {
|
cm.mu.Lock()
|
defer cm.mu.Unlock()
|
delete(cm.cache, key)
|
}
|
|
const (
|
PlcCacheKey = "plc:%v"
|
CurrentTaskCacheKey = "current_task"
|
CurrentProgressCacheKey = "current_progress"
|
)
|
|
func PlcCacheGet(key string) (interface{}, bool) {
|
return defaultCacheStore.Get(fmt.Sprintf(PlcCacheKey, key))
|
}
|
|
func PlcCacheSet(key string, value interface{}) {
|
defaultCacheStore.Add(fmt.Sprintf(PlcCacheKey, key), value)
|
}
|
|
func TaskCacheSet(value *response.TaskData) {
|
defaultCacheStore.Add(CurrentTaskCacheKey, value)
|
}
|
|
func TaskCacheUnset() {
|
defaultCacheStore.Remove(CurrentTaskCacheKey)
|
}
|
|
func TaskCacheGet() (*response.TaskData, bool) {
|
if v, ok := defaultCacheStore.Get(CurrentTaskCacheKey); ok {
|
return v.(*response.TaskData), ok
|
}
|
return nil, false
|
}
|
|
func ProgressCacheGet() (*model.ProductionProgress, bool) {
|
if v, ok := defaultCacheStore.Get(CurrentProgressCacheKey); ok {
|
return v.(*model.ProductionProgress), ok
|
}
|
return nil, false
|
}
|
|
func ProgressCacheSet(value *model.ProductionProgress) {
|
defaultCacheStore.Add(CurrentProgressCacheKey, value)
|
}
|
|
func ProgressCacheUnset() {
|
defaultCacheStore.Remove(CurrentProgressCacheKey)
|
}
|