-
Notifications
You must be signed in to change notification settings - Fork 2
/
lfu_cache.go
276 lines (224 loc) · 5.84 KB
/
lfu_cache.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
package incache
import (
"container/list"
"sync"
"time"
)
// Least Frequently Used Cache
type LFUCache[K comparable, V any] struct {
mu sync.RWMutex
size uint
m map[K]*list.Element
evictionList *list.List
}
func NewLFU[K comparable, V any](size uint) *LFUCache[K, V] {
return &LFUCache[K, V]{
size: size,
m: make(map[K]*list.Element),
evictionList: list.New(),
}
}
type lfuItem[K comparable, V any] struct {
key K
value V
freq uint
expireAt *time.Time
}
// Set adds the key-value pair to the cache.
func (l *LFUCache[K, V]) Set(key K, value V) {
l.mu.Lock()
defer l.mu.Unlock()
l.set(key, value, 0)
}
// SetWithTimeout adds the key-value pair to the cache with a specified expiration time.
func (l *LFUCache[K, V]) SetWithTimeout(key K, value V, exp time.Duration) {
l.mu.Lock()
defer l.mu.Unlock()
l.set(key, value, exp)
}
func (l *LFUCache[K, V]) set(key K, value V, exp time.Duration) {
item, ok := l.m[key]
var tm *time.Time
if exp > 0 {
t := time.Now().Add(exp)
tm = &t
}
if ok {
lfuItem := item.Value.(*lfuItem[K, V])
lfuItem.value = value
lfuItem.expireAt = tm
lfuItem.freq++
l.move(item)
} else {
if len(l.m) == int(l.size) {
l.evict(1)
}
lfuItem := lfuItem[K, V]{
key: key,
value: value,
expireAt: tm,
freq: 1,
}
l.m[key] = l.evictionList.PushBack(&lfuItem)
l.move(l.m[key])
}
}
// Get retrieves the value associated with the given key from the cache.
// If the key is not found or has expired, it returns (zero value of V, false).
// Otherwise, it returns (value, true).
func (l *LFUCache[K, V]) Get(key K) (v V, b bool) {
l.mu.Lock()
defer l.mu.Unlock()
item, ok := l.m[key]
if !ok {
return
}
lfuItem := item.Value.(*lfuItem[K, V])
if lfuItem.expireAt != nil && lfuItem.expireAt.Before(time.Now()) {
l.delete(key, item)
return
}
lfuItem.freq++
l.move(item)
return lfuItem.value, true
}
// NotFoundSet adds the key-value pair to the cache only if the key does not exist.
// It returns true if the key was added to the cache, otherwise false.
func (l *LFUCache[K, V]) NotFoundSet(k K, v V) bool {
l.mu.Lock()
defer l.mu.Unlock()
_, ok := l.m[k]
if ok {
return false
}
l.set(k, v, 0)
return true
}
// NotFoundSetWithTimeout adds the key-value pair to the cache only if the key does not exist.
// It sets an expiration time for the key-value pair.
// It returns true if the key was added to the cache, otherwise false.
func (l *LFUCache[K, V]) NotFoundSetWithTimeout(k K, v V, t time.Duration) bool {
l.mu.Lock()
defer l.mu.Unlock()
_, ok := l.m[k]
if ok {
return false
}
l.set(k, v, t)
return true
}
// GetAll retrieves all key-value pairs from the cache.
// It returns a map containing all the key-value pairs that are not expired.
func (l *LFUCache[K, V]) GetAll() map[K]V {
l.mu.RLock()
defer l.mu.RUnlock()
m := make(map[K]V)
for k, v := range l.m {
if lfuItem := v.Value.(*lfuItem[K, V]); lfuItem.expireAt == nil || !lfuItem.expireAt.Before(time.Now()) {
m[k] = lfuItem.value
}
}
return m
}
// TransferTo transfers all non-expired key-value pairs from the source cache to the destination cache.
func (src *LFUCache[K, V]) TransferTo(dst *LFUCache[K, V]) {
src.mu.Lock()
defer src.mu.Unlock()
for k, v := range src.m {
if lfuItem := v.Value.(*lfuItem[K, V]); lfuItem.expireAt == nil || !lfuItem.expireAt.Before(time.Now()) {
src.delete(k, v)
dst.Set(k, lfuItem.value)
}
}
}
// CopyTo copies all non-expired key-value pairs from the source cache to the destination cache.
func (src *LFUCache[K, V]) CopyTo(dst *LFUCache[K, V]) {
src.mu.RLock()
defer src.mu.RUnlock()
for k, v := range src.m {
if lfuItem := v.Value.(*lfuItem[K, V]); lfuItem.expireAt == nil || !lfuItem.expireAt.Before(time.Now()) {
dst.Set(k, lfuItem.value)
}
}
}
// Keys returns a slice of all keys currently stored in the cache.
// The returned slice does not include expired keys.
// The order of keys in the slice is not guaranteed.
func (l *LFUCache[K, V]) Keys() []K {
l.mu.RLock()
defer l.mu.RUnlock()
keys := make([]K, 0, l.Count())
for k, v := range l.m {
if lfuItem := v.Value.(*lfuItem[K, V]); lfuItem.expireAt == nil || !lfuItem.expireAt.Before(time.Now()) {
keys = append(keys, k)
}
}
return keys
}
// Purge removes all key-value pairs from the cache.
func (l *LFUCache[K, V]) Purge() {
l.mu.Lock()
defer l.mu.Unlock()
l.m = make(map[K]*list.Element)
l.evictionList.Init()
}
// Count returns the number of non-expired key-value pairs currently stored in the cache.
func (l *LFUCache[K, V]) Count() int {
l.mu.RLock()
defer l.mu.RUnlock()
var count int
for _, v := range l.m {
if lfuItem := v.Value.(*lfuItem[K, V]); lfuItem.expireAt == nil || !lfuItem.expireAt.Before(time.Now()) {
count++
}
}
return count
}
// Len returns the number of elements in the cache.
func (l *LFUCache[K, V]) Len() int {
l.mu.RLock()
defer l.mu.RUnlock()
return len(l.m)
}
// Delete removes the key-value pair associated with the given key from the cache.
func (l *LFUCache[K, V]) Delete(k K) {
l.mu.Lock()
defer l.mu.Unlock()
item, ok := l.m[k]
if !ok {
return
}
l.delete(k, item)
}
func (l *LFUCache[K, V]) delete(key K, elem *list.Element) {
delete(l.m, key)
l.evictionList.Remove(elem)
}
func (l *LFUCache[K, V]) evict(n int) {
for i := 0; i < n; i++ {
if b := l.evictionList.Back(); b != nil {
delete(l.m, b.Value.(*lfuItem[K, V]).key)
l.evictionList.Remove(b)
} else {
return
}
}
}
func (l *LFUCache[K, V]) move(elem *list.Element) {
item := elem.Value.(*lfuItem[K, V])
freq := item.freq
curr := elem
for ; curr.Prev() != nil; curr = curr.Prev() {
if freq < curr.Value.(*lfuItem[K, V]).freq {
break
}
}
if curr == elem {
return
}
if curr.Value.(*lfuItem[K, V]).freq == freq {
l.evictionList.MoveToFront(elem)
return
}
l.evictionList.MoveAfter(elem, curr)
}