-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hot.go
802 lines (660 loc) · 21.3 KB
/
hot.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
package hot
import (
"runtime"
"sync"
"time"
"github.com/samber/go-singleflightx"
"github.com/samber/hot/internal"
"github.com/samber/hot/pkg/base"
"github.com/samber/hot/pkg/metrics"
)
// Revalidation is done in batch,.
var DebounceRevalidationFactor = 0.2
func newHotCache[K comparable, V any](
cache base.InMemoryCache[K, *item[V]],
missingSharedCache bool,
missingCache base.InMemoryCache[K, *item[V]],
ttl time.Duration,
stale time.Duration,
jitterLambda float64,
jitterUpperBound time.Duration,
loaderFns LoaderChain[K, V],
revalidationLoaderFns LoaderChain[K, V],
revalidationErrorPolicy revalidationErrorPolicy,
onEviction base.EvictionCallback[K, V],
copyOnRead func(V) V,
copyOnWrite func(V) V,
) *HotCache[K, V] {
return &HotCache[K, V]{
cache: cache,
missingSharedCache: missingSharedCache,
missingCache: missingCache,
// Better store int64 microseconds instead of time.Time (benchmark resulted in 10x speedup).
ttlMicro: ttl.Microseconds(),
staleMicro: stale.Microseconds(),
jitterLambda: jitterLambda,
jitterUpperBound: jitterUpperBound,
loaderFns: loaderFns,
revalidationLoaderFns: revalidationLoaderFns,
revalidationErrorPolicy: revalidationErrorPolicy,
onEviction: onEviction,
copyOnRead: copyOnRead,
copyOnWrite: copyOnWrite,
group: singleflightx.Group[K, V]{},
metrics: metrics.NewMetrics(ttl, jitterLambda, jitterUpperBound, stale),
}
}
type HotCache[K comparable, V any] struct {
ticker *time.Ticker
stopOnce *sync.Once
stopJanitor chan struct{}
cache base.InMemoryCache[K, *item[V]]
missingSharedCache bool
missingCache base.InMemoryCache[K, *item[V]]
// Better store int64 microseconds instead of time.Time (benchmark resulted in 10x speedup).
ttlMicro int64
staleMicro int64
jitterLambda float64
jitterUpperBound time.Duration
loaderFns LoaderChain[K, V]
revalidationLoaderFns LoaderChain[K, V]
revalidationErrorPolicy revalidationErrorPolicy
onEviction base.EvictionCallback[K, V]
copyOnRead func(V) V
copyOnWrite func(V) V
group singleflightx.Group[K, V]
metrics *metrics.Metrics
}
// Set adds a value to the cache. If the key already exists, its value is updated. It uses the default ttl or none.
func (c *HotCache[K, V]) Set(key K, v V) {
if c.copyOnWrite != nil {
v = c.copyOnWrite(v)
}
c.setUnsafe(key, true, v, c.ttlMicro)
}
// SetMissing adds a key to the `missing` cache. If the key already exists, its value is dropped. It uses the default ttl or none.
func (c *HotCache[K, V]) SetMissing(key K) {
if c.missingCache == nil && !c.missingSharedCache {
panic("missing cache is not enabled")
}
c.setUnsafe(key, false, zero[V](), c.ttlMicro)
}
// SetWithTTL adds a value to the cache. If the key already exists, its value is updated. It uses the given ttl.
func (c *HotCache[K, V]) SetWithTTL(key K, v V, ttl time.Duration) {
if c.copyOnWrite != nil {
v = c.copyOnWrite(v)
}
c.setUnsafe(key, true, v, ttl.Microseconds())
}
// SetMissingWithTTL adds a key to the `missing` cache. If the key already exists, its value is dropped. It uses the given ttl.
func (c *HotCache[K, V]) SetMissingWithTTL(key K, ttl time.Duration) {
if c.missingCache == nil && !c.missingSharedCache {
panic("missing cache is not enabled")
}
c.setUnsafe(key, false, zero[V](), ttl.Microseconds())
}
// SetMany adds many values to the cache. If the keys already exist, values are updated. It uses the default ttl or none.
func (c *HotCache[K, V]) SetMany(items map[K]V) {
if c.copyOnWrite != nil {
for k, v := range items {
items[k] = c.copyOnWrite(v)
}
}
c.setManyUnsafe(items, []K{}, c.ttlMicro)
}
// SetMissingMany adds many keys to the cache. If the keys already exist, values are dropped. It uses the default ttl or none.
func (c *HotCache[K, V]) SetMissingMany(missingKeys []K) {
if c.missingCache == nil && !c.missingSharedCache {
panic("missing cache is not enabled")
}
c.setManyUnsafe(map[K]V{}, missingKeys, c.ttlMicro)
}
// SetManyWithTTL adds many values to the cache. If the keys already exist, values are updated. It uses the given ttl.
func (c *HotCache[K, V]) SetManyWithTTL(items map[K]V, ttl time.Duration) {
if c.copyOnWrite != nil {
for k, v := range items {
items[k] = c.copyOnWrite(v)
}
}
c.setManyUnsafe(items, []K{}, ttl.Microseconds())
}
// SetManyWithTTL adds many keys to the cache. If the keys already exist, values are dropped. It uses the given ttl.
func (c *HotCache[K, V]) SetMissingManyWithTTL(missingKeys []K, ttl time.Duration) {
if c.missingCache == nil && !c.missingSharedCache {
panic("missing cache is not enabled")
}
c.setManyUnsafe(map[K]V{}, missingKeys, ttl.Microseconds())
}
// Has checks if a key exists in the cache.
// Missing values are not valid, even if cached.
func (c *HotCache[K, V]) Has(key K) bool {
v, ok := c.cache.Peek(key)
return ok && v.hasValue
}
// HasMany checks if keys exist in the cache.
// Missing values are not valid, even if cached.
func (c *HotCache[K, V]) HasMany(keys []K) map[K]bool {
values, missing := c.cache.PeekMany(keys)
output := make(map[K]bool, len(keys))
for k, v := range values {
output[k] = v.hasValue
}
for _, k := range missing {
output[k] = false
}
return output
}
// Get returns a value from the cache, a boolean indicating whether the key was found and an error when loaders fail.
func (c *HotCache[K, V]) Get(key K) (value V, found bool, err error) {
return c.GetWithLoaders(key, c.loaderFns...)
}
// MustGet returns a value from the cache, a boolean indicating whether the key was found. It panics when loaders fail.
func (c *HotCache[K, V]) MustGet(key K) (value V, found bool) {
value, found, err := c.Get(key)
if err != nil {
panic(err)
}
return value, found
}
// GetWithLoaders returns a value from the cache, a boolean indicating whether the key was found and an error when loaders fail.
func (c *HotCache[K, V]) GetWithLoaders(key K, loaders ...Loader[K, V]) (value V, found bool, err error) {
// the item might be found, but without value
cached, revalidate, found := c.getUnsafe(key)
if found {
if revalidate {
go c.revalidate(map[K]*item[V]{key: cached}, loaders)
}
if cached.hasValue && c.copyOnRead != nil {
return c.copyOnRead(cached.value), true, nil
}
return cached.value, cached.hasValue, nil
}
loaded, err := c.loadAndSetMany([]K{key}, loaders)
if err != nil {
return zero[V](), false, err
}
// `loaded` is expected to contain `key`, even if values was not available
item, ok := loaded[key]
if !ok || !item.hasValue {
return zero[V](), false, nil
}
if c.copyOnRead != nil {
return c.copyOnRead(item.value), true, nil
}
return item.value, true, nil
}
// MustGetWithLoaders returns a value from the cache, a boolean indicating whether the key was found. It panics when loaders fail.
func (c *HotCache[K, V]) MustGetWithLoaders(key K, loaders ...Loader[K, V]) (value V, found bool) {
value, found, err := c.GetWithLoaders(key, loaders...)
if err != nil {
panic(err)
}
return value, found
}
// GetMany returns many values from the cache, a slice of missing keys and an error when loaders fail.
func (c *HotCache[K, V]) GetMany(keys []K) (values map[K]V, missing []K, err error) {
return c.GetManyWithLoaders(keys, c.loaderFns...)
}
// MustGetMany returns many values from the cache, a slice of missing keys. It panics when loaders fail.
func (c *HotCache[K, V]) MustGetMany(keys []K) (values map[K]V, missing []K) {
values, missing, err := c.GetMany(keys)
if err != nil {
panic(err)
}
return values, missing
}
// GetManyWithLoaders returns many values from the cache, a slice of missing keys and an error when loaders fail.
func (c *HotCache[K, V]) GetManyWithLoaders(keys []K, loaders ...Loader[K, V]) (values map[K]V, missing []K, err error) {
// Some items might be found in cached, but without value.
// Other items will be returned in `missing`.
cached, missing, revalidate := c.getManyUnsafe(keys)
loaded, err := c.loadAndSetMany(missing, loaders)
if err != nil {
return nil, nil, err
}
if len(revalidate) > 0 {
go c.revalidate(revalidate, loaders)
}
found, missing := itemMapsToValues(c.copyOnRead, cached, loaded)
return found, missing, nil
}
// MustGetManyWithLoaders returns many values from the cache, a slice of missing keys. It panics when loaders fail.
func (c *HotCache[K, V]) MustGetManyWithLoaders(keys []K, loaders ...Loader[K, V]) (values map[K]V, missing []K) {
values, missing, err := c.GetManyWithLoaders(keys, loaders...)
if err != nil {
panic(err)
}
return values, missing
}
// Peek is similar to Get, but do not check expiration and do not call loaders/revalidation.
// Missing values are not returned, even if cached.
func (c *HotCache[K, V]) Peek(key K) (value V, ok bool) {
// no need to check missingCache, since it will be missing anyway
item, ok := c.cache.Peek(key)
if ok && item.hasValue {
if c.copyOnRead != nil {
return c.copyOnRead(item.value), true
}
return item.value, true
}
return zero[V](), false
}
// PeekMany is similar to GetMany, but do not check expiration and do not call loaders/revalidation.
// Missing values are not returned, even if cached.
func (c *HotCache[K, V]) PeekMany(keys []K) (map[K]V, []K) {
cached := make(map[K]V)
missing := []K{}
// no need to check missingCache, since it will be missing anyway
items, _ := c.cache.PeekMany(keys)
for _, key := range keys {
if item, ok := items[key]; ok && item.hasValue {
if c.copyOnRead != nil {
cached[key] = c.copyOnRead(item.value)
} else {
cached[key] = item.value
}
} else {
missing = append(missing, key)
}
}
return cached, missing
}
// Keys returns all keys in the cache.
// Missing keys are not included.
func (c *HotCache[K, V]) Keys() []K {
output := []K{}
c.cache.Range(func(k K, v *item[V]) bool {
if v.hasValue { // equalivant to testing `missingSharedCache`
output = append(output, k)
}
return true
})
return output
}
// Values returns all values in the cache.
// Missing values are not included.
func (c *HotCache[K, V]) Values() []V {
values := c.cache.Values()
output := []V{}
for _, v := range values {
if v.hasValue {
if c.copyOnRead != nil {
output = append(output, c.copyOnRead(v.value))
} else {
output = append(output, v.value)
}
}
}
return output
}
// Range calls a function for each key/value pair in the cache.
// The callback should be kept short because it is called while holding a read lock.
// @TODO: loop over missingCache? Use a different callback?
// Missing values are not included.
func (c *HotCache[K, V]) Range(f func(K, V) bool) {
c.cache.Range(func(k K, v *item[V]) bool {
if !v.hasValue { // equalivant to testing `missingSharedCache`
return true
}
if c.copyOnRead != nil {
return f(k, v.value)
}
return f(k, v.value)
})
}
// Delete removes a key from the cache.
func (c *HotCache[K, V]) Delete(key K) bool {
return c.cache.Delete(key) || (c.missingCache != nil && c.missingCache.Delete(key))
}
// DeleteMany removes many keys from the cache.
func (c *HotCache[K, V]) DeleteMany(keys []K) map[K]bool {
// @TODO: should be done in a single call to avoid multiple locks
a := c.cache.DeleteMany(keys)
b := map[K]bool{}
if c.missingCache != nil {
b = c.missingCache.DeleteMany(keys)
}
output := map[K]bool{}
for _, key := range keys {
output[key] = a[key] || b[key]
}
return output
}
// Purge removes all items from the cache.
func (c *HotCache[K, V]) Purge() {
c.cache.Purge()
if c.missingCache != nil {
// @TODO: should be done in a single call to avoid multiple locks
c.missingCache.Purge()
}
}
// Capacity returns the cache capacity.
func (c *HotCache[K, V]) Capacity() (int, int) {
if c.missingCache != nil {
// @TODO: should be done in a single call to avoid multiple locks
return c.cache.Capacity(), c.missingCache.Capacity()
}
return c.cache.Capacity(), 0
}
// Algorithm returns the cache algo.
func (c *HotCache[K, V]) Algorithm() (string, string) {
if c.missingCache != nil {
// @TODO: should be done in a single call to avoid multiple locks
return c.cache.Algorithm(), c.missingCache.Algorithm()
}
return c.cache.Algorithm(), ""
}
// Len returns the number of items in the cache.
// Missing items are included.
func (c *HotCache[K, V]) Len() int {
if c.missingCache != nil {
// @TODO: should be done in a single call to avoid multiple locks
return c.cache.Len() + c.missingCache.Len()
}
return c.cache.Len()
}
// WarmUp loads all keys from the loader and sets them in the cache.
func (c *HotCache[K, V]) WarmUp(loader func() (map[K]V, []K, error)) error {
items, missing, err := loader()
if err != nil {
return err
}
if c.copyOnWrite != nil {
for k, v := range items {
items[k] = c.copyOnWrite(v)
}
}
if c.missingCache == nil && !c.missingSharedCache && len(missing) > 0 {
panic("missing cache is not enabled")
}
c.setManyUnsafe(items, missing, c.ttlMicro)
return nil
}
// Janitor runs a background goroutine to clean up the cache.
func (c *HotCache[K, V]) Janitor() {
c.ticker = time.NewTicker(time.Duration(c.ttlMicro) * time.Microsecond)
c.stopOnce = &sync.Once{}
c.stopJanitor = make(chan struct{})
runtime.SetFinalizer(c, func(f *HotCache[K, V]) {
c.StopJanitor()
})
go func() {
for {
select {
case <-c.stopJanitor:
c.stopJanitor <- struct{}{}
return
case <-c.ticker.C:
nowMicro := internal.NowMicro()
{
toDelete := []K{}
toDeleteKV := map[K]V{}
c.cache.Range(func(k K, v *item[V]) bool {
if v.isExpired(nowMicro) {
toDelete = append(toDelete, k)
if c.onEviction != nil {
toDeleteKV[k] = v.value
}
}
return true
})
deleted := c.cache.DeleteMany(toDelete)
if c.onEviction != nil {
for k, ok := range deleted {
if ok {
c.onEviction(k, toDeleteKV[k])
}
}
}
}
if c.missingCache != nil {
toDelete := []K{}
toDeleteKV := map[K]V{}
c.missingCache.Range(func(k K, v *item[V]) bool {
if v.isExpired(nowMicro) {
toDelete = append(toDelete, k)
if c.onEviction != nil {
toDeleteKV[k] = v.value
}
}
return true
})
deleted := c.missingCache.DeleteMany(toDelete)
if c.onEviction != nil {
for k, ok := range deleted {
if ok {
c.onEviction(k, toDeleteKV[k])
}
}
}
}
}
}
}()
}
func (c *HotCache[K, V]) StopJanitor() {
runtime.SetFinalizer(c, nil)
c.stopOnce.Do(func() {
c.stopJanitor <- struct{}{}
c.ticker.Stop()
<-c.stopJanitor
close(c.stopJanitor)
})
}
func (c *HotCache[K, V]) setUnsafe(key K, hasValue bool, value V, ttlMicro int64) {
if !hasValue && c.missingCache == nil && !c.missingSharedCache {
return
}
ttlMicro = applyJitter(ttlMicro, c.jitterLambda, c.jitterUpperBound)
// since we don't know where is the previous key, we need to delete prempetively
if c.missingCache != nil {
// @TODO: should be done in a single call to avoid multiple locks
if hasValue {
c.missingCache.Delete(key)
} else {
c.cache.Delete(key)
}
}
// @TODO: should be done in a single call to avoid multiple locks
if hasValue || c.missingSharedCache {
c.cache.Set(key, newItem(value, hasValue, ttlMicro, c.staleMicro))
} else if c.missingCache != nil {
c.missingCache.Set(key, newItemNoValue[V](ttlMicro, c.staleMicro))
}
}
func (c *HotCache[K, V]) setManyUnsafe(items map[K]V, missing []K, ttlMicro int64) {
if c.missingCache == nil && !c.missingSharedCache {
missing = []K{}
}
if c.missingCache != nil {
keysHavingValues := make([]K, 0, len(items))
for k := range items {
keysHavingValues = append(keysHavingValues, k)
}
// since we don't know where is the previous key, we need to delete all of them
// @TODO: should be done in a single call to avoid multiple locks
c.cache.DeleteMany(missing)
c.missingCache.DeleteMany(keysHavingValues)
}
values := map[K]*item[V]{}
for k, v := range items {
values[k] = newItemWithValue(v, ttlMicro, c.staleMicro)
}
if c.missingSharedCache {
for _, k := range missing {
values[k] = newItemNoValue[V](ttlMicro, c.staleMicro)
}
}
c.cache.SetMany(values)
if c.missingCache != nil {
values = map[K]*item[V]{}
for _, k := range missing {
values[k] = newItemNoValue[V](ttlMicro, c.staleMicro)
}
c.missingCache.SetMany(values)
}
}
// getUnsafe returns true if the key was fonud, even if it has no value.
func (c *HotCache[K, V]) getUnsafe(key K) (value *item[V], revalidate bool, found bool) {
nowMicro := internal.NowMicro()
// @TODO: should be done in a single call to avoid multiple locks
if item, ok := c.cache.Get(key); ok {
if !item.isExpired(nowMicro) {
return item, item.shouldRevalidate(nowMicro), true
}
ok := c.cache.Delete(key)
if ok && c.onEviction != nil {
c.onEviction(key, item.value)
}
}
if c.missingCache != nil {
// @TODO: should be done in a single call to avoid multiple locks
if item, ok := c.missingCache.Get(key); ok {
if !item.isExpired(nowMicro) {
return item, item.shouldRevalidate(nowMicro), true
}
ok := c.missingCache.Delete(key)
if ok && c.onEviction != nil {
c.onEviction(key, item.value)
}
}
}
return nil, false, false
}
func (c *HotCache[K, V]) getManyUnsafe(keys []K) (cached map[K]*item[V], missing []K, revalidate map[K]*item[V]) {
nowMicro := internal.NowMicro()
cached = make(map[K]*item[V])
revalidate = make(map[K]*item[V])
toDeleteCache := []K{}
toDeleteMissingCache := []K{}
onEvictKV := map[K]V{}
tmp, missing := c.cache.GetMany(keys)
for k, v := range tmp {
if !v.isExpired(nowMicro) {
cached[k] = v
if v.shouldRevalidate(nowMicro) {
revalidate[k] = v
}
continue
}
toDeleteCache = append(toDeleteCache, k)
if c.onEviction != nil {
onEvictKV[k] = v.value
}
}
if len(toDeleteCache) > 0 {
// @TODO: should be done in a single call to avoid multiple locks
deleted := c.cache.DeleteMany(toDeleteCache)
if c.onEviction != nil {
for k, ok := range deleted {
if ok {
c.onEviction(k, onEvictKV[k])
}
}
}
missing = append(missing, toDeleteCache...)
}
if len(missing) > 0 && c.missingCache != nil {
tmp, missing = c.missingCache.GetMany(missing)
for k, v := range tmp {
if !v.isExpired(nowMicro) {
cached[k] = v
if v.shouldRevalidate(nowMicro) {
revalidate[k] = v
}
continue
}
toDeleteMissingCache = append(toDeleteMissingCache, k)
if c.onEviction != nil {
onEvictKV[k] = v.value
}
}
if len(toDeleteMissingCache) > 0 {
// @TODO: should be done in a single call to avoid multiple locks
deleted := c.missingCache.DeleteMany(toDeleteMissingCache)
if c.onEviction != nil {
for k, ok := range deleted {
if ok {
c.onEviction(k, onEvictKV[k])
}
}
}
missing = append(missing, toDeleteMissingCache...)
}
}
return cached, missing, revalidate
}
// loadAndSetMany loads the keys and sets them in the cache.
// It returns a map of keys to items and an error when loaders fail.
// All requested keys are returned, even if they have no value.
func (c *HotCache[K, V]) loadAndSetMany(keys []K, loaders LoaderChain[K, V]) (map[K]*item[V], error) {
if len(keys) == 0 || len(loaders) == 0 {
result := map[K]*item[V]{}
for _, key := range keys {
result[key] = newItemNoValue[V](0, 0)
}
return result, nil
}
// go-singleflightx is used to avoid calling the loaders multiple times for concurrent loads.
// go-singleflightx returns all keys, so we don't need to keep track of missing keys.
// Instead of looping over every loaders, we should return the valid keys as soon as possible (and it will reduce errors).
// @TODO: a custom implem of go-singleflightx could be used to avoid some loops.
results := c.group.DoX(keys, func(missing []K) (map[K]V, error) {
results, stillMissing, err := loaders.run(missing)
if err != nil {
return nil, err
}
// Checking if a results is not empty before call to setManyUnsafe ensure we don't call mutex for nothing.
if c.copyOnWrite != nil && len(results) > 0 {
for k, v := range results {
results[k] = c.copyOnWrite(v)
}
}
// We keep track of missing keys to avoid calling the loaders again.
// Any values in `results` that were not requested in `keys` are cached
c.setManyUnsafe(results, stillMissing, c.ttlMicro)
return results, nil
})
// format output
output := map[K]*item[V]{}
for _, key := range keys {
if v, ok := results[key]; ok {
if v.Err != nil {
return map[K]*item[V]{}, v.Err
}
output[key] = newItem(v.Value.Value, v.Value.Valid, 0, 0)
} else {
// not expected, since go-singleflightx should return all keys
output[key] = newItemNoValue[V](0, 0)
}
}
return output, nil
}
func (c *HotCache[K, V]) revalidate(items map[K]*item[V], fallbackLoaders LoaderChain[K, V]) {
if len(items) == 0 {
return
}
keys := []K{}
for k := range items {
keys = append(keys, k)
}
loaders := fallbackLoaders
if len(c.revalidationLoaderFns) > 0 {
loaders = c.revalidationLoaderFns
}
// @TODO: we might be fetching keys one by one, which is not efficient.
// We should batch the keys and fetch them after a short delay.
_, err := c.loadAndSetMany(keys, loaders)
if err != nil && c.revalidationErrorPolicy == KeepOnError {
valid := map[K]V{}
missing := []K{}
for k, v := range items {
if v.hasValue {
valid[k] = v.value
} else {
missing = append(missing, k)
}
}
c.setManyUnsafe(valid, missing, c.ttlMicro)
}
}