-
Notifications
You must be signed in to change notification settings - Fork 1
/
lru.go
114 lines (100 loc) · 2.19 KB
/
lru.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
package allcache
import (
"github.com/satmaelstorm/list"
"sync"
)
type LRU[K comparable, T any] struct {
lru *ntsLRU[K, T]
lock sync.Mutex
}
func NewLRU[K comparable, T any](
maxSize uint64,
calcSize SizeCalculator[T],
) Cache[K, T] {
lru := new(LRU[K, T])
lru.lru = newNtsLRU[K, T](maxSize, calcSize)
return lru
}
func (c *LRU[K, T]) Put(key K, item T) {
c.lock.Lock()
defer c.lock.Unlock()
c.lru.put(key, item)
}
func (c *LRU[K, T]) Get(key K, def T) (T, bool) {
c.lock.Lock()
defer c.lock.Unlock()
return c.lru.get(key, def)
}
func (c *LRU[K, T]) Delete(key K) {
c.lock.Lock()
defer c.lock.Unlock()
c.lru.delete(key)
}
//non thread safe LRU
type ntsLRU[K comparable, T any] struct {
items map[K]*list.Node[cacheEntry[K, T]]
evictQueue *list.Queue[cacheEntry[K, T]]
length uint64
maxSize uint64
sizeCalc SizeCalculator[T]
}
func newNtsLRU[K comparable, T any](
maxSize uint64,
sizeCalc SizeCalculator[T],
) *ntsLRU[K, T] {
if nil == sizeCalc {
sizeCalc = func(T) uint64 { return 1 }
}
return &ntsLRU[K, T]{
items: make(map[K]*list.Node[cacheEntry[K, T]], maxSize),
evictQueue: list.NewQueue[cacheEntry[K, T]](),
maxSize: maxSize,
length: 0,
sizeCalc: sizeCalc,
}
}
func (c *ntsLRU[K, T]) put(key K, value T) {
defer c.adjust()
newValue := cacheEntry[K, T]{key, value}
if e, ok := c.items[key]; ok {
c.evictQueue.MoveToBack(e)
oldValue := e.Value().value
e.SetValue(newValue)
c.length += c.sizeCalc(value) - c.sizeCalc(oldValue)
return
}
c.evictQueue.Enqueue(newValue)
c.length += c.sizeCalc(value)
c.items[key] = c.evictQueue.Tail()
return
}
func (c *ntsLRU[K, T]) evict() {
e := c.evictQueue.Dequeue()
if e != nil {
c.remove(e)
}
}
func (c *ntsLRU[K, T]) remove(e *list.Node[cacheEntry[K, T]]) {
delete(c.items, e.Value().key)
c.length -= c.sizeCalc(e.Value().value)
}
func (c *ntsLRU[K, T]) adjust() {
for {
if c.maxSize >= c.length {
return
}
c.evict()
}
}
func (c *ntsLRU[K, T]) get(key K, def T) (T, bool) {
if e, ok := c.items[key]; ok {
c.evictQueue.MoveToBack(e)
return e.Value().value, ok
}
return def, false
}
func (c *ntsLRU[K, T]) delete(key K) {
if e, ok := c.items[key]; ok {
c.remove(e)
}
}