forked from jftuga/TtlMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
252 lines (216 loc) · 7.17 KB
/
map.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
package ttl
import (
"context"
"maps"
"sync"
"sync/atomic"
"time"
)
type mapItem[V any] struct {
value V
itemTTL time.Duration
lastAccess atomic.Int64
}
func (i *mapItem[V]) touch() {
i.lastAccess.Store(time.Now().UnixNano())
}
// Map is a "time-to-live" map such that after a given amount of time, items in the map are deleted.
// Map is safe for concurrent use.
//
// When a [Map.Load] or [Map.Store] occurs, the lastAccess time is set to the current time.
// Therefore, only items that are not called by [Map.Load] or [Map.Store] will be deleted after
// the TTL expires.
//
// [Map.LoadPassive] can be used in which case the lastAccess time will *not* be updated.
//
// Adapted from: https://stackoverflow.com/a/25487392/452281
type Map[K comparable, V any] struct {
m map[K]*mapItem[V]
mtx sync.RWMutex
defaultTTL time.Duration
refreshOnLoad bool
stop chan bool
closed atomic.Bool
}
// NewMap returns a new [Map] with items expiring according to the defaultTTL specified if
// they have not been accessed within that duration. Access refresh can be overridden so that
// items expire after the TTL whether they have been accessed or not.
//
// [Map] objects returned by NewMap must be closed with [Map.Close] when they're no longer needed.
func NewMap[K comparable, V any](
defaultTTL time.Duration,
length int,
pruneInterval time.Duration,
refreshOnLoad bool,
) (m *Map[K, V]) {
ctx := context.Background()
return NewMapContext[K, V](ctx, defaultTTL, length, pruneInterval, refreshOnLoad)
}
// NewMapContext returns a new [Map] with items expiring according to the defaultTTL specified if
// they have not been accessed within that duration. Access refresh can be overridden so that
// items expire after the TTL whether they have been accessed or not.
//
// NewMapContext accepts a context. If the context is cancelled, the pruning process will automatically
// stop whether you've called [Map.Close] or not. It's safe to use either approach.
//
// context.Background() is perfectly acceptable as the default context, however you should
// [Map.Close] the Map yourself in that case.
//
// [Map] objects returned by NewMapContext may still be closed with [Map.Close] when they're no
// longer needed or if the cancellation of the context is not guaranteed.
func NewMapContext[K comparable, V any](
ctx context.Context,
defaultTTL time.Duration,
length int,
pruneInterval time.Duration,
refreshOnLoad bool,
) (m *Map[K, V]) {
if length < 0 {
length = 0
}
m = &Map[K, V]{
m: make(map[K]*mapItem[V], length),
defaultTTL: defaultTTL,
refreshOnLoad: refreshOnLoad,
stop: make(chan bool),
}
go func() {
ticker := time.NewTicker(pruneInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
m.Close()
return
case <-m.stop:
return
case now := <-ticker.C:
currentTime := now.UnixNano()
m.mtx.Lock()
maps.DeleteFunc(m.m, func(key K, item *mapItem[V]) bool {
return currentTime-item.lastAccess.Load() >= int64(item.itemTTL)
})
m.mtx.Unlock()
}
}
}()
return
}
// Close will terminate TTL pruning of the Map. If Close is not called on a Map after it's no longer
// needed, the Map will leak (unless the context has been cancelled).
//
// Close may be called multiple times and is safe to call even if the context has been cancelled.
func (m *Map[K, V]) Close() {
if m.closed.CompareAndSwap(false, true) {
close(m.stop)
}
}
// Length returns the current length of the [Map]'s internal map. Length is safe for concurrent use.
func (m *Map[K, V]) Length() int {
m.mtx.RLock()
defer m.mtx.RUnlock()
return len(m.m)
}
// Load will retrieve a value from the [Map], as well as a bool indicating whether the key was
// found. If the item was not found the value returned is undefined. Load is safe for concurrent
// use.
func (m *Map[K, V]) Load(key K) (value V, ok bool) {
return m.loadImpl(key, true)
}
// LoadPassive will retrieve a value from the [Map] (without updating that value's time to live),
// as well as a bool indicating whether the key was found. If the item was not found the value
// returned is undefined. LoadPassive is safe for concurrent use.
func (m *Map[K, V]) LoadPassive(key K) (value V, ok bool) {
return m.loadImpl(key, false)
}
// Store will insert a value into the [Map] with the default tome to live. If the key/value pair
// already exists, the last access time will be updated, but the TTL will not be changed. This
// is important if the key/value pair was created with a non-default TTL using [Map.StoreWithTTL].
// Store is safe for concurrent use.
func (m *Map[K, V]) Store(key K, value V) {
m.mtx.Lock()
defer m.mtx.Unlock()
it, ok := m.m[key]
if !ok {
it = &mapItem[V]{
itemTTL: m.defaultTTL,
}
m.m[key] = it
}
it.value = value
it.touch()
}
// StoreWithTTL will insert a value into the [Map] with a custom time to live. If the key/value pair
// already exists, the last access time will be updated and the TTL will not be changed to the
// parameter value. Store is safe for concurrent use.
func (m *Map[K, V]) StoreWithTTL(key K, value V, TTL time.Duration) {
m.mtx.Lock()
defer m.mtx.Unlock()
it, ok := m.m[key]
if !ok {
it = &mapItem[V]{}
m.m[key] = it
}
it.value = value
it.itemTTL = TTL
it.touch()
}
func (m *Map[K, V]) loadImpl(key K, update bool) (value V, ok bool) {
m.mtx.RLock()
defer m.mtx.RUnlock()
var it *mapItem[V]
if it, ok = m.m[key]; !ok {
return
}
value = it.value
if !update || !m.refreshOnLoad {
return
}
it.touch()
return
}
// Delete will remove a key and its value from the [Map]. Delete is safe for concurrent use.
func (m *Map[K, V]) Delete(key K) {
m.mtx.Lock()
defer m.mtx.Unlock()
delete(m.m, key)
}
// DeleteFunc deletes any key/value pairs from the [Map] for which del returns true. DeleteFunc is
// safe for concurrent use.
func (m *Map[K, V]) DeleteFunc(del func(key K, value V) bool) {
m.mtx.Lock()
defer m.mtx.Unlock()
for key, item := range m.m {
if del(key, item.value) {
delete(m.m, key)
}
}
}
// Clear will remove all key/value pairs from the [Map]. Clear is safe for concurrent use.
func (m *Map[K, V]) Clear() {
m.mtx.Lock()
defer m.mtx.Unlock()
clear(m.m)
}
// Range calls f sequentially for each key and value present in the [Map]. If f returns false, Range
// stops the iteration.
//
// Range is safe for concurrent use and supports modifying the value (assuming it's a reference
// type like a slice, map, or a pointer) within the range function. However, this requires a write
// lock on the Map – so you are not able to perform [Map.Delete] or [Map.Store] operations on the
// original [Map] directly within the range func, as that would cause a panic. Even an accessor
// like [Map.Load] or [Map.LoadPassive] would lock indefinitely.
//
// If you need to perform operations on the original [Map], do so in a new goroutine from within
// the range func – effectively deferring the operation until the Range completes.
//
// If you just need to delete items with a certain key or value, use [Map.DeleteFunc] instead.
func (m *Map[K, V]) Range(f func(key K, value V) bool) {
m.mtx.Lock()
defer m.mtx.Unlock()
for key, item := range m.m {
if !f(key, item.value) {
break
}
}
}