-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
236 lines (195 loc) · 5.15 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package goutil
import (
"sync"
"time"
)
var cacheListExpCB []func(cacheTime time.Duration)
var cacheListDelCB []func()
type CacheMap[K Hashable, V any] struct {
value map[K]V
err map[K]error
lastUse map[K]time.Time
exp time.Duration
mu sync.Mutex
null V
}
func init() {
go func() {
for {
time.Sleep(10 * time.Minute)
var cacheTime time.Duration
// SysFreeMemory returns the total free system memory in megabytes
mb := SysFreeMemory()
if mb < 200 && mb != 0 {
// low memory: remove cache items have not been accessed in over 10 minutes
cacheTime = 10 * time.Minute
} else if mb < 500 && mb != 0 {
// low memory: remove cache items have not been accessed in over 30 minutes
cacheTime = 30 * time.Minute
} else if mb < 2000 && mb != 0 {
// low memory: remove cache items have not been accessed in over 1 hour
cacheTime = 1 * time.Hour
} else if mb > 64000 {
// high memory: remove cache items have not been accessed in over 12 hour
cacheTime = 12 * time.Hour
} else if mb > 32000 {
// high memory: remove cache items have not been accessed in over 6 hour
cacheTime = 6 * time.Hour
} else if mb > 16000 {
// high memory: remove cache items have not been accessed in over 3 hour
cacheTime = 3 * time.Hour
}
for _, cb := range cacheListExpCB {
cb(cacheTime)
}
time.Sleep(10 * time.Second)
if mb := SysFreeMemory(); mb < 10 && mb != 0 {
for _, cb := range cacheListDelCB {
cb()
}
}
}
}()
}
// NewCache creates a new cache map
func NewCache[K Hashable, V any](exp time.Duration) *CacheMap[K, V] {
cache := CacheMap[K, V]{
value: map[K]V{},
err: map[K]error{},
lastUse: map[K]time.Time{},
exp: exp,
}
cacheListExpCB = append(cacheListExpCB, func(cacheTime time.Duration) {
if cacheTime == 0 || (cacheTime < cache.exp && cacheTime >= 3*time.Hour) {
cacheTime = cache.exp
}
if cacheTime == 0 {
return
}
cache.DelOld(cacheTime)
})
cacheListDelCB = append(cacheListDelCB, func() {
cache.DelOld(0)
})
return &cache
}
// Get returns a value or an error if it exists
//
// if the object key does not exist, it will return both a nil/zero value (of the relevant type) and nil error
func (cache *CacheMap[K, V]) Get(key K) (V, error) {
cache.mu.Lock()
defer cache.mu.Unlock()
if err, ok := cache.err[key]; ok {
cache.lastUse[key] = time.Now()
return cache.null, err
} else if val, ok := cache.value[key]; ok {
cache.lastUse[key] = time.Now()
return val, nil
}
return cache.null, nil
}
// Set sets or adds a new key with either a value, or an error
func (cache *CacheMap[K, V]) Set(key K, value V, err error) {
cache.mu.Lock()
defer cache.mu.Unlock()
if err != nil {
cache.err[key] = err
delete(cache.value, key)
cache.lastUse[key] = time.Now()
} else {
cache.value[key] = value
delete(cache.err, key)
cache.lastUse[key] = time.Now()
}
}
// Del removes a cache item by key
func (cache *CacheMap[K, V]) Del(key K) {
cache.mu.Lock()
defer cache.mu.Unlock()
delete(cache.value, key)
delete(cache.err, key)
delete(cache.lastUse, key)
}
// DelOld removes old cache items
func (cache *CacheMap[K, V]) DelOld(cacheTime time.Duration) {
cache.mu.Lock()
defer cache.mu.Unlock()
if cacheTime == 0 {
for key := range cache.lastUse {
delete(cache.value, key)
delete(cache.err, key)
delete(cache.lastUse, key)
}
return
}
now := time.Now().UnixNano()
for key, lastUse := range cache.lastUse {
if now-lastUse.UnixNano() > int64(cacheTime) {
delete(cache.value, key)
delete(cache.err, key)
delete(cache.lastUse, key)
}
}
}
// Has returns true if a key value exists and is not an error
func (cache *CacheMap[K, V]) Has(key K) bool {
cache.mu.Lock()
defer cache.mu.Unlock()
if _, ok := cache.err[key]; ok {
cache.lastUse[key] = time.Now()
return false
} else if _, ok := cache.value[key]; ok {
cache.lastUse[key] = time.Now()
return true
}
return false
}
// Expire sets the ttl for all cache items
func (cache *CacheMap[K, V]) Expire(exp time.Duration) bool {
cache.mu.Lock()
defer cache.mu.Unlock()
cache.exp = exp
return false
}
// Touch resets a cache items expiration so it will stay in the cache longer
func (cache *CacheMap[K, V]) Touch(key K) bool {
cache.mu.Lock()
defer cache.mu.Unlock()
cache.lastUse[key] = time.Now()
return false
}
// ForEach runs a callback function for each cache item that has not expired
//
// in the callback, return true to continue, and false to break the loop
func (cache *CacheMap[K, V]) ForEach(cb func(key K, value V) bool, touch ...bool) {
cache.mu.Lock()
keyList := []K{}
for key := range cache.value {
keyList = append(keyList, key)
}
cache.mu.Unlock()
now := time.Now()
for _, key := range keyList {
cache.mu.Lock()
if _, ok := cache.err[key]; ok {
cache.lastUse[key] = now
cache.mu.Unlock()
continue
}
if now.UnixNano()-cache.lastUse[key].UnixNano() > int64(cache.exp) {
delete(cache.value, key)
delete(cache.err, key)
delete(cache.lastUse, key)
cache.mu.Unlock()
continue
}
var val V
if v, ok := cache.value[key]; ok {
val = v
}
cache.mu.Unlock()
if !cb(key, val) {
break
}
}
}