forked from hashicorp/golang-lru
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpiring.go
414 lines (377 loc) · 11.1 KB
/
expiring.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
package lru
import (
"container/list"
"fmt"
"sync"
"time"
"github.com/hashicorp/golang-lru/simplelru"
)
// common interface shared by 2q, arc and simple LRU, used as interface of backing LRU
type lruCache interface {
// Adds a value to the cache, returns true if happened and
// updates the "recently used"-ness of the key.
Add(k, v interface{}) (evicted bool)
// Returns key's value from the cache if found and
// updates the "recently used"-ness of the key.
Get(k interface{}) (v interface{}, ok bool)
// Removes a key from the cache
Remove(k interface{}) bool
// Returns key's value without updating the "recently used"-ness of the key.
Peek(key interface{}) (value interface{}, ok bool)
// Checks if a key exists in cache without updating the recent-ness.
Contains(k interface{}) bool
// Returns a slice of the keys in the cache, from oldest to newest.
Keys() []interface{}
// Returns the number of items in the cache.
Len() int
// Clears all cache entries.
Purge()
}
type entry struct {
key interface{}
val interface{}
expirationTime time.Time
elem *list.Element
}
func (e entry) String() string {
return fmt.Sprintf("%v,%v %v", e.key, e.val, e.expirationTime)
}
// two expiration policies
type expiringType byte
const (
expireAfterWrite expiringType = iota
expireAfterAccess
)
// ExpiringCache will wrap an existing LRU and make its entries expiring
// according to two policies:
// expireAfterAccess and expireAfterWrite (default)
// Internally keep a expireList sorted by entries' expirationTime
type ExpiringCache struct {
lru lruCache
expiration time.Duration
expireList *expireList
expireType expiringType
evictedEntry *entry
onEvictedCB func(k, v interface{})
// placeholder for time.Now() for easier testing setup
timeNow func() time.Time
lock sync.RWMutex
}
// OptionExp defines options to customize ExpiringCache
type OptionExp func(c *ExpiringCache) error
func newExpiringCacheWithOptions(expir time.Duration, opts []OptionExp) (elru *ExpiringCache, err error) {
// create expiring cache with default settings
elru = &ExpiringCache{
expiration: expir,
expireList: newExpireList(),
expireType: expireAfterWrite,
timeNow: time.Now,
}
// apply options to customize
for _, opt := range opts {
if err = opt(elru); err != nil {
return
}
}
return
}
// NewExpiring2Q creates an expiring cache with specifized
// size and entries lifetime duration, backed by a 2-queue LRU
func NewExpiring2Q(size int, expir time.Duration, opts ...OptionExp) (elru *ExpiringCache, err error) {
if elru, err = newExpiringCacheWithOptions(expir, opts); err != nil {
return
}
elru.lru, err = simplelru.New2QWithEvict(size, elru.onEvicted)
if err != nil {
return
}
return
}
// NewExpiringARC creates an expiring cache with specifized
// size and entries lifetime duration, backed by a ARC LRU
func NewExpiringARC(size int, expir time.Duration, opts ...OptionExp) (elru *ExpiringCache, err error) {
if elru, err = newExpiringCacheWithOptions(expir, opts); err != nil {
return
}
elru.lru, err = simplelru.NewARCWithEvict(size, elru.onEvicted)
if err != nil {
return
}
return
}
// NewExpiringLRU creates an expiring cache with specifized
// size and entries lifetime duration, backed by a simple LRU
func NewExpiringLRU(size int, expir time.Duration, opts ...OptionExp) (elru *ExpiringCache, err error) {
if elru, err = newExpiringCacheWithOptions(expir, opts); err != nil {
return
}
elru.lru, err = simplelru.NewLRU(size, elru.onEvicted)
if err != nil {
return
}
return
}
// ExpireAfterWrite sets expiring policy
func ExpireAfterWrite(elru *ExpiringCache) error {
elru.expireType = expireAfterWrite
return nil
}
// ExpireAfterAccess sets expiring policy
func ExpireAfterAccess(elru *ExpiringCache) error {
elru.expireType = expireAfterAccess
return nil
}
// EvictedCallback register a callback to receive expired/evicted key, values
func EvictedCallback(cb func(k, v interface{})) OptionExp {
return func(elru *ExpiringCache) error {
elru.onEvictedCB = cb
return nil
}
}
// TimeTicker sets the function used to return current time, for test setup
func TimeTicker(tn func() time.Time) OptionExp {
return func(elru *ExpiringCache) error {
elru.timeNow = tn
return nil
}
}
// buffer evicted key/val to be sent on registered callback
func (elru *ExpiringCache) onEvicted(k, v interface{}) {
elru.evictedEntry = v.(*entry)
}
// Add add a key/val pair to cache with cache's default expiration duration
// return true if eviction happens.
// Should be used in most cases for better performance
func (elru *ExpiringCache) Add(k, v interface{}) (evicted bool) {
return elru.AddWithTTL(k, v, elru.expiration)
}
// AddWithTTL add a key/val pair to cache with provided expiration duration
// return true if eviction happens.
// Using this with variant expiration durations could cause degraded performance
func (elru *ExpiringCache) AddWithTTL(k, v interface{}, expiration time.Duration) (evicted bool) {
elru.lock.Lock()
now := elru.timeNow()
var ent *entry
var expired []*entry
if ent0, _ := elru.lru.Peek(k); ent0 != nil {
// update existing cache entry
ent = ent0.(*entry)
ent.val = v
ent.expirationTime = now.Add(expiration)
elru.expireList.MoveToFront(ent)
} else {
// first remove 1 possible expiration to add space for new entry
expired = elru.removeExpired(now, false)
// add new entry to expiration list
ent = &entry{
key: k,
val: v,
expirationTime: now.Add(expiration),
}
elru.expireList.PushFront(ent)
}
// Add/Update cache entry in backing cache
evicted = elru.lru.Add(k, ent)
var ke, ve interface{}
if evicted {
// remove evicted ent from expireList
ke, ve = elru.evictedEntry.key, elru.evictedEntry.val
elru.expireList.Remove(elru.evictedEntry)
elru.evictedEntry = nil
} else if len(expired) > 0 {
evicted = true
ke = expired[0].key
ve = expired[0].val
}
elru.lock.Unlock()
if evicted && elru.onEvictedCB != nil {
elru.onEvictedCB(ke, ve)
}
return evicted
}
// Get returns key's value from the cache if found
func (elru *ExpiringCache) Get(k interface{}) (v interface{}, ok bool) {
elru.lock.Lock()
defer elru.lock.Unlock()
now := elru.timeNow()
if ent0, ok := elru.lru.Get(k); ok {
ent := ent0.(*entry)
if ent.expirationTime.After(now) {
if elru.expireType == expireAfterAccess {
ent.expirationTime = now.Add(elru.expiration)
elru.expireList.MoveToFront(ent)
}
return ent.val, true
}
}
return
}
// Remove removes a key from the cache
func (elru *ExpiringCache) Remove(k interface{}) (ok bool) {
var ke, ve interface{}
elru.lock.Lock()
if ok = elru.lru.Remove(k); ok {
// there must be a eviction
elru.expireList.Remove(elru.evictedEntry)
ke, ve = elru.evictedEntry.key, elru.evictedEntry.val
elru.evictedEntry = nil
}
elru.lock.Unlock()
if ok && elru.onEvictedCB != nil {
elru.onEvictedCB(ke, ve)
}
return
}
// Peek return key's value without updating the "recently used"-ness of the key.
// returns ok=false if k not found or entry expired
func (elru *ExpiringCache) Peek(k interface{}) (v interface{}, ok bool) {
elru.lock.RLock()
defer elru.lock.RUnlock()
if ent0, ok := elru.lru.Peek(k); ok {
ent := ent0.(*entry)
if ent.expirationTime.After(elru.timeNow()) {
return ent.val, true
}
return ent.val, false
}
return
}
// Contains is used to check if the cache contains a key
// without updating recency or frequency.
func (elru *ExpiringCache) Contains(k interface{}) bool {
_, ok := elru.Peek(k)
return ok
}
// Keys returns a slice of the keys in the cache.
// The frequently used keys are first in the returned slice.
func (elru *ExpiringCache) Keys() (res []interface{}) {
elru.lock.Lock()
// to get accurate key set, remove all expired
ents := elru.removeExpired(elru.timeNow(), true)
res = elru.lru.Keys()
elru.lock.Unlock()
if elru.onEvictedCB != nil {
for _, ent := range ents {
elru.onEvictedCB(ent.key, ent.val)
}
}
return
}
// Len returns the number of items in the cache.
func (elru *ExpiringCache) Len() (sz int) {
elru.lock.Lock()
// to get accurate size, remove all expired
ents := elru.removeExpired(elru.timeNow(), true)
sz = elru.lru.Len()
elru.lock.Unlock()
if elru.onEvictedCB != nil {
for _, ent := range ents {
elru.onEvictedCB(ent.key, ent.val)
}
}
return
}
// Purge is used to completely clear the cache.
func (elru *ExpiringCache) Purge() {
var ents []*entry
elru.lock.Lock()
if elru.onEvictedCB != nil {
ents = elru.expireList.AllEntries()
}
elru.lru.Purge()
elru.evictedEntry = nil
elru.expireList.Init()
elru.lock.Unlock()
if elru.onEvictedCB != nil {
for _, ent := range ents {
elru.onEvictedCB(ent.key, ent.val)
}
}
}
// RemoveAllExpired remove all expired entries, can be called by cleanup goroutine
func (elru *ExpiringCache) RemoveAllExpired() {
elru.lock.Lock()
ents := elru.removeExpired(elru.timeNow(), true)
elru.lock.Unlock()
if elru.onEvictedCB != nil {
for _, ent := range ents {
elru.onEvictedCB(ent.key, ent.val)
}
}
}
// either remove one (the oldest expired), or all expired
func (elru *ExpiringCache) removeExpired(now time.Time, removeAllExpired bool) (res []*entry) {
res = elru.expireList.RemoveExpired(now, removeAllExpired)
for i := 0; i < len(res); i++ {
elru.lru.Remove(res[i].key)
}
// now here we already remove them from expireList,
// don't need to do it again
elru.evictedEntry = nil
return
}
// oldest entries are at front of expire list
type expireList struct {
expList *list.List
}
func newExpireList() *expireList {
return &expireList{
expList: list.New(),
}
}
func (el *expireList) Init() {
el.expList.Init()
}
func (el *expireList) PushFront(ent *entry) {
// When all operations use ExpiringCache default expiration,
// PushFront should succeed at first/front entry of list
for e := el.expList.Front(); e != nil; e = e.Next() {
if !ent.expirationTime.Before(e.Value.(*entry).expirationTime) {
ent.elem = el.expList.InsertBefore(ent, e)
return
}
}
ent.elem = el.expList.PushBack(ent)
}
func (el *expireList) MoveToFront(ent *entry) {
// When all operations use ExpiringCache default expiration,
// MoveToFront should succeed at first/front entry of list
for e := el.expList.Front(); e != nil; e = e.Next() {
if !ent.expirationTime.Before(e.Value.(*entry).expirationTime) {
el.expList.MoveBefore(ent.elem, e)
return
}
}
el.expList.MoveAfter(ent.elem, el.expList.Back())
}
func (el *expireList) AllEntries() (res []*entry) {
for e := el.expList.Front(); e != nil; e = e.Next() {
res = append(res, e.Value.(*entry))
}
return
}
func (el *expireList) Remove(ent *entry) interface{} {
return el.expList.Remove(ent.elem)
}
// either remove one (the oldest expired), or remove all expired
func (el *expireList) RemoveExpired(now time.Time, removeAllExpired bool) (res []*entry) {
back := el.expList.Back()
if back == nil || back.Value.(*entry).expirationTime.After(now) {
return
}
// expired
ent := el.expList.Remove(back).(*entry)
res = append(res, ent)
if removeAllExpired {
for {
back = el.expList.Back()
if back == nil || back.Value.(*entry).expirationTime.After(now) {
break
}
// expired
ent := el.expList.Remove(back).(*entry)
res = append(res, ent)
}
}
return
}