zhangqian
2023-09-05 49e90e5de2e7166e74e26102dff9064b933fc5fd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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)
}