-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocal.go
631 lines (574 loc) · 15.8 KB
/
local.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
package cache
import (
"errors"
"sync"
"sync/atomic"
"time"
)
const (
// Default maximum number of cache entries.
maximumCapacity = 1 << 30
// Buffer size of entry channels
chanBufSize = 64
// Maximum number of entries to be drained in a single clean up.
drainMax = 16
// Number of cache access operations that will trigger clean up.
drainThreshold = 64
)
// currentTime is an alias for time.Now, used for testing.
var currentTime = time.Now
// localCache is an asynchronous LRU cache.
type localCache struct {
// internal data structure
cache cache // Must be aligned on 32-bit
// user configurations
expireAfterAccess time.Duration
expireAfterWrite time.Duration
refreshAfterWrite time.Duration
policyName string
onInsertion Func
onRemoval Func
loader LoaderFunc
reloader Reloader
stats StatsCounter
// cap is the cache capacity.
cap int
// accessQueue is the cache retention policy, which manages entries by access time.
accessQueue policy
// writeQueue is for managing entries by write time.
// It is only fulfilled when expireAfterWrite or refreshAfterWrite is set.
writeQueue policy
// events is the cache event queue for processEntries
events chan entryEvent
// readCount is a counter of the number of reads since the last write.
readCount int32
// for closing routines created by this cache.
closing int32
closeWG sync.WaitGroup
}
// newLocalCache returns a default localCache.
// init must be called before this cache can be used.
func newLocalCache() *localCache {
return &localCache{
cap: maximumCapacity,
cache: cache{},
stats: &statsCounter{},
}
}
// init initializes cache replacement policy after all user configuration properties are set.
func (c *localCache) init() {
c.accessQueue = newPolicy(c.policyName)
c.accessQueue.init(&c.cache, c.cap)
if c.expireAfterWrite > 0 || c.refreshAfterWrite > 0 {
c.writeQueue = &recencyQueue{}
} else {
c.writeQueue = discardingQueue{}
}
c.writeQueue.init(&c.cache, c.cap)
c.events = make(chan entryEvent, chanBufSize)
c.closeWG.Add(1)
go c.processEntries()
}
// Close implements io.Closer and always returns a nil error.
// Caller would ensure the cache is not being used (reading and writing) before closing.
func (c *localCache) Close() error {
if atomic.CompareAndSwapInt32(&c.closing, 0, 1) {
// Do not close events channel to avoid panic when cache is still being used.
c.events <- entryEvent{nil, eventClose}
// Wait for the goroutine to close this channel
c.closeWG.Wait()
}
return nil
}
// GetIfPresent gets cached value from entries list and updates
// last access time for the entry if it is found.
func (c *localCache) GetIfPresent(k Key) (Value, bool) {
en := c.cache.get(k, sum(k))
if en == nil {
c.stats.RecordMisses(1)
return nil, false
}
now := currentTime()
if c.isExpired(en, now) {
c.sendEvent(eventDelete, en)
c.stats.RecordMisses(1)
return nil, false
}
c.setEntryAccessTime(en, now)
c.sendEvent(eventAccess, en)
c.stats.RecordHits(1)
return en.getValue(), true
}
// Put adds new entry to entries list.
func (c *localCache) Put(k Key, v Value) {
h := sum(k)
en := c.cache.get(k, h)
now := currentTime()
if en == nil {
en = newEntry(k, v, h)
c.setEntryWriteTime(en, now)
c.setEntryAccessTime(en, now)
// Add to the cache directly so the new value is available immediately.
// However, only do this within the cache capacity (approximately).
if c.cap == 0 || c.cache.len() < c.cap {
cen := c.cache.getOrSet(en)
if cen != nil {
cen.setValue(v)
c.setEntryWriteTime(cen, now)
en = cen
}
}
} else {
// Update value and send notice
en.setValue(v)
c.setEntryWriteTime(en, now)
}
c.sendEvent(eventWrite, en)
}
// Invalidate removes the entry associated with key k.
func (c *localCache) Invalidate(k Key) {
en := c.cache.get(k, sum(k))
if en != nil {
en.setInvalidated(true)
c.sendEvent(eventDelete, en)
}
}
// InvalidateAll resets entries list.
func (c *localCache) InvalidateAll() {
c.cache.walk(func(en *entry) {
en.setInvalidated(true)
})
c.sendEvent(eventDelete, nil)
}
// Get returns value associated with k or call underlying loader to retrieve value
// if it is not in the cache. The returned value is only cached when loader returns
// nil error.
func (c *localCache) Get(k Key) (Value, error) {
en := c.cache.get(k, sum(k))
if en == nil {
c.stats.RecordMisses(1)
return c.load(k)
}
// Check if this entry needs to be refreshed
now := currentTime()
if c.isExpired(en, now) {
if c.loader == nil {
c.sendEvent(eventDelete, en)
} else {
// For loading cache, we do not delete entry but leave it to
// the eviction policy, so users still can get the old value.
c.setEntryAccessTime(en, now)
c.refreshAsync(en)
}
c.stats.RecordMisses(1)
} else {
c.setEntryAccessTime(en, now)
c.sendEvent(eventAccess, en)
c.stats.RecordHits(1)
}
return en.getValue(), nil
}
func (c *localCache) GetActive(k Key) (Value, error) {
obj, err := c.Get(k)
if err != nil {
return nil, err
}
en := c.cache.get(k, sum(k))
if en != nil && ! en.getInvalidated() {
return obj, nil
}
return nil, errors.New ("entry invalidated")
}
// GetAllKeys returns all keys.
func (c *localCache) GetAllKeys() []interface{} {
keys := make([]interface{}, 0, c.cache.len())
c.cache.walk(func(en *entry) {
if ! en.getInvalidated() {
keys = append(keys, en.key)
}
})
return keys
}
// GetAllValues returns all values.
func (c *localCache) GetAllValues() []interface{} {
values := make([]interface{}, 0, c.cache.len())
c.cache.walk(func(en *entry) {
if ! en.getInvalidated() {
values = append(values, en.getValue())
}
})
return values
}
// GetAll returns all entries.
func (c *localCache) GetAll() map[interface{}]interface{} {
var values = make(map[interface{}]interface{}, c.cache.len())
c.cache.walk(func(en *entry) {
if ! en.getInvalidated() {
values[en.key] = en.getValue()
}
})
return values
}
// Size returns the number of entries in the cache.
func (c *localCache) Size() int {
return c.cache.len()
}
// Refresh asynchronously reloads value for Key if it existed, otherwise
// it will synchronously load and block until it value is loaded.
func (c *localCache) Refresh(k Key) {
if c.loader == nil {
return
}
en := c.cache.get(k, sum(k))
if en == nil {
c.load(k)
} else {
c.refreshAsync(en)
}
}
// Stats copies cache stats to t.
func (c *localCache) Stats(t *Stats) {
c.stats.Snapshot(t)
}
func (c *localCache) processEntries() {
defer c.closeWG.Done()
for e := range c.events {
switch e.event {
case eventWrite:
c.write(e.entry)
c.postWriteCleanup()
case eventAccess:
c.access(e.entry)
c.postReadCleanup()
case eventDelete:
if e.entry == nil {
c.removeAll()
} else {
c.remove(e.entry)
}
c.postReadCleanup()
case eventClose:
if c.reloader != nil {
// Stop all refresh tasks.
c.reloader.Close()
}
c.removeAll()
return
}
}
}
// sendEvent sends event only when the cache is not closing/closed.
func (c *localCache) sendEvent(typ event, en *entry) {
if atomic.LoadInt32(&c.closing) == 0 {
c.events <- entryEvent{en, typ}
}
}
// This function must only be called from processEntries goroutine.
func (c *localCache) write(en *entry) {
ren := c.accessQueue.write(en)
c.writeQueue.write(en)
if c.onInsertion != nil {
c.onInsertion(en.key, en.getValue())
}
if ren != nil {
c.writeQueue.remove(ren)
// An entry has been evicted
c.stats.RecordEviction()
if c.onRemoval != nil {
c.onRemoval(ren.key, ren.getValue())
}
}
}
// removeAll remove all entries in the cache.
// This function must only be called from processEntries goroutine.
func (c *localCache) removeAll() {
c.accessQueue.iterate(func(en *entry) bool {
c.remove(en)
return true
})
}
// remove removes the given element from the cache and entries list.
// It also calls onRemoval callback if it is set.
func (c *localCache) remove(en *entry) {
ren := c.accessQueue.remove(en)
c.writeQueue.remove(en)
if ren != nil && c.onRemoval != nil {
c.onRemoval(ren.key, ren.getValue())
}
}
// access moves the given element to the top of the entries list.
// This function must only be called from processEntries goroutine.
func (c *localCache) access(en *entry) {
c.accessQueue.access(en)
}
// load uses current loader to synchronously retrieve value for k and adds new
// entry to the cache only if loader returns a nil error.
func (c *localCache) load(k Key) (Value, error) {
if c.loader == nil {
panic("cache loader function must be set")
}
// TODO: Poll the value instead when the entry is loading.
start := currentTime()
v, err := c.loader(k)
now := currentTime()
loadTime := now.Sub(start)
if err != nil {
c.stats.RecordLoadError(loadTime)
return nil, err
}
en := newEntry(k, v, sum(k))
c.setEntryWriteTime(en, now)
c.setEntryAccessTime(en, now)
if c.cap == 0 || c.cache.len() < c.cap {
cen := c.cache.getOrSet(en)
if cen != nil {
cen.setValue(v)
c.setEntryWriteTime(cen, now)
en = cen
}
}
c.sendEvent(eventWrite, en)
c.stats.RecordLoadSuccess(loadTime)
return v, nil
}
// refreshAsync reloads value in a go routine or using custom executor if defined.
func (c *localCache) refreshAsync(en *entry) bool {
if en.setLoading(true) {
// Only do refresh if it isn't running.
if c.reloader == nil {
go c.refresh(en)
} else {
c.reload(en)
}
return true
}
return false
}
// refresh reloads value for the given key. If loader returns an error,
// that error will be omitted. Otherwise, the entry value will be updated.
// This function would only be called by refreshAsync.
func (c *localCache) refresh(en *entry) {
defer en.setLoading(false)
start := currentTime()
v, err := c.loader(en.key)
now := currentTime()
loadTime := now.Sub(start)
if err == nil {
en.setValue(v)
c.setEntryWriteTime(en, now)
c.sendEvent(eventWrite, en)
c.stats.RecordLoadSuccess(loadTime)
} else {
// TODO: Log error
c.stats.RecordLoadError(loadTime)
}
}
// reload uses user-defined reloader to reloads value.
func (c *localCache) reload(en *entry) {
start := currentTime()
setFn := func(newValue Value, err error) {
defer en.setLoading(false)
now := currentTime()
loadTime := now.Sub(start)
if err == nil {
en.setValue(newValue)
c.setEntryWriteTime(en, now)
c.sendEvent(eventWrite, en)
c.stats.RecordLoadSuccess(loadTime)
} else {
c.stats.RecordLoadError(loadTime)
}
}
c.reloader.Reload(en.key, en.getValue(), setFn)
}
// postReadCleanup is run after entry access/delete event.
// This function must only be called from processEntries goroutine.
func (c *localCache) postReadCleanup() {
if atomic.AddInt32(&c.readCount, 1) > drainThreshold {
atomic.StoreInt32(&c.readCount, 0)
c.expireEntries()
}
}
// postWriteCleanup is run after entry add event.
// This function must only be called from processEntries goroutine.
func (c *localCache) postWriteCleanup() {
atomic.StoreInt32(&c.readCount, 0)
c.expireEntries()
}
// expireEntries removes expired entries.
func (c *localCache) expireEntries() {
remain := drainMax
now := currentTime()
if c.expireAfterAccess > 0 {
expiry := now.Add(-c.expireAfterAccess).UnixNano()
c.accessQueue.iterate(func(en *entry) bool {
if remain == 0 || en.getAccessTime() >= expiry {
// Can stop as the entries are sorted by access time.
// (the next entry is accessed more recently.)
return false
}
// accessTime + expiry passed
c.remove(en)
c.stats.RecordEviction()
remain--
return remain > 0
})
}
if remain > 0 && c.expireAfterWrite > 0 {
expiry := now.Add(-c.expireAfterWrite).UnixNano()
c.writeQueue.iterate(func(en *entry) bool {
if remain == 0 || en.getWriteTime() >= expiry {
return false
}
// writeTime + expiry passed
c.remove(en)
c.stats.RecordEviction()
remain--
return remain > 0
})
}
if remain > 0 && c.loader != nil && c.refreshAfterWrite > 0 {
expiry := now.Add(-c.refreshAfterWrite).UnixNano()
c.writeQueue.iterate(func(en *entry) bool {
if remain == 0 || en.getWriteTime() >= expiry {
return false
}
// FIXME: This can cause deadlock if the custom executor runs refresh in current go routine.
// The refresh function, when finish, will send to event channels.
if c.refreshAsync(en) {
// TODO: Maybe move this entry up?
remain--
}
return remain > 0
})
}
}
func (c *localCache) isExpired(en *entry, now time.Time) bool {
if en.getInvalidated() {
return true
}
if c.expireAfterAccess > 0 && en.getAccessTime() < now.Add(-c.expireAfterAccess).UnixNano() {
// accessTime + expiry passed
return true
}
if c.expireAfterWrite > 0 && en.getWriteTime() < now.Add(-c.expireAfterWrite).UnixNano() {
// writeTime + expiry passed
return true
}
return false
}
func (c *localCache) needRefresh(en *entry, now time.Time) bool {
if en.getLoading() {
return false
}
if c.refreshAfterWrite > 0 {
tm := en.getWriteTime()
if tm > 0 && tm < now.Add(-c.refreshAfterWrite).UnixNano() {
// writeTime + refresh passed
return true
}
}
return false
}
// setEntryAccessTime sets access time if needed.
func (c *localCache) setEntryAccessTime(en *entry, now time.Time) {
if c.expireAfterAccess > 0 {
en.setAccessTime(now.UnixNano())
}
}
// setEntryWriteTime sets write time if needed.
func (c *localCache) setEntryWriteTime(en *entry, now time.Time) {
if c.expireAfterWrite > 0 || c.refreshAfterWrite > 0 {
en.setWriteTime(now.UnixNano())
}
}
// New returns a local in-memory Cache.
func New(options ...Option) Cache {
c := newLocalCache()
for _, opt := range options {
opt(c)
}
c.init()
return c
}
// NewLoadingCache returns a new LoadingCache with given loader function
// and cache options.
func NewLoadingCache(loader LoaderFunc, options ...Option) LoadingCache {
c := newLocalCache()
c.loader = loader
for _, opt := range options {
opt(c)
}
c.init()
return c
}
// Option add options for default Cache.
type Option func(c *localCache)
// WithMaximumSize returns an Option which sets maximum size for the cache.
// Any non-positive numbers is considered as unlimited.
func WithMaximumSize(size int) Option {
if size < 0 {
size = 0
}
if size > maximumCapacity {
size = maximumCapacity
}
return func(c *localCache) {
c.cap = size
}
}
// WithRemovalListener returns an Option to set cache to call onRemoval for each
// entry evicted from the cache.
func WithRemovalListener(onRemoval Func) Option {
return func(c *localCache) {
c.onRemoval = onRemoval
}
}
// WithExpireAfterAccess returns an option to expire a cache entry after the
// given duration without being accessed.
func WithExpireAfterAccess(d time.Duration) Option {
return func(c *localCache) {
c.expireAfterAccess = d
}
}
// WithExpireAfterWrite returns an option to expire a cache entry after the
// given duration from creation.
func WithExpireAfterWrite(d time.Duration) Option {
return func(c *localCache) {
c.expireAfterWrite = d
}
}
// WithRefreshAfterWrite returns an option to refresh a cache entry after the
// given duration. This option is only applicable for LoadingCache.
func WithRefreshAfterWrite(d time.Duration) Option {
return func(c *localCache) {
c.refreshAfterWrite = d
}
}
// WithStatsCounter returns an option which overrides default cache stats counter.
func WithStatsCounter(st StatsCounter) Option {
return func(c *localCache) {
c.stats = st
}
}
// WithPolicy returns an option which sets cache policy associated to the given name.
// Supported policies are: lru, slru, tinylfu.
func WithPolicy(name string) Option {
return func(c *localCache) {
c.policyName = name
}
}
// WithReloader returns an option which sets reloader for a loading cache.
// By default, each asynchronous reload is run in a go routine.
// This option is only applicable for LoadingCache.
func WithReloader(reloader Reloader) Option {
return func(c *localCache) {
c.reloader = reloader
}
}
// withInsertionListener is used for testing.
func withInsertionListener(onInsertion Func) Option {
return func(c *localCache) {
c.onInsertion = onInsertion
}
}