-
Notifications
You must be signed in to change notification settings - Fork 2
/
map_ttl.go
237 lines (196 loc) · 4.45 KB
/
map_ttl.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
package geche
import (
"context"
"sync"
"time"
)
// defaultCleanupInterval controls how often cache will purge obsolete values.
const defaultCleanupInterval = time.Second
type ttlRec[K comparable, V any] struct {
// linked list to maintain order
prev K
next K
value V
timestamp time.Time
}
// zero returns zero value for the type T.
func zero[T any]() T {
var z T
return z
}
// MapTTLCache is the thread-safe map-based cache with TTL cache invalidation support.
// MapTTLCache uses double linked list to maintain FIFO order of inserted values.
type MapTTLCache[K comparable, V any] struct {
data map[K]ttlRec[K, V]
mux sync.RWMutex
ttl time.Duration
now func() time.Time
tail K
head K
zero K
}
// NewMapTTLCache creates MapTTLCache instance and spawns background
// cleanup goroutine, that periodically removes outdated records.
// Cleanup goroutine will run cleanup once in cleanupInterval until ctx is canceled.
// Each record in the cache is valid for ttl duration since it was Set.
func NewMapTTLCache[K comparable, V any](
ctx context.Context,
ttl time.Duration,
cleanupInterval time.Duration,
) *MapTTLCache[K, V] {
if cleanupInterval == 0 {
cleanupInterval = defaultCleanupInterval
}
c := MapTTLCache[K, V]{
data: make(map[K]ttlRec[K, V]),
ttl: ttl,
now: time.Now,
zero: zero[K](), // cache zero value for comparisons.
}
go func(ctx context.Context) {
t := time.NewTicker(cleanupInterval)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
_ = c.cleanup()
}
}
}(ctx)
return &c
}
func (c *MapTTLCache[K, V]) Set(key K, value V) {
c.mux.Lock()
defer c.mux.Unlock()
c.set(key, value)
}
// SetIfPresent sets the given key to the given value if the key was already present, and resets the TTL
func (c *MapTTLCache[K, V]) SetIfPresent(key K, value V) (V, bool) {
c.mux.Lock()
defer c.mux.Unlock()
old, err := c.get(key)
if err == nil {
c.set(key, value)
return old, true
}
return old, false
}
// Get returns ErrNotFound if key is not found in the cache or record is outdated.
func (c *MapTTLCache[K, V]) Get(key K) (V, error) {
c.mux.RLock()
defer c.mux.RUnlock()
return c.get(key)
}
func (c *MapTTLCache[K, V]) Del(key K) error {
c.mux.Lock()
defer c.mux.Unlock()
rec, ok := c.data[key]
if !ok {
return nil
}
delete(c.data, key)
if key == c.head {
c.head = rec.next
}
if key == c.tail {
c.tail = rec.prev
}
if rec.prev != c.zero {
prev := c.data[rec.prev]
prev.next = rec.next
c.data[rec.prev] = prev
}
if rec.next != c.zero {
next := c.data[rec.next]
next.prev = rec.prev
c.data[rec.next] = next
}
return nil
}
// cleanup removes outdated records.
func (c *MapTTLCache[K, V]) cleanup() error {
c.mux.Lock()
defer c.mux.Unlock()
key := c.head
for {
rec, ok := c.data[key]
if !ok {
break
}
if c.now().Sub(rec.timestamp) < c.ttl {
break
}
c.head = rec.next
delete(c.data, key)
if key == c.tail {
c.tail = c.zero
return nil
}
next, ok := c.data[rec.next]
if ok {
next.prev = c.zero
c.data[rec.next] = next
}
key = rec.next
}
return nil
}
// Snapshot returns a shallow copy of the cache data.
// Locks the cache from modification for the duration of the copy.
func (c *MapTTLCache[K, V]) Snapshot() map[K]V {
c.mux.RLock()
defer c.mux.RUnlock()
snapshot := make(map[K]V, len(c.data))
for k, v := range c.data {
snapshot[k] = v.value
}
return snapshot
}
// Len returns the number of records in the cache.
func (c *MapTTLCache[K, V]) Len() int {
c.mux.RLock()
defer c.mux.RUnlock()
return len(c.data)
}
func (c *MapTTLCache[K, V]) set(key K, value V) {
val := ttlRec[K, V]{
value: value,
prev: c.tail,
timestamp: c.now(),
}
if c.head == c.zero {
c.head = key
c.tail = key
val.prev = c.zero
c.data[key] = val
return
}
// If the record for this key already exists
// and is somewhere in the middle of the list
// removing it before adding to the tail.
if rec, ok := c.data[key]; ok && key != c.tail {
prev := c.data[rec.prev]
next := c.data[rec.next]
prev.next = rec.next
next.prev = rec.prev
c.data[rec.prev] = prev
c.data[rec.next] = next
}
tailval := c.data[c.tail]
tailval.next = key
c.data[c.tail] = tailval
c.tail = key
c.data[key] = val
}
func (c *MapTTLCache[K, V]) get(key K) (V, error) {
v, ok := c.data[key]
if !ok {
return v.value, ErrNotFound
}
if c.now().Sub(v.timestamp) >= c.ttl {
return v.value, ErrNotFound
}
return v.value, nil
}