-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
273 lines (224 loc) · 5.98 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package cache
import (
"runtime"
"sync"
"time"
. "github.com/AlexandreChamard/go-algorithm"
"github.com/AlexandreChamard/go-priorityqueue"
)
type Cache[Key comparable, Value any] interface {
Set(Key, Value)
Get(Key) (value Value, ok bool)
Size() int
DeleteExpiredItems()
FlushKOldest(int)
Flush()
Stop()
}
type cacheOptions struct {
cacheDuration time.Duration // No expiration on 0
maxSize int // No size limit on 0
cacheOffset int // 10% of the maxSize on <=0 ; Remove that amount of items on reaching the maxSize
purgeTimer time.Duration // No auto purge on 0
}
func NewCacheOptions() *cacheOptions {
return &cacheOptions{
purgeTimer: time.Minute,
}
}
func (this *cacheOptions) CacheDuration(cacheDuration time.Duration) *cacheOptions {
this.cacheDuration = cacheDuration
return this
}
func (this *cacheOptions) NoExpiration() *cacheOptions {
return this.CacheDuration(0)
}
func (this *cacheOptions) MaxSize(maxSize int) *cacheOptions {
this.maxSize = maxSize
return this
}
func (this *cacheOptions) NoSizeLimit() *cacheOptions {
return this.MaxSize(0)
}
func (this *cacheOptions) CacheOffset(cacheOffset int) *cacheOptions {
this.cacheOffset = cacheOffset
return this
}
func (this *cacheOptions) DefaultCacheOffset() *cacheOptions {
return this.CacheOffset(0)
}
func (this *cacheOptions) PurgeTimer(purgeTimer time.Duration) *cacheOptions {
this.purgeTimer = purgeTimer
return this
}
func (this *cacheOptions) NoPurge() *cacheOptions {
return this.PurgeTimer(0)
}
// options must be define - use the NewCacheOptions to get the default values
func NewCache[Key comparable, Value any](options *cacheOptions) Cache[Key, Value] {
return newCache[Key, Value](options)
}
type cache[Key comparable, Value any] struct {
data map[Key]mapData[Value]
cacheDuration time.Duration
maxSize int
cacheOffset int
runningChan chan bool
mutex sync.RWMutex
}
type mapData[Value any] struct {
value Value
deleteAt int64
}
type cacheWrapper[Key comparable, Value any] struct {
*cache[Key, Value]
}
func newCache[Key comparable, Value any](options *cacheOptions) *cacheWrapper[Key, Value] {
if options == nil {
return nil
}
cache := &cache[Key, Value]{
data: map[Key]mapData[Value]{},
cacheDuration: options.cacheDuration,
maxSize: options.maxSize,
cacheOffset: Min(options.cacheOffset, options.maxSize),
}
if cache.cacheOffset <= 0 {
cache.cacheOffset = cache.maxSize / 10
}
cacheW := &cacheWrapper[Key, Value]{cache}
if options.purgeTimer > 0 {
cache.runningChan = make(chan bool)
go cache.runJanitor(options.purgeTimer)
runtime.SetFinalizer(cacheW, func(cache *cacheWrapper[Key, Value]) {
cache.Stop()
})
}
return cacheW
}
func (this *cache[Key, Value]) Set(key Key, value Value) {
this.set(key, value, this.cacheDuration, false)
}
func (this *cache[Key, Value]) SetWithExpiration(key Key, value Value, expiration time.Duration) {
this.set(key, value, expiration, true)
}
func (this *cache[Key, Value]) set(key Key, value Value, expiration time.Duration, forceExpiration bool) {
this.mutex.Lock()
if data, ok := this.data[key]; ok && !forceExpiration {
// If the key is aready defined, just reset its deletion time
this.resetExpiration(key, mapData[Value]{
value: value,
deleteAt: data.deleteAt,
}, this.cacheDuration)
} else {
// Delete oldest elements if the cache size is exceeded
if this.maxSize > 0 && this.Size() >= this.maxSize {
this.flushKOldest(this.cacheOffset + 1) // remove the k oldest items in the cache (10% by default)
}
// If not, insert it in the map and the pqueue
this.data[key] = mapData[Value]{
value: value,
deleteAt: computeExpirationTimestamp(expiration),
}
}
this.mutex.Unlock()
}
func (this *cache[Key, Value]) Get(key Key) (Value, bool) {
this.mutex.RLock()
data, ok := this.data[key]
this.mutex.RUnlock()
if ok && data.deleteAt != 0 && data.deleteAt <= time.Now().UnixNano() {
return struct{ v Value }{}.v, false
}
return data.value, ok
}
func (this *cache[Key, Value]) resetExpiration(key Key, data mapData[Value], expiration time.Duration) {
deleteAt := computeExpirationTimestamp(expiration)
if deleteAt > data.deleteAt {
this.data[key] = mapData[Value]{
value: data.value,
deleteAt: deleteAt,
}
}
}
func (this *cache[Key, Value]) DeleteExpiredItems() {
this.mutex.Lock()
now := time.Now().UnixNano()
for key, data := range this.data {
if data.deleteAt != 0 && data.deleteAt < now {
delete(this.data, key)
}
}
this.mutex.Unlock()
}
func (this *cache[Key, Value]) Size() int {
return len(this.data)
}
type keyDeleteTuple[Key any] struct {
key Key
deleteAt int64
}
func (this *cache[Key, Value]) FlushKOldest(n int) {
if n <= 0 {
return
}
this.mutex.Lock()
this.flushKOldest(n)
this.mutex.Unlock()
}
func (this *cache[Key, Value]) flushKOldest(n int) {
if n <= 0 {
return
}
if n >= this.Size() {
this.flush()
return
}
queue := priorityqueue.NewPriorityQueue(func(a, b keyDeleteTuple[Key]) bool {
return a.deleteAt < b.deleteAt
})
for key, value := range this.data {
queue.Push(keyDeleteTuple[Key]{
key: key,
deleteAt: value.deleteAt,
})
if queue.Size() > n {
queue.Pop()
}
}
for !queue.Empty() {
delete(this.data, queue.Front().key)
queue.Pop()
}
}
func (this *cache[Key, Value]) Flush() {
this.mutex.Lock()
this.flush()
this.mutex.Unlock()
}
func (this *cache[Key, Value]) flush() {
this.data = make(map[Key]mapData[Value])
}
func (this *cache[Key, Value]) Stop() {
this.mutex.Lock()
this.flush()
this.mutex.Unlock()
close(this.runningChan)
}
func computeExpirationTimestamp(expiration time.Duration) int64 {
if expiration > 0 {
return time.Now().Add(expiration).UnixNano()
}
return 0
}
// should ONLY be called by the cache builder (see NewCache function)
func (this *cache[Key, Value]) runJanitor(purgeTimer time.Duration) {
for {
select {
case <-this.runningChan:
return
case <-time.After(purgeTimer):
this.DeleteExpiredItems()
}
}
}