-
Notifications
You must be signed in to change notification settings - Fork 15
/
Cache.go
124 lines (109 loc) · 2.68 KB
/
Cache.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package s
import (
"github.com/ssgo/discover"
"sync"
"time"
)
type memoryCache struct {
value any
seconds int64
cacheTime int64
locker sync.Mutex
}
var memoryCaches = map[string]*memoryCache{}
var memoryCacheLocker = sync.Mutex{}
var memoryCacheStarted bool
func CacheByMemory(key string, seconds int64, maker func() any) any {
memoryCacheLocker.Lock()
cache := memoryCaches[key]
if cache == nil {
cache = &memoryCache{
value: nil,
seconds: 0,
cacheTime: 0,
locker: sync.Mutex{},
}
memoryCaches[key] = cache
}
memoryCacheLocker.Unlock()
cache.locker.Lock()
value := cache.value
nowTime := time.Now().Unix()
//fmt.Println(" >>>>>>>>>", value == nil, nowTime-cache.cacheTime, seconds)
if value == nil || nowTime-cache.cacheTime > seconds {
// 需要生成缓存
value = maker()
cache.value = value
cache.seconds = seconds
cache.cacheTime = time.Now().Unix()
}
cache.locker.Unlock()
return value
}
func StartMemoryCacheCleaner() {
NewTimerServer("memoryCacheCleaner", time.Minute, func(isRunning *bool) {
cleanList := make([]*memoryCache, 0)
nowTime := time.Now().Unix()
memoryCacheLocker.Lock()
for _, cache := range memoryCaches {
if cache.value != nil && nowTime-cache.cacheTime > cache.seconds {
cleanList = append(cleanList, cache)
}
}
memoryCacheLocker.Unlock()
if !*isRunning {
return
}
for _, cache := range cleanList {
cache.locker.Lock()
//fmt.Println(" !!!!!>>>>", cache.value != nil, nowTime-cache.cacheTime, cache.seconds)
if cache.value != nil && nowTime-cache.cacheTime > cache.seconds {
cache.value = nil
cache.cacheTime = 0
}
cache.locker.Unlock()
if !*isRunning {
return
}
}
}, func() {
Subscribe("S_ClearMemoryCache_"+discover.Config.App, func() {
// 连接重置时清除所有缓存
//fmt.Println("######## 2.1")
clearAllMemoryCache()
}, func(msgBytes []byte) {
clearMemoryCache(string(msgBytes))
})
memoryCacheStarted = true
}, func() {
//fmt.Println("######## 2.2")
clearAllMemoryCache()
})
}
func ClearMemoryCache(key string) {
if memoryCacheStarted {
// 清除服务启动时,通过订阅通知进行缓存清理
//fmt.Println("######## Publish ", key)
Publish("S_ClearMemoryCache_"+discover.Config.App, key)
} else {
clearMemoryCache(key)
}
}
func clearMemoryCache(key string) {
//fmt.Println("######## 1")
memoryCacheLocker.Lock()
cache := memoryCaches[key]
memoryCacheLocker.Unlock()
if cache == nil {
return
}
cache.locker.Lock()
cache.value = nil
cache.cacheTime = 0
cache.locker.Unlock()
}
func clearAllMemoryCache() {
memoryCacheLocker.Lock()
memoryCaches = map[string]*memoryCache{}
memoryCacheLocker.Unlock()
}