forked from dgryski/go-tinylfu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s2lru.go
124 lines (94 loc) · 2.33 KB
/
s2lru.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
package tinylfu
import "container/list"
type slruItem struct {
listid int
key string
value interface{}
keyh uint64
}
// Cache is an LRU cache. It is not safe for concurrent access.
type slruCache struct {
data map[string]*list.Element
onecap, twocap int
one, two *list.List
}
func newSLRU(onecap, twocap int, data map[string]*list.Element) *slruCache {
return &slruCache{
data: data,
onecap: onecap,
one: list.New(),
twocap: twocap,
two: list.New(),
}
}
// get updates the cache data structures for a get
func (slru *slruCache) get(v *list.Element) {
item := v.Value.(*slruItem)
// already on list two?
if item.listid == 2 {
slru.two.MoveToFront(v)
return
}
// must be list one
// is there space on the next list?
if slru.two.Len() < slru.twocap {
// just do the remove/add
slru.one.Remove(v)
item.listid = 2
slru.data[item.key] = slru.two.PushFront(item)
return
}
back := slru.two.Back()
bitem := back.Value.(*slruItem)
// swap the key/values
*bitem, *item = *item, *bitem
bitem.listid = 2
item.listid = 1
// update pointers in the map
slru.data[item.key] = v
slru.data[bitem.key] = back
// move the elements to the front of their lists
slru.one.MoveToFront(v)
slru.two.MoveToFront(back)
}
// Set sets a value in the cache
func (slru *slruCache) add(newitem slruItem) {
newitem.listid = 1
if slru.one.Len() < slru.onecap || (slru.Len() < slru.onecap+slru.twocap) {
slru.data[newitem.key] = slru.one.PushFront(&newitem)
return
}
// reuse the tail item
e := slru.one.Back()
item := e.Value.(*slruItem)
delete(slru.data, item.key)
*item = newitem
slru.data[item.key] = e
slru.one.MoveToFront(e)
}
func (slru *slruCache) victim() *slruItem {
if slru.Len() < slru.onecap+slru.twocap {
return nil
}
v := slru.one.Back()
return v.Value.(*slruItem)
}
// Len returns the total number of items in the cache
func (slru *slruCache) Len() int {
return slru.one.Len() + slru.two.Len()
}
// Remove removes an item from the cache, returning the item and a boolean indicating if it was found
func (slru *slruCache) Remove(key string) (interface{}, bool) {
v, ok := slru.data[key]
if !ok {
return nil, false
}
item := v.Value.(*slruItem)
if item.listid == 2 {
slru.two.Remove(v)
} else {
slru.one.Remove(v)
}
delete(slru.data, key)
return item.value, true
}