-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtlru.go
493 lines (423 loc) · 13.6 KB
/
tlru.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
// * tlru <https://github.com/jahnestacado/tlru>
// * Copyright (c) 2020 Ioannis Tzanellis
// * Licensed under the MIT License (MIT).
// Package tlru (Time aware Least Recently Used) cache
package tlru
import (
"fmt"
"sync"
"time"
)
// Config of cache
type Config[K comparable, V any] struct {
// Max size of cache
MaxSize int
// Time to live of cached entries
TTL time.Duration
// Channel to listen for evicted entries events
EvictionChannel *chan EvictedEntry[K, V]
// Eviction policy of tlru. Default is LRA
EvictionPolicy evictionPolicy
// GarbageCollectionInterval. If not set it defaults to 10 seconds
GarbageCollectionInterval time.Duration
}
// Entry in cache
type Entry[K comparable, V any] struct {
// The unique identifier of entry
Key K `json:"key"`
// The value to be cached
Value V `json:"value"`
// Optional field. If provided TTL of entry will be checked against this field
// Timestamp is in UTC
Timestamp *time.Time `json:"timestamp"`
}
// CacheEntry holds the cached value along with some additional information
type CacheEntry[K comparable, V any] struct {
// The unique identifier of entry
Key K `json:"key"`
// The cached value
Value V `json:"value"`
// The number of times this entry has been inserted or accessed based
// on the EvictionPolicy
Counter int64 `json:"counter"`
// The time that this entry was last inserted or accessed based
// on the EvictionPolicy
LastUsedAt time.Time `json:"last_used_at"`
// The time this entry was inserted to the cache
CreatedAt time.Time `json:"created_at"`
}
// EvictedEntry is an entry that is removed from the cache due to
// an evictionReason
type EvictedEntry[K comparable, V any] struct {
CacheEntry[K, V]
// The time this entry was evicted from the cache
EvictedAt time.Time `json:"evicted_at"`
// The reason this entry has been removed
Reason evictionReason `json:"reason"`
}
// State is the internal representation of the cache.
// State can be retrieved/set via the GetState/SetState methods respectively
type State[K comparable, V any] struct {
Entries []StateEntry[K, V] `json:"entries"`
EvictionPolicy evictionPolicy `json:"eviction_policy"`
ExtractedAt time.Time `json:"extracted_at"`
}
// StateEntry is a representation of a doublyLinkedNode without pointer references
type StateEntry[K comparable, V any] struct {
Key K `json:"key"`
Value V `json:"value"`
Counter int64 `json:"counter"`
LastUsedAt time.Time `json:"last_used_at"`
CreatedAt time.Time `json:"created_at"`
}
const (
// LRA - Least Recenty Accessed
LRA evictionPolicy = iota
// LRI - Least Recenty Inserted
LRI
)
const (
// EvictionReasonDropped occurs when cache is full
EvictionReasonDropped evictionReason = iota
// EvictionReasonExpired occurs when the TTL of an entry is expired
EvictionReasonExpired
// EvictionReasonDeleted occurs when the Delete method is called for a key
EvictionReasonDeleted
)
const (
defaultGarbageCollectionInterval = 10 * time.Second
)
// TLRU cache
type TLRU[K comparable, V any] struct {
sync.RWMutex
cache map[K]*doublyLinkedNode[K, V]
config Config[K, V]
headNode *doublyLinkedNode[K, V]
tailNode *doublyLinkedNode[K, V]
garbageCollectionInterval time.Duration
garbageCollectionTimer *time.Timer
}
// New returns a new instance of TLRU cache
func New[K comparable, V any](config Config[K, V]) *TLRU[K, V] {
var headNodeRef, tailNodeRef K
headNode := &doublyLinkedNode[K, V]{key: headNodeRef}
tailNode := &doublyLinkedNode[K, V]{key: tailNodeRef}
headNode.next = tailNode
tailNode.previous = headNode
garbageCollectionInterval := defaultGarbageCollectionInterval
if config.GarbageCollectionInterval > 0 {
garbageCollectionInterval = config.GarbageCollectionInterval
}
cache := &TLRU[K, V]{
config: config,
cache: make(map[K]*doublyLinkedNode[K, V]),
garbageCollectionInterval: garbageCollectionInterval,
}
cache.initializeDoublyLinkedList()
return cache
}
// Get retrieves an entry from the cache by key
// Get behaves differently depending on the EvictionPolicy used
// * EvictionPolicy.LRA - (Least Recenty Accessed):
// - If the key entry exists then the entry is marked as the
// most recently used entry
// - If the key entry exists then the entrys Counter is incremented and the
// LastUsedAt property is updated
// - If an entry for the specified key doesn't exist then it returns nil
//
// * EvictionPolicy.LRI - (Least Recenty Inserted):
// - If an entry for the specified key doesn't exist then it returns nil
func (c *TLRU[K, V]) Get(key K) *CacheEntry[K, V] {
c.RLock()
linkedNode, exists := c.cache[key]
if !exists {
c.RUnlock()
return nil
}
if c.config.TTL < time.Since(linkedNode.lastUsedAt) {
c.RUnlock()
c.Lock()
defer c.Unlock()
c.evictEntry(linkedNode, EvictionReasonExpired)
return nil
}
if c.config.EvictionPolicy == LRA {
c.RUnlock()
c.Lock()
c.handleNodeState(Entry[K, V]{Key: key, Value: linkedNode.value})
c.Unlock()
c.RLock()
}
defer c.RUnlock()
cacheEntry := linkedNode.ToCacheEntry()
return &cacheEntry
}
// Set inserts/updates an entry in the cache
// Set behaves differently depending on the EvictionPolicy used
// * EvictionPolicy.LRA - (Least Recenty Accessed):
// - If the key entry doesn't exist then it inserts it as the most
// recently used entry with Counter = 0
// - If the key entry already exists then it will return an error
// - If the cache is full (Config.MaxSize) then the least recently accessed
// entry(the node before the tailNode) will be dropped and an
// EvictedEntry will be emitted to the EvictionChannel(if present)
// with EvictionReasonDropped
//
// * EvictionPolicy.LRI - (Least Recenty Inserted):
// - If the key entry doesn't exist then it inserts it as the
// most recently used entry with Counter = 1
// - If the key entry already exists then it will update
// the Value, Counter and LastUsedAt properties of
// the existing entry and mark it as the most recently used entry
// - If the cache is full (Config.MaxSize) then
// the least recently inserted entry(the node before the tailNode)
// will be dropped and an EvictedEntry will be emitted to
// the EvictionChannel(if present) with EvictionReasonDropped
func (c *TLRU[K, V]) Set(key K, value V) error {
return c.set(key, value, nil)
}
// SetWithTimestamp is identical to the Set function but it allows to set the timestamp for the inserted entry
func (c *TLRU[K, V]) SetWithTimestamp(key K, value V, timestamp time.Time) error {
return c.set(key, value, ×tamp)
}
func (c *TLRU[K, V]) set(key K, value V, timestamp *time.Time) error {
defer c.Unlock()
c.Lock()
if c.garbageCollectionTimer == nil {
c.garbageCollectionTimer = time.AfterFunc(c.garbageCollectionInterval, func() {
c.Lock()
c.evictExpiredEntries()
c.Unlock()
})
}
entry := Entry[K, V]{Key: key, Value: value, Timestamp: timestamp}
_, exists := c.cache[entry.Key]
if c.config.MaxSize != 0 && !exists && len(c.cache) == c.config.MaxSize {
c.evictEntry(c.tailNode.previous, EvictionReasonDropped)
}
if exists && c.config.EvictionPolicy == LRA {
return fmt.Errorf("tlru.Set: Key '%+v' already exist. Entry replacement is not allowed in LRA EvictionPolicy", entry.Key)
}
c.handleNodeState(entry)
return nil
}
// Delete removes the entry that corresponds to the provided key from cache
// An EvictedEntry will be emitted to the EvictionChannel(if present)
// with EvictionReasonDeleted
func (c *TLRU[K, V]) Delete(key K) {
defer c.Unlock()
c.Lock()
linkedNode, exists := c.cache[key]
if exists {
c.evictEntry(linkedNode, EvictionReasonDeleted)
}
}
// Keys returns an unordered slice of all available keys in the cache
// The order of keys is not guaranteed
// It will also evict expired entries based on the TTL of the cache
func (c *TLRU[K, V]) Keys() []K {
c.Lock()
c.evictExpiredEntries()
c.Unlock()
defer c.RUnlock()
c.RLock()
keys := make([]K, 0, len(c.cache))
for key := range c.cache {
keys = append(keys, key)
}
return keys
}
// Entries returns an unordered slice of all available entries in the cache
// The order of entries is not guaranteed
// It will also evict expired entries based on the TTL of the cache
func (c *TLRU[K, V]) Entries() []CacheEntry[K, V] {
c.Lock()
c.evictExpiredEntries()
c.Unlock()
defer c.RUnlock()
c.RLock()
entries := make([]CacheEntry[K, V], 0, len(c.cache))
for _, linkedNode := range c.cache {
entries = append(entries, linkedNode.ToCacheEntry())
}
return entries
}
// Clear removes all entries from the cache and frees underlying resources
func (c *TLRU[K, V]) Clear() {
defer c.Unlock()
c.Lock()
c.clear()
if c.garbageCollectionTimer != nil {
c.garbageCollectionTimer.Stop()
c.garbageCollectionTimer = nil
}
}
// GetState returns the internal State of the cache
// This State can be put in persistent storage and rehydrated at a later point
// via the SetState method
func (c *TLRU[K, V]) GetState() State[K, V] {
defer c.RUnlock()
c.RLock()
state := State[K, V]{
EvictionPolicy: c.config.EvictionPolicy,
Entries: make([]StateEntry[K, V], 0, len(c.cache)),
ExtractedAt: time.Now().UTC(),
}
nextNode := c.headNode.next
for nextNode != nil && nextNode != c.tailNode {
state.Entries = append(state.Entries, nextNode.ToStateEntry())
nextNode = nextNode.next
}
return state
}
// SetState sets the internal State of the cache
func (c *TLRU[K, V]) SetState(state State[K, V]) error {
defer c.Unlock()
c.Lock()
if state.EvictionPolicy != c.config.EvictionPolicy {
return fmt.Errorf("tlru.SetState: Incompatible state EvictionPolicy %s", state.EvictionPolicy.String())
}
c.clear()
previousNode := c.headNode
cache := make(map[K]*doublyLinkedNode[K, V], 0)
for _, StateEntry := range state.Entries {
rehydratedNode := &doublyLinkedNode[K, V]{
key: StateEntry.Key,
value: StateEntry.Value,
counter: StateEntry.Counter,
lastUsedAt: StateEntry.LastUsedAt,
createdAt: StateEntry.CreatedAt,
}
previousNode.next = rehydratedNode
rehydratedNode.previous = previousNode
previousNode = rehydratedNode
cache[rehydratedNode.key] = rehydratedNode
}
previousNode.next = c.tailNode
c.tailNode.previous = previousNode
c.cache = cache
return nil
}
// Has returns true if the provided keys exists in cache otherwise it returns false
func (c *TLRU[K, V]) Has(key K) bool {
defer c.RUnlock()
c.RLock()
_, exists := c.cache[key]
return exists
}
type doublyLinkedNode[K comparable, V any] struct {
key K
value V
counter int64
lastUsedAt time.Time
createdAt time.Time
previous *doublyLinkedNode[K, V]
next *doublyLinkedNode[K, V]
}
func (d *doublyLinkedNode[K, V]) ToCacheEntry() CacheEntry[K, V] {
return CacheEntry[K, V]{
Key: d.key,
Value: d.value,
Counter: d.counter,
LastUsedAt: d.lastUsedAt,
CreatedAt: d.createdAt,
}
}
func (d *doublyLinkedNode[K, V]) ToEvictedEntry(reason evictionReason) EvictedEntry[K, V] {
return EvictedEntry[K, V]{
CacheEntry: CacheEntry[K, V]{
Key: d.key,
Value: d.value,
Counter: d.counter,
LastUsedAt: d.lastUsedAt,
CreatedAt: d.createdAt,
},
EvictedAt: time.Now().UTC(),
Reason: reason,
}
}
func (d *doublyLinkedNode[K, V]) ToStateEntry() StateEntry[K, V] {
return StateEntry[K, V]{
Key: d.key,
Value: d.value,
Counter: d.counter,
LastUsedAt: d.lastUsedAt,
CreatedAt: d.createdAt,
}
}
type evictionReason int
func (e evictionReason) String() string {
return [...]string{0: "Dropped", 1: "Expired", 2: "Deleted"}[e]
}
type evictionPolicy int
func (p evictionPolicy) String() string {
return [...]string{0: "LRA", 1: "LRI"}[p]
}
func (c *TLRU[K, V]) clear() {
if len(c.cache) > 0 {
c.cache = make(map[K]*doublyLinkedNode[K, V])
c.initializeDoublyLinkedList()
}
}
func (c *TLRU[K, V]) initializeDoublyLinkedList() {
var headNodeRef, tailNodeRef K
headNode := &doublyLinkedNode[K, V]{key: headNodeRef}
tailNode := &doublyLinkedNode[K, V]{key: tailNodeRef}
headNode.next = tailNode
tailNode.previous = headNode
c.headNode = headNode
c.tailNode = tailNode
}
func (c *TLRU[K, V]) handleNodeState(e Entry[K, V]) {
var counter int64
if c.config.EvictionPolicy == LRI {
counter++
}
lastUsedAt := time.Now().UTC()
if e.Timestamp != nil {
lastUsedAt = *e.Timestamp
}
linkedNode, exists := c.cache[e.Key]
if exists {
if c.config.TTL >= time.Since(linkedNode.lastUsedAt) {
linkedNode.counter++
}
linkedNode.lastUsedAt = lastUsedAt
// Re-wire siblings of linkedNode
linkedNode.next.previous = linkedNode.previous
linkedNode.previous.next = linkedNode.next
} else {
linkedNode = &doublyLinkedNode[K, V]{
key: e.Key,
value: e.Value,
counter: counter,
lastUsedAt: lastUsedAt,
previous: c.headNode,
next: c.headNode.next,
createdAt: time.Now().UTC(),
}
c.cache[e.Key] = linkedNode
}
// Re-wire headNode
linkedNode.previous = c.headNode
linkedNode.next = c.headNode.next
c.headNode.next.previous = linkedNode
c.headNode.next = linkedNode
}
func (c *TLRU[K, V]) evictEntry(evictedNode *doublyLinkedNode[K, V], reason evictionReason) {
evictedNode.previous.next = evictedNode.next
evictedNode.next.previous = evictedNode.previous
delete(c.cache, evictedNode.key)
if c.config.EvictionChannel != nil {
*c.config.EvictionChannel <- evictedNode.ToEvictedEntry(reason)
}
}
func (c *TLRU[K, V]) evictExpiredEntries() {
previousNode := c.tailNode.previous
for previousNode != nil && previousNode != c.headNode {
if c.config.TTL < time.Since(previousNode.lastUsedAt) {
c.evictEntry(previousNode, EvictionReasonExpired)
}
previousNode = previousNode.previous
}
}