-
Notifications
You must be signed in to change notification settings - Fork 20
/
lru.go
206 lines (167 loc) · 4.01 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
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
package lru
import (
"reflect"
"time"
"github.com/Asutorufa/yuhaiin/pkg/utils/synclist"
"github.com/Asutorufa/yuhaiin/pkg/utils/syncmap"
)
type lruEntry[K, V any] struct {
key K
data V
expire time.Time
}
// LRU Least Recently Used
type LRU[K comparable, V any] struct {
capacity uint
list *synclist.SyncList[*lruEntry[K, V]]
mapping syncmap.SyncMap[K, *synclist.Element[*lruEntry[K, V]]]
reverseMapping syncmap.SyncMap[V, *synclist.Element[*lruEntry[K, V]]]
valueComparable bool
timeout time.Duration
lastPopEntry *lruEntry[K, V]
onRemove func(K, V)
}
type Option[K comparable, V any] func(*LRU[K, V])
func WithOnRemove[K comparable, V any](f func(K, V)) func(*LRU[K, V]) {
return func(l *LRU[K, V]) {
l.onRemove = f
}
}
func WithExpireTimeout[K comparable, V any](t time.Duration) func(*LRU[K, V]) {
return func(l *LRU[K, V]) {
l.timeout = t
}
}
func WithCapacity[K comparable, V any](capacity uint) func(*LRU[K, V]) {
return func(l *LRU[K, V]) {
l.capacity = capacity
}
}
// New create new lru cache
func New[K comparable, V any](options ...Option[K, V]) *LRU[K, V] {
l := &LRU[K, V]{
list: synclist.New[*lruEntry[K, V]](),
}
for _, o := range options {
o(l)
}
var t V
if tp := reflect.TypeOf(t); tp != nil {
l.valueComparable = tp.Comparable()
}
return l
}
func (l *LRU[K, V]) store(v *lruEntry[K, V], le *synclist.Element[*lruEntry[K, V]]) {
l.mapping.Store(v.key, le)
if l.valueComparable {
l.reverseMapping.Store(v.data, le)
}
}
func (l *LRU[K, V]) delete(v *lruEntry[K, V]) {
l.mapping.Delete(v.key)
if l.valueComparable {
l.reverseMapping.Delete(v.data)
}
if l.onRemove != nil {
l.onRemove(v.key, v.data)
}
}
type addOptions struct {
expireTime time.Time
}
type AddOption func(*addOptions)
func WithExpireTimeUnix(t time.Time) AddOption {
return func(o *addOptions) {
o.expireTime = t
}
}
func (l *LRU[K, V]) Add(key K, value V, opts ...AddOption) {
o := &addOptions{}
for _, z := range opts {
z(o)
}
if l.timeout != 0 && o.expireTime.IsZero() {
o.expireTime = time.Now().Add(l.timeout)
}
entry := &lruEntry[K, V]{key, value, o.expireTime}
if elem, ok := l.mapping.Load(key); ok {
l.list.MoveToFront(elem.SetValue(entry))
return
}
var elem *synclist.Element[*lruEntry[K, V]]
if l.capacity == 0 || uint(l.list.Len()) < l.capacity {
elem = l.list.PushFront(entry)
} else {
elem = l.list.Back()
l.lastPopEntry = &lruEntry[K, V]{
key: elem.Value.key,
data: elem.Value.data,
expire: elem.Value.expire,
}
l.delete(elem.Value)
l.list.MoveToFront(elem.SetValue(entry))
}
l.store(entry, elem)
}
// Delete delete a key from cache
func (l *LRU[K, V]) Delete(key K) {
v, ok := l.mapping.LoadAndDelete(key)
if ok {
l.delete(v.Value)
}
}
func (l *LRU[K, V]) load(e *synclist.Element[*lruEntry[K, V]]) *lruEntry[K, V] {
if l.timeout != 0 && time.Now().After(e.Value.expire) {
l.delete(e.Value)
l.list.Remove(e)
return nil
}
l.list.MoveToFront(e)
return e.Value
}
func (l *LRU[K, V]) Load(key K) (v V, ok bool) {
v, _, ok = l.LoadExpireTime(key)
return
}
func (l *LRU[K, V]) LoadExpireTime(key K) (v V, expireTime time.Time, ok bool) {
node, ok := l.mapping.Load(key)
if !ok {
return v, expireTime, false
}
if z := l.load(node); z != nil {
return z.data, z.expire, true
}
return v, expireTime, false
}
func (l *LRU[K, V]) ReverseLoad(v V) (k K, ok bool) {
if !l.valueComparable {
return k, false
}
node, ok := l.reverseMapping.Load(v)
if !ok {
return k, false
}
if z := l.load(node); z != nil {
return z.key, true
}
return k, false
}
func (l *LRU[K, V]) ValueExist(key V) bool {
if !l.valueComparable {
return false
}
_, ok := l.reverseMapping.Load(key)
return ok
}
func (l *LRU[K, V]) LastPopValue() (v V, _ bool) {
if l.lastPopEntry == nil {
return v, false
}
return l.lastPopEntry.data, true
}
func (l *LRU[K, V]) Range(ranger func(K, V)) {
l.mapping.Range(func(key K, value *synclist.Element[*lruEntry[K, V]]) bool {
ranger(key, value.Value.data)
return true
})
}