-
Notifications
You must be signed in to change notification settings - Fork 0
/
arc.go
371 lines (300 loc) · 6.88 KB
/
arc.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
package gcache
import (
"container/list"
"errors"
"time"
)
// Constantly balances between LRU and LFU, to improve the combined result.
type ARC struct {
baseCache
store map[interface{}]*arcItem
// t1 ("tier-1") is an internal cache tracking "recency"
t1 *list.List
// t2 ("tier-2") is an internal cache tracking "frequency"
t2 *list.List
// Ghost lists for t1 and t2 respectively: tracks recently evicted keys.
b1 *list.List
b2 *list.List
// split is a tuning parameter used by the cache to optimize hit rate for
// recency or frequency, depending on the workload.
// It's sometime referred as the "learning parameter" of the cache.
split int
}
type arcItem struct {
key interface{}
value interface{}
parent *list.List
element *list.Element
ghost bool
}
func (c *ARC) init() {
c.store = make(map[interface{}]*arcItem, c.capacity)
c.t1 = list.New()
c.t2 = list.New()
c.b1 = list.New()
c.b2 = list.New()
}
func newARC(cb *CacheBuilder) *ARC {
c := &ARC{}
buildCache(&c.baseCache, cb)
c.loadGroup.cache = c
c.init()
return c
}
func (c *ARC) set(key, value interface{}) (interface{}, error) {
var err error
if c.serializeFunc != nil {
value, err = c.serializeFunc(key, value)
if err != nil {
return nil, err
}
}
if c.addedFunc != nil {
defer c.addedFunc(key, value)
}
entry, exists := c.store[key]
if !exists {
c.size++
entry = &arcItem{
key: key,
value: value,
}
c.request(entry)
c.store[key] = entry
return entry, nil
}
if entry.ghost {
c.size++
}
entry.value = value
entry.ghost = false
c.request(entry)
return entry, nil
}
func (c *ARC) Set(key, value interface{}) error {
c.mu.Lock()
defer c.mu.Unlock()
_, err := c.set(key, value)
return err
}
func (c *ARC) SetWithExpire(key, value interface{}, expiration time.Duration) error {
return c.Set(key, value)
}
func (c *ARC) get(key interface{}, onLoad bool) (interface{}, error) {
entry, exists := c.store[key]
if !exists {
// Ugly. This needs to go.
if !onLoad {
c.stats.IncrMissCount()
}
return nil, KeyNotFoundError
}
c.request(entry)
if c.deserializeFunc != nil {
return c.deserializeFunc(key, entry.value)
}
return entry.value, nil
}
func (c *ARC) getWithLoader(key interface{}, isWait bool) (interface{}, error) {
if c.loaderExpireFunc == nil {
return nil, KeyNotFoundError
}
value, _, err := c.load(key, func(v interface{}, expiration *time.Duration, e error) (interface{}, error) {
if e != nil {
return nil, e
}
err := c.Set(key, v)
if err != nil {
return nil, err
}
return v, nil
}, isWait)
if err != nil {
return nil, err
}
return value, nil
}
func (c *ARC) Get(key interface{}) (interface{}, error) {
c.mu.Lock()
v, err := c.get(key, false)
c.mu.Unlock()
if err == KeyNotFoundError {
return c.getWithLoader(key, true)
}
return v, err
}
func (c *ARC) GetIFPresent(key interface{}) (interface{}, error) {
c.mu.Lock()
defer c.mu.Unlock()
v, err := c.get(key, false)
if err == KeyNotFoundError {
return c.getWithLoader(key, false)
}
return v, err
}
func (c *ARC) cached() []interface{} {
cached := make([]interface{}, 0)
for e := c.t1.Front(); e != nil; e = e.Next() {
cached = append(cached, e.Value.(*arcItem))
}
for e := c.t2.Front(); e != nil; e = e.Next() {
cached = append(cached, e.Value.(*arcItem))
}
return cached
}
func (c *ARC) GetALL() map[interface{}]interface{} {
c.mu.Lock()
defer c.mu.Unlock()
storeActual := make(map[interface{}]interface{})
cached := c.cached()
for _, elt := range cached {
storeActual[elt.(*arcItem).key] = elt.(*arcItem).value
}
return storeActual
}
func (c *ARC) remove(key interface{}) error {
elt, exists := c.store[key]
if !exists {
return KeyNotFoundError
}
if elt.parent != nil {
elt.parent.Remove(elt.element)
}
if c.evictedFunc != nil {
c.evictedFunc(key, elt.value)
}
delete(c.store, elt.key)
c.size--
return nil
}
func (c *ARC) Remove(key interface{}) error {
c.mu.Lock()
defer c.mu.Unlock()
return c.remove(key)
}
func (c *ARC) Purge() {
c.mu.Lock()
defer c.mu.Unlock()
if c.purgeVisitorFunc != nil {
for elt := c.t1.Front(); elt != nil; elt = elt.Next() {
entry := elt.Value.(*arcItem)
c.purgeVisitorFunc(entry.key, entry.value)
}
for elt := c.t2.Front(); elt != nil; elt = elt.Next() {
entry := elt.Value.(*arcItem)
c.purgeVisitorFunc(entry.key, entry.value)
}
}
c.init()
}
func (c *ARC) Keys() []interface{} {
c.mu.Lock()
defer c.mu.Unlock()
cached := c.cached()
keys := make([]interface{}, 0)
for _, elt := range cached {
keys = append(keys, elt.(*arcItem).key)
}
return keys
}
func (c *ARC) Len() int {
return c.size
}
func (c *ARC) removeLRU(l *list.List) {
lru := l.Back()
if c.evictedFunc != nil {
defer c.evictedFunc(lru.Value.(*arcItem).key, lru.Value.(*arcItem).value)
}
l.Remove(lru)
delete(c.store, lru.Value.(*arcItem).key)
c.size--
}
func (c *ARC) request(e *arcItem) error {
var delta int
if e.parent == c.t1 || e.parent == c.t2 {
c.stats.IncrHitCount()
e.setMRU(c.t2)
return nil
}
// Note: I find this way of doing things really ugly. I am not happy with that.
// For now, we will stick to the specs but there is a refactor to be done here.
if e.parent == c.b1 {
if c.b1.Len() >= c.b2.Len() {
delta = 1
} else {
delta = c.b2.Len() / c.b1.Len()
}
c.split = minInt(c.split+delta, c.capacity)
c.replace(e)
e.setMRU(c.t2)
return nil
}
if e.parent == c.b2 {
if c.b2.Len() >= c.b1.Len() {
delta = 1
} else {
delta = c.b1.Len() / c.b2.Len()
}
c.split = maxInt(c.split-delta, 0)
c.replace(e)
e.setMRU(c.t2)
return nil
}
if e.parent != nil {
return errors.New("ARC/internal/request: unknown internal cache type")
}
l1Len := c.t1.Len() + c.b1.Len()
l2Len := c.t2.Len() + c.b2.Len()
if l1Len == c.capacity {
if c.t1.Len() < c.capacity {
c.removeLRU(c.b1)
c.replace(e)
} else {
c.removeLRU(c.t1)
}
} else {
if l1Len+l2Len >= c.capacity {
if l1Len+l2Len == 2*c.capacity {
c.removeLRU(c.b2)
}
c.replace(e)
}
}
e.setMRU(c.t1)
return nil
}
func (e *arcItem) setMRU(l *list.List) {
if e.parent != nil {
e.parent.Remove(e.element)
}
e.parent = l
e.element = e.parent.PushFront(e)
}
func (c *ARC) replace(e *arcItem) {
var lru *arcItem
var target *list.List
if c.t1.Len() > 0 && (c.t1.Len() > c.split) || (e.parent == c.b2 && c.t1.Len() == c.split) {
lru = c.t1.Back().Value.(*arcItem)
target = c.b1
} else {
lru = c.t2.Back().Value.(*arcItem)
target = c.b2
}
if c.evictedFunc != nil {
defer c.evictedFunc(lru.key, lru.value)
}
lru.value = nil
lru.ghost = true
lru.setMRU(target)
c.size--
}
func (c *ARC) Debug() map[string][]int {
d := make(map[string][]int)
d["arc"] = []int{len(c.store), c.split, c.t1.Len(), c.b1.Len(), c.t2.Len(), c.b2.Len()}
return d
}
func (c *ARC) unsafeGet(key interface{}, onLoad bool) (interface{}, error) {
c.mu.Lock()
defer c.mu.Unlock()
return c.get(key, onLoad)
}