-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimcache.go
1234 lines (1177 loc) · 34.2 KB
/
imcache.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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package imcache provides a generic in-memory cache.
// It supports absolute expiration, sliding expiration, max entries limit,
// eviction callbacks and sharding.
// It's safe for concurrent use by multiple goroutines.
//
// The New function creates a new in-memory non-sharded cache instance.
//
// The NewSharded function creates a new in-memory sharded cache instance.
package imcache
import (
"sync"
"time"
)
// nowf is a func returning current time. It simply calls time.Now()
// under the hood. It should be changed only for the testing purposes.
//
// This global variable is not ideal but it simplifies a lot, making
// zero value Cache simple. No need to init a field level clock, that
// is important for read-only (Peek*) methods.
var nowf = func() time.Time {
return time.Now()
}
// New returns a new Cache instance.
//
// By default, a returned Cache has no default expiration, no default sliding
// expiration, no entry limit and no eviction callback.
//
// Option(s) can be used to customize the returned Cache.
func New[K comparable, V any](opts ...Option[K, V]) *Cache[K, V] {
o := options[K, V]{defaultExp: noExp}
for _, opt := range opts {
opt.apply(&o)
}
return newCache(o)
}
func newCache[K comparable, V any](opts options[K, V]) *Cache[K, V] {
var limit int
if opts.maxEntriesLimit > 0 {
limit = opts.maxEntriesLimit
}
c := &Cache[K, V]{
m: make(map[K]node[K, V], limit),
onEviction: opts.onEviction,
defaultExp: opts.defaultExp,
sliding: opts.sliding,
limit: limit,
policy: opts.evictionPolicy,
queue: newEvictionQueue[K, V](opts.maxEntriesLimit, opts.evictionPolicy),
}
if opts.cleanerInterval > 0 {
c.cleaner = newCleaner()
if err := c.cleaner.start(c, opts.cleanerInterval); err != nil {
// With current sanitization this should never happen.
panic(err)
}
}
return c
}
// Cache is a non-sharded in-memory cache.
//
// By default, it has no default expiration, no default sliding expiration
// no entry limit and no eviction callback.
//
// The zero value Cache is ready to use.
//
// If you want to configure a Cache, use the New function
// and provide proper Option(s).
//
// Example:
//
// c := imcache.New[string, interface{}](
// imcache.WithDefaultExpirationOption[string, interface{}](time.Second),
// imcache.WithCleanerOption[string, interface{}](5*time.Minute),
// imcache.WithMaxEntriesOption[string, interface{}](10000),
// imcache.WithEvictionCallbackOption[string, interface{}](LogEvictedEntry),
// )
type Cache[K comparable, V any] struct {
queue evictionQueue[K, V]
m map[K]node[K, V]
onEviction EvictionCallback[K, V]
cleaner *cleaner
defaultExp time.Duration
policy EvictionPolicy
limit int
mu sync.RWMutex
sliding bool
closed bool
}
// init initializes the Cache.
// It is not a concurrency-safe method.
func (c *Cache[K, V]) init() {
if c.m == nil {
c.m = make(map[K]node[K, V])
c.defaultExp = noExp
c.queue = &nopEvictionQueue[K, V]{}
}
}
// Get returns the value for the given key.
//
// If it encounters an expired entry, the expired entry is evicted.
func (c *Cache[K, V]) Get(key K) (V, bool) {
now := nowf()
var zero V
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return zero, false
}
node, ok := c.m[key]
if !ok {
return zero, false
}
entry := node.entry()
if entry.expired(now) {
c.queue.remove(node)
delete(c.m, key)
if c.onEviction != nil {
go c.onEviction(key, entry.val, EvictionReasonExpired)
}
return zero, false
}
entry.slide(now)
node.setEntry(entry)
c.queue.touch(node)
return entry.val, true
}
// GetMultiple returns the values for the given keys.
// If the Cache is not closed, then the returned map
// is always a non-nil one.
//
// If it encounters an expired entry, the expired entry is evicted.
func (c *Cache[K, V]) GetMultiple(keys ...K) map[K]V {
return c.getMultiple(nowf(), keys...)
}
func (c *Cache[K, V]) getMultiple(now time.Time, keys ...K) map[K]V {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return nil
}
got := make(map[K]V, len(keys))
// To avoid copying the expired entries if there's no eviction callback.
if c.onEviction == nil {
for _, key := range keys {
node, ok := c.m[key]
if !ok {
continue
}
entry := node.entry()
if entry.expired(now) {
c.queue.remove(node)
delete(c.m, key)
continue
}
entry.slide(now)
node.setEntry(entry)
c.queue.touch(node)
got[key] = entry.val
}
return got
}
var expired []entry[K, V]
for _, key := range keys {
node, ok := c.m[key]
if !ok {
continue
}
entry := node.entry()
if entry.expired(now) {
expired = append(expired, entry)
c.queue.remove(node)
delete(c.m, key)
continue
}
entry.slide(now)
node.setEntry(entry)
c.queue.touch(node)
got[key] = entry.val
}
go func() {
for _, entry := range expired {
c.onEviction(entry.key, entry.val, EvictionReasonExpired)
}
}()
return got
}
// GetAll returns a copy of all entries in the cache.
//
// If it encounters an expired entry, the expired entry is evicted.
func (c *Cache[K, V]) GetAll() map[K]V {
return c.getAll(nowf())
}
func (c *Cache[K, V]) getAll(now time.Time) map[K]V {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return nil
}
// To avoid copying the expired entries if there's no eviction callback.
if c.onEviction == nil {
got := make(map[K]V, len(c.m))
for key, node := range c.m {
entry := node.entry()
if entry.expired(now) {
c.queue.remove(node)
delete(c.m, key)
continue
}
entry.slide(now)
node.setEntry(entry)
got[key] = entry.val
}
c.queue.touchall()
return got
}
var expired []entry[K, V]
got := make(map[K]V, len(c.m))
for key, node := range c.m {
entry := node.entry()
if entry.expired(now) {
expired = append(expired, entry)
delete(c.m, key)
c.queue.remove(node)
continue
}
entry.slide(now)
node.setEntry(entry)
got[key] = entry.val
}
c.queue.touchall()
if len(expired) != 0 {
go func() {
for _, kv := range expired {
c.onEviction(kv.key, kv.val, EvictionReasonExpired)
}
}()
}
return got
}
// Peek returns the value for the given key without
// actively evicting the entry if it is expired and
// updating the entry's sliding expiration.
//
// If the max entries limit is set, it doesn't update
// the entry's position in the eviction queue.
func (c *Cache[K, V]) Peek(key K) (V, bool) {
now := nowf()
var zero V
c.mu.RLock()
defer c.mu.RUnlock()
if c.closed {
return zero, false
}
n, ok := c.m[key]
if !ok || n.entry().expired(now) {
return zero, false
}
return n.entry().val, true
}
// PeekMultiple returns the values for the given keys without
// actively evicting the encountered entry if it is expired and
// updating the entry's sliding expiration.
// If the Cache is not closed, then the returned map
// is always a non-nil one.
//
// If the max entries limit is set, it doesn't update
// the encountered entry's position in the eviction queue.
func (c *Cache[K, V]) PeekMultiple(keys ...K) map[K]V {
return c.peekMultiple(nowf(), keys...)
}
func (c *Cache[K, V]) peekMultiple(now time.Time, keys ...K) map[K]V {
c.mu.RLock()
defer c.mu.RUnlock()
if c.closed {
return nil
}
got := make(map[K]V, len(keys))
for _, key := range keys {
node, ok := c.m[key]
if !ok || node.entry().expired(now) {
continue
}
got[key] = node.entry().val
}
return got
}
// PeekAll returns a copy of all entries in the cache without
// actively evicting the encountered entry if it is expired and
// updating the entry's sliding expiration.
//
// If the max entries limit is set, it doesn't update
// the encountered entry's position in the eviction queue.
func (c *Cache[K, V]) PeekAll() map[K]V {
return c.peekAll(nowf())
}
func (c *Cache[K, V]) peekAll(now time.Time) map[K]V {
c.mu.RLock()
defer c.mu.RUnlock()
if c.closed {
return nil
}
got := make(map[K]V, len(c.m))
for key, node := range c.m {
if node.entry().expired(now) {
continue
}
got[key] = node.entry().val
}
return got
}
// Set sets the value for the given key.
// If the entry already exists, it is replaced.
//
// If it encounters an expired entry, it is evicted and a new entry is added.
//
// If you don't want to replace an existing entry, use the GetOrSet method
// instead. If you don't want to add a new entry if it doesn't exist, use
// the Replace method instead.
func (c *Cache[K, V]) Set(key K, val V, exp Expiration) {
now := nowf()
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return
}
// Make sure that the shard is initialized.
c.init()
currentNode, ok := c.m[key]
newEntry := entry[K, V]{key: key, val: val, exp: exp.new(now, c.defaultExp, c.sliding)}
if ok {
currentEntry := currentNode.entry()
if !currentEntry.expired(now) {
currentNode.setEntry(newEntry)
c.queue.touch(currentNode)
if c.onEviction != nil {
go c.onEviction(key, currentEntry.val, EvictionReasonReplaced)
}
return
}
c.m[key] = c.queue.add(newEntry)
c.queue.remove(currentNode)
if c.onEviction != nil {
go c.onEviction(key, currentEntry.val, EvictionReasonExpired)
}
return
}
var evictedNode node[K, V]
if c.limit > 0 && c.len() == c.limit {
evictedNode = c.queue.pop()
delete(c.m, evictedNode.entry().key)
}
c.m[key] = c.queue.add(entry[K, V]{key: key, val: val, exp: exp.new(now, c.defaultExp, c.sliding)})
if c.onEviction == nil || evictedNode == nil {
return
}
evictedEntry := evictedNode.entry()
if evictedEntry.expired(now) {
go c.onEviction(evictedEntry.key, evictedEntry.val, EvictionReasonExpired)
} else {
go c.onEviction(evictedEntry.key, evictedEntry.val, EvictionReasonMaxEntriesExceeded)
}
}
// GetOrSet returns the value for the given key and true if it exists,
// otherwise it sets the value for the given key and returns the set value
// and false.
//
// If it encounters an expired entry, the expired entry is evicted.
func (c *Cache[K, V]) GetOrSet(key K, val V, exp Expiration) (value V, present bool) {
now := nowf()
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
var zero V
return zero, false
}
// Make sure that the shard is initialized.
c.init()
currentNode, ok := c.m[key]
if ok && !currentNode.entry().expired(now) {
currentEntry := currentNode.entry()
currentEntry.slide(now)
currentNode.setEntry(currentEntry)
c.queue.touch(currentNode)
return currentEntry.val, true
}
if ok {
c.m[key] = c.queue.add(entry[K, V]{key: key, val: val, exp: exp.new(now, c.defaultExp, c.sliding)})
c.queue.remove(currentNode)
if c.onEviction != nil {
go c.onEviction(key, currentNode.entry().val, EvictionReasonExpired)
}
return val, false
}
var evictedNode node[K, V]
if c.limit > 0 && c.len() == c.limit {
evictedNode = c.queue.pop()
delete(c.m, evictedNode.entry().key)
}
c.m[key] = c.queue.add(entry[K, V]{key: key, val: val, exp: exp.new(now, c.defaultExp, c.sliding)})
if c.onEviction == nil || evictedNode == nil {
return val, false
}
var evictionReason EvictionReason
evictedEntry := evictedNode.entry()
if evictedEntry.expired(now) {
evictionReason = EvictionReasonExpired
} else {
evictionReason = EvictionReasonMaxEntriesExceeded
}
go c.onEviction(evictedEntry.key, evictedEntry.val, evictionReason)
return val, false
}
// Replace replaces the value for the given key.
// It returns true if the value is present and replaced, otherwise it returns
// false.
//
// If it encounters an expired entry, the expired entry is evicted.
//
// If you want to add or replace an entry, use the Set method instead.
func (c *Cache[K, V]) Replace(key K, val V, exp Expiration) (present bool) {
now := nowf()
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return false
}
currentNode, ok := c.m[key]
if !ok {
return false
}
currentEntry := currentNode.entry()
if currentEntry.expired(now) {
c.queue.remove(currentNode)
delete(c.m, key)
if c.onEviction != nil {
go c.onEviction(key, currentEntry.val, EvictionReasonExpired)
}
return false
}
currentNode.setEntry(entry[K, V]{key: key, val: val, exp: exp.new(now, c.defaultExp, c.sliding)})
c.queue.touch(currentNode)
if c.onEviction != nil {
go c.onEviction(key, currentEntry.val, EvictionReasonReplaced)
}
return true
}
// Number is a constraint that permits any numeric type except complex ones.
//
// Deprecated: Number constraint is deprecated. It is easy to write your own
// constraint. imcache's goal is to be simple. Creating artificial types
// or functions that are not even needed conflicts with this goal.
type Number interface {
~float32 | ~float64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~int | ~int8 | ~int16 | ~int32 | ~int64
}
// Increment increments the given number by one.
//
// Deprecated: Increment function is deprecated. It is easy to write your own
// function. imcache's goal is to be simple. Creating artificial types
// or functions that are not even needed conflicts with this goal.
func Increment[V Number](old V) V {
return old + 1
}
// Decrement decrements the given number by one.
//
// Deprecated: Decrement function is deprecated. It is easy to write your own
// function. imcache's goal is to be simple. Creating artificial types
// or functions that are not even needed conflicts with this goal.
func Decrement[V Number](old V) V {
return old - 1
}
// ReplaceWithFunc replaces the value for the given key with the result
// of the given function that takes the current value as an argument.
// It returns true if the value is present and replaced, otherwise it returns
// false.
//
// If it encounters an expired entry, the expired entry is evicted.
//
// If you want to replace the value with a new value not depending on
// the current value, use the Replace method instead.
//
// Example showing how to increment the value by 1 using ReplaceWithFunc:
//
// var c imcache.Cache[string, int32]
// c.Set("foo", 997, imcache.WithNoExpiration())
// _ = c.ReplaceWithFunc(
// "foo",
// func(current int32) int32 { return current + 1 },
// imcache.WithNoExpiration(),
// )
func (c *Cache[K, V]) ReplaceWithFunc(key K, f func(current V) (new V), exp Expiration) (present bool) {
now := nowf()
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return false
}
currentNode, ok := c.m[key]
if !ok {
return false
}
currentEntry := currentNode.entry()
if currentEntry.expired(now) {
c.queue.remove(currentNode)
delete(c.m, key)
if c.onEviction != nil {
go c.onEviction(key, currentEntry.val, EvictionReasonExpired)
}
return false
}
currentNode.setEntry(entry[K, V]{key: key, val: f(currentEntry.val), exp: exp.new(now, c.defaultExp, c.sliding)})
c.queue.touch(currentNode)
if c.onEviction != nil {
go c.onEviction(key, currentEntry.val, EvictionReasonReplaced)
}
return true
}
// ReplaceKey replaces the given old key with the new key.
// The value remains the same. It returns true if the key is present
// and replaced, otherwise it returns false. If there is an existing
// entry for the new key, it is replaced.
//
// If it encounters an expired entry, the expired entry is evicted.
func (c *Cache[K, V]) ReplaceKey(old, new K, exp Expiration) (present bool) {
now := nowf()
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return false
}
oldNode, ok := c.m[old]
if !ok {
return false
}
oldEntry := oldNode.entry()
delete(c.m, old)
c.queue.remove(oldNode)
if oldEntry.expired(now) {
if c.onEviction != nil {
go c.onEviction(old, oldEntry.val, EvictionReasonExpired)
}
return false
}
newEntry := entry[K, V]{key: new, val: oldEntry.val, exp: exp.new(now, c.defaultExp, c.sliding)}
currentNode, ok := c.m[new]
if !ok {
c.m[new] = c.queue.add(newEntry)
if c.onEviction != nil {
go c.onEviction(old, oldEntry.val, EvictionReasonKeyReplaced)
}
return true
}
currentEntry := currentNode.entry()
currentNode.setEntry(newEntry)
c.queue.touch(currentNode)
if c.onEviction != nil {
go func() {
c.onEviction(old, oldEntry.val, EvictionReasonKeyReplaced)
if currentEntry.expired(now) {
c.onEviction(new, currentEntry.val, EvictionReasonExpired)
} else {
c.onEviction(new, currentEntry.val, EvictionReasonReplaced)
}
}()
}
return true
}
// CompareAndSwap replaces the value for the given key if the current value
// is equal to the expected value.
//
// Equality is defined by the given compare function.
//
// If it encounters an expired entry, the expired entry is evicted.
func (c *Cache[K, V]) CompareAndSwap(key K, expected, new V, compare func(V, V) bool, exp Expiration) (swapped, present bool) {
now := nowf()
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return false, false
}
currentNode, ok := c.m[key]
if !ok {
return false, false
}
currentEntry := currentNode.entry()
if currentEntry.expired(now) {
c.queue.remove(currentNode)
delete(c.m, key)
if c.onEviction != nil {
go c.onEviction(key, currentEntry.val, EvictionReasonExpired)
}
return false, false
}
if !compare(currentEntry.val, expected) {
c.queue.touch(currentNode)
return false, true
}
currentNode.setEntry(entry[K, V]{key: key, val: new, exp: exp.new(now, c.defaultExp, c.sliding)})
c.queue.touch(currentNode)
if c.onEviction != nil {
go c.onEviction(key, currentEntry.val, EvictionReasonReplaced)
}
return true, true
}
// Remove removes the cache entry for the given key.
//
// It returns true if the entry is present and removed,
// otherwise it returns false.
//
// If it encounters an expired entry, the expired entry is evicted.
// It results in calling the eviction callback with EvictionReasonExpired,
// not EvictionReasonRemoved. If entry is expired, it returns false.
func (c *Cache[K, V]) Remove(key K) (present bool) {
now := nowf()
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return false
}
currentNode, ok := c.m[key]
if !ok {
return false
}
c.queue.remove(currentNode)
delete(c.m, key)
currentEntry := currentNode.entry()
if c.onEviction == nil {
return !currentEntry.expired(now)
}
if currentEntry.expired(now) {
go c.onEviction(key, currentEntry.val, EvictionReasonExpired)
return false
}
go c.onEviction(key, currentEntry.val, EvictionReasonRemoved)
return true
}
// RemoveAll removes all entries.
//
// If an eviction callback is set, it is called for each removed entry.
//
// If it encounters an expired entry, the expired entry is evicted.
// It results in calling the eviction callback with EvictionReasonExpired,
// not EvictionReasonRemoved.
func (c *Cache[K, V]) RemoveAll() {
c.removeAll(nowf())
}
func (c *Cache[K, V]) removeAll(now time.Time) {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return
}
removed := c.m
c.m = make(map[K]node[K, V], c.limit)
c.queue = newEvictionQueue[K, V](c.limit, c.policy)
if c.onEviction != nil && len(removed) != 0 {
go func() {
for key, node := range removed {
entry := node.entry()
if entry.expired(now) {
c.onEviction(key, entry.val, EvictionReasonExpired)
} else {
c.onEviction(key, entry.val, EvictionReasonRemoved)
}
}
}()
}
}
// RemoveExpired removes all expired entries.
//
// If an eviction callback is set, it is called for each removed entry.
func (c *Cache[K, V]) RemoveExpired() {
c.removeExpired(nowf())
}
func (c *Cache[K, V]) removeExpired(now time.Time) {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return
}
// To avoid copying the expired entries if there's no eviction callback.
if c.onEviction == nil {
for key, node := range c.m {
entry := node.entry()
if entry.expired(now) {
c.queue.remove(node)
delete(c.m, key)
}
}
return
}
var removed []entry[K, V]
for key, node := range c.m {
entry := node.entry()
if entry.expired(now) {
removed = append(removed, entry)
delete(c.m, key)
c.queue.remove(node)
}
}
if len(removed) != 0 {
go func() {
for _, entry := range removed {
c.onEviction(entry.key, entry.val, EvictionReasonExpired)
}
}()
}
}
// Len returns the number of entries in the cache.
func (c *Cache[K, V]) Len() int {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return 0
}
n := c.len()
return n
}
// len returns the number of entries in the cache.
// It is not a concurrency-safe method.
func (c *Cache[K, V]) len() int {
return len(c.m)
}
// Close closes the cache. It purges all entries and stops the cleaner
// if it is running. After Close, all other methods are NOP returning
// zero values immediately.
//
// It is safe to call Close multiple times.
//
// It's not necessary to call Close if the cache is no longer referenced
// and there is no cleaner running. Garbage collector will collect the cache.
func (c *Cache[K, V]) Close() {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return
}
c.m = nil
c.closed = true
// If the cleaner is running, stop it.
// It's safe to access c.cleaner without a lock because
// it's only set during initialization and never modified.
if c.cleaner != nil {
c.cleaner.stop()
}
}
// NewSharded returns a new Sharded instance.
// It panics if n is not greater than 0 or hasher is nil.
//
// By default, a returned Sharded has no default expiration,
// no default sliding expiration, no entry limit and
// no eviction callback.
//
// Option(s) can be used to customize the returned Sharded.
// Note that Option(s) are applied to each shard (Cache instance)
// not to the Sharded instance itself.
func NewSharded[K comparable, V any](n int, hasher Hasher64[K], opts ...Option[K, V]) *Sharded[K, V] {
if n <= 0 {
panic("imcache: number of shards must be greater than 0")
}
if hasher == nil {
panic("imcache: hasher must be not nil")
}
o := options[K, V]{defaultExp: noExp}
for _, opt := range opts {
opt.apply(&o)
}
cleanerInterval := o.cleanerInterval
// To prevent running the cleaner in each shard.
o.cleanerInterval = 0
shards := make([]*Cache[K, V], n)
for i := 0; i < n; i++ {
shards[i] = newCache(o)
}
s := &Sharded[K, V]{
shards: shards,
hasher: hasher,
mask: uint64(n - 1),
}
if cleanerInterval > 0 {
s.cleaner = newCleaner()
if err := s.cleaner.start(s, cleanerInterval); err != nil {
// With current sanitization this should never happen.
panic(err)
}
}
return s
}
// Sharded is a sharded in-memory cache.
// It is a cache consisting of n shards
// and sharded by the given Hasher64.
//
// Each shard is a separate Cache instance.
//
// By default, it has no default expiration, no default sliding expiration
// no entry limit and no eviction callback.
//
// The zero value Sharded is NOT ready to use.
// The NewSharded function must be used to create a new Sharded.
//
// Example:
//
// c := imcache.NewSharded[string, interface{}](8, imcache.DefaultStringHasher64{},
// imcache.WithDefaultExpirationOption[string, interface{}](time.Second),
// imcache.WithCleanerOption[string, interface{}](5*time.Minute),
// imcache.WithMaxEntriesOption[string, interface{}](10000),
// imcache.WithEvictionCallbackOption[string, interface{}](LogEvictedEntry),
// )
type Sharded[K comparable, V any] struct {
hasher Hasher64[K]
cleaner *cleaner
shards []*Cache[K, V]
mask uint64
}
// shard returns the shard for the given key.
func (s *Sharded[K, V]) shard(key K) *Cache[K, V] {
return s.shards[s.shardIndex(key)]
}
// shardIndex returns the shard index for the given key.
func (s *Sharded[K, V]) shardIndex(key K) int {
return int(s.hasher.Sum64(key) & s.mask)
}
// Get returns the value for the given key.
//
// If it encounters an expired entry, the expired entry is evicted.
func (s *Sharded[K, V]) Get(key K) (value V, present bool) {
return s.shard(key).Get(key)
}
// GetMultiple returns the values for the given keys.
// If the Sharded is not closed, then the returned map
// is always a non-nil one.
//
// If it encounters an expired entry, the expired entry is evicted.
func (s *Sharded[K, V]) GetMultiple(keys ...K) map[K]V {
now := nowf()
keysByShard := make(map[int][]K)
for _, key := range keys {
idx := s.shardIndex(key)
keysByShard[idx] = append(keysByShard[idx], key)
}
ms := make([]map[K]V, len(keysByShard))
var n int
for idx, keys := range keysByShard {
m := s.shards[idx].getMultiple(now, keys...)
if m == nil {
// getMultiple returns nil if the shard is closed.
// If the shard is closed, then the Sharded is closed too.
return nil
}
ms = append(ms, m)
n += len(m)
}
result := make(map[K]V, n)
for _, m := range ms {
for k, v := range m {
result[k] = v
}
}
return result
}
// GetAll returns a copy of all entries in the cache.
//
// If it encounters an expired entry, the expired entry is evicted.
func (s *Sharded[K, V]) GetAll() map[K]V {
now := nowf()
var n int
ms := make([]map[K]V, 0, len(s.shards))
for _, shard := range s.shards {
m := shard.getAll(now)
// If Cache.getAll returns nil, it means that the shard is closed
// hence Sharded is closed too.
if m == nil {
return nil
}
n += len(m)
ms = append(ms, m)
}
all := make(map[K]V, n)
for _, m := range ms {
for key, val := range m {
all[key] = val
}
}
return all
}
// Peek returns the value for the given key without
// actively evicting the entry if it is expired and
// updating the entry's sliding expiration.
//
// If the max entries limit is set, it doesn't update
// the entry's position in the eviction queue.
func (s *Sharded[K, V]) Peek(key K) (V, bool) {
return s.shard(key).Peek(key)
}
// PeekMultiple returns the values for the given keys without
// actively evicting the encountered entry if it is expired and
// updating the entry's sliding expiration.
// If the Sharded is not closed, then the returned map
// is always a non-nil one.
//
// If the max entries limit is set, it doesn't update
// the encountered entry's position in the eviction queue.
func (s *Sharded[K, V]) PeekMultiple(keys ...K) map[K]V {
now := nowf()
keysByShard := make(map[int][]K)
for _, key := range keys {
idx := s.shardIndex(key)
keysByShard[idx] = append(keysByShard[idx], key)
}
ms := make([]map[K]V, len(keysByShard))
var n int
for idx, keys := range keysByShard {
m := s.shards[idx].peekMultiple(now, keys...)
if m == nil {
// peekMultiple returns nil if the shard is closed.
// If the shard is closed, then the Sharded is closed too.
return nil
}
ms = append(ms, m)
n += len(m)
}
result := make(map[K]V, n)
for _, m := range ms {
for k, v := range m {
result[k] = v
}
}
return result
}
// PeekAll returns a copy of all entries in the cache without
// actively evicting the encountered entry if it is expired and
// updating the entry's sliding expiration.
//
// If the max entries limit is set, it doesn't update
// the encountered entry's position in the eviction queue.
func (s *Sharded[K, V]) PeekAll() map[K]V {
now := nowf()
var n int
ms := make([]map[K]V, 0, len(s.shards))
for _, shard := range s.shards {
m := shard.peekAll(now)
// If Cache.peekAll returns nil, it means that the shard is closed
// hence Sharded is closed too.
if m == nil {
return nil
}
n += len(m)
ms = append(ms, m)
}
all := make(map[K]V, n)
for _, m := range ms {
for key, val := range m {
all[key] = val
}
}
return all
}