-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcache.go
334 lines (271 loc) · 7.25 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Package pocache implements an in-memory, LRU cache, with preemptive update feature.
package pocache
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
lru "github.com/hashicorp/golang-lru/v2"
)
var (
ErrValidation = errors.New("invalid")
ErrPanic = errors.New("panicked")
)
type (
// ErrOnUpdate defines the type of the hook function, which is called
// if there's any error when trying to update a key in the background
ErrOnUpdate func(err error)
// Updater defines the function which is used to get the new value
// of a key. This is required for pocache to do background updates
Updater[K comparable, T any] func(ctx context.Context, key K) (T, error)
// Store defines the interface required for the underlying storage of pocache.
Store[K comparable, T any] interface {
Add(key K, value *Payload[T]) (evicted bool)
Get(key K) (value *Payload[T], found bool)
Remove(key K) (present bool)
}
)
type Config[K comparable, T any] struct {
// LRUCacheSize is the number of keys to be maintained in the cache
LRUCacheSize uint
// QLength is the length of update and delete queue
QLength uint
// CacheAge is for how long the cache would be maintained, apart from the LRU eviction
// It's maintained to not maintain stale data if/when keys are not evicted based on LRU
CacheAge time.Duration
// Threshold is the duration prior to expiry, when the key is considered eligible to be updated
Threshold time.Duration
DisableCache bool
// ServeStale will not return error if the cache has expired. It will return the stale
// value, and trigger an update as well. This is useful for usecases where it's ok
// to serve stale values and data consistency is not of paramount importance
ServeStale bool
// UpdaterTimeout is the context time out for when the updater function is called
UpdaterTimeout time.Duration
Updater Updater[K, T]
Store Store[K, T]
// ErrWatcher is called when there's any error when trying to update cache
ErrWatcher ErrOnUpdate
}
func (cfg *Config[K, T]) Sanitize() {
if cfg.LRUCacheSize == 0 {
cfg.LRUCacheSize = 1000
}
if cfg.QLength == 0 {
cfg.QLength = 1000
}
if cfg.CacheAge <= 0 {
cfg.CacheAge = time.Minute
}
if cfg.Threshold <= 0 {
cfg.Threshold = cfg.CacheAge - time.Second
}
if cfg.UpdaterTimeout <= 0 {
cfg.UpdaterTimeout = time.Second
}
}
func (cfg *Config[K, T]) Validate() error {
if cfg.LRUCacheSize == 0 {
return errors.Join(
ErrValidation,
fmt.Errorf("LRU cache size cannot be 0"),
)
}
if cfg.CacheAge <= cfg.Threshold {
return errors.Join(
ErrValidation,
fmt.Errorf(
"cache age %s cannot be shorter than threshold %s",
cfg.CacheAge,
cfg.Threshold,
))
}
return nil
}
func (cfg *Config[K, T]) SanitizeValidate() error {
cfg.Sanitize()
return cfg.Validate()
}
type Payload[T any] struct {
// ExpireAt is an atomic pointer to avoid race condition
// while concurrently reading the timestamp
ExpireAt *atomic.Pointer[time.Time]
Payload T
}
func (pyl *Payload[T]) Expiry() time.Time {
if pyl.ExpireAt == nil {
return time.Time{}
}
return *pyl.ExpireAt.Load()
}
func (pyl *Payload[T]) Value() T {
return pyl.Payload
}
type Tuple[K comparable, T any] struct {
Key K
Value T
}
type Value[T any] struct {
V T
Found bool
}
type Cache[K comparable, T any] struct {
isDisabled bool
disableServeStale bool
store Store[K, T]
cacheAge time.Duration
deleteQ chan<- K
// following configurations are used only when an updater & threshold update are enabled
// threshold is the duration within which if the cache is about to expire, it is eligible to be updated
threshold time.Duration
updateQ chan<- K
updater Updater[K, T]
updaterTimeout time.Duration
// updateInProgress is used to handle update debounce
updateInProgress *sync.Map
errWatcher ErrOnUpdate
}
// initUpdater initializes all configuration required for threshold based update
func (ch *Cache[K, T]) initUpdater(cfg *Config[K, T]) {
if cfg.Updater == nil {
return
}
ch.threshold = cfg.Threshold.Abs()
updateQ := make(chan K, cfg.QLength)
ch.updateQ = updateQ
ch.updater = cfg.Updater
ch.updaterTimeout = cfg.UpdaterTimeout
ch.updateInProgress = new(sync.Map)
ch.errWatcher = cfg.ErrWatcher
go ch.updateListener(updateQ)
}
func (ch *Cache[K, T]) errCallback(err error) {
if err == nil || ch.errWatcher == nil {
return
}
ch.errWatcher(err)
}
func (ch *Cache[K, T]) enqueueUpdate(key K) {
if ch.updater == nil {
return
}
_, inprogress := ch.updateInProgress.Load(key)
if inprogress {
// key is already queued for update, no need to update again
return
}
ch.updateInProgress.Store(key, struct{}{})
ch.updateQ <- key
}
func (ch *Cache[K, T]) deleteListener(keys <-chan K) {
for key := range keys {
ch.store.Remove(key)
}
}
func (ch *Cache[K, T]) updateListener(keys <-chan K) {
for key := range keys {
ch.update(key)
}
}
func (ch *Cache[K, T]) update(key K) {
defer func() {
rec := recover()
if rec == nil {
return
}
ch.updateInProgress.Delete(key)
err, isErr := rec.(error)
if isErr {
ch.errCallback(errors.Join(ErrPanic, err))
return
}
ch.errCallback(errors.Join(ErrPanic, fmt.Errorf("%+v", rec)))
}()
ctx, cancel := context.WithTimeout(context.Background(), ch.updaterTimeout)
defer cancel()
value, err := ch.updater(ctx, key)
ch.updateInProgress.Delete(key)
if err != nil {
ch.errCallback(err)
return
}
ch.Add(key, value)
}
func (ch *Cache[K, T]) Get(key K) Value[T] {
var v Value[T]
if ch.isDisabled {
return v
}
cp, found := ch.store.Get(key)
if !found {
return v
}
expireAt := cp.ExpireAt.Load()
delta := time.Since(*expireAt)
if delta >= 0 && ch.disableServeStale {
// cache expired and should be removed
ch.deleteQ <- key
return v
}
inTreshold := delta < 0 && delta.Abs() <= ch.threshold
expired := delta >= 0
if inTreshold || expired {
// key is eligible for update
ch.enqueueUpdate(key)
}
v.Found = true
v.V = cp.Payload
return v
}
func (ch *Cache[K, T]) Add(key K, value T) (evicted bool) {
if ch.isDisabled {
return false
}
expireAt := time.Now().Add(ch.cacheAge)
cea := atomic.Pointer[time.Time]{}
cea.Store(&expireAt)
return ch.store.Add(key, &Payload[T]{
ExpireAt: &cea,
Payload: value,
})
}
func (ch *Cache[K, T]) BulkAdd(tuples []Tuple[K, T]) (evicted []bool) {
evicted = make([]bool, len(tuples))
for i, tuple := range tuples {
evicted[i] = ch.Add(tuple.Key, tuple.Value)
}
return evicted
}
func DefaultStore[K comparable, T any](lrusize int) (Store[K, T], error) {
lCache, err := lru.New[K, *Payload[T]](int(lrusize))
if err != nil {
return nil, fmt.Errorf("failed initializing LRU cache: %w", err)
}
return lCache, nil
}
func New[K comparable, T any](cfg Config[K, T]) (*Cache[K, T], error) {
err := cfg.SanitizeValidate()
if err != nil {
return nil, err
}
cstore := cfg.Store
if cstore == nil {
cstore, err = DefaultStore[K, T](int(cfg.LRUCacheSize))
if err != nil {
return nil, err
}
}
deleteQ := make(chan K, cfg.QLength)
ch := &Cache[K, T]{
isDisabled: cfg.DisableCache,
disableServeStale: !cfg.ServeStale,
store: cstore,
cacheAge: cfg.CacheAge.Abs(),
deleteQ: deleteQ,
}
ch.initUpdater(&cfg)
go ch.deleteListener(deleteQ)
return ch, nil
}