-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru.go
executable file
·190 lines (170 loc) · 3.55 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
// Copyright (c) 2021 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
// Package lru implements an LRU cache.
package lru
// Reference represents the reference counter.
type Reference interface {
Done()
}
type node struct {
key interface{}
value interface{}
cost int
prev *node
next *node
free Free
counter int64
}
type reference struct {
n *node
step int64
}
func (n *node) newReference() Reference {
return &reference{n, 1}
}
// Done decrements the reference counter by one.
func (r *reference) Done() {
r.n.counter -= r.step
r.step = 0
if r.n.free != nil && r.n.counter < 0 {
r.n.free(r.n.key, r.n.value)
}
}
// Free is a free callback.
type Free func(key, value interface{})
// LRU represents an LRU cache.
type LRU struct {
nodes map[interface{}]*node
root *node
capacity int
cost int
free Free
}
// New returns a new LRU cache. LRU is not thread safe.
func New(capacity int, free Free) *LRU {
if capacity <= 0 {
panic("non-positive capacity")
}
l := &LRU{
nodes: make(map[interface{}]*node),
root: &node{},
capacity: capacity,
free: free,
}
l.Reset()
return l
}
// Reset resets the LRU cache.
func (l *LRU) Reset() {
for _, n := range l.nodes {
l.delete(n)
}
l.root.next = l.root
l.root.prev = l.root
}
// Resize sets the LRU cache capacity.
func (l *LRU) Resize(capacity int) {
if capacity <= 0 {
panic("non-positive capacity")
}
l.capacity = capacity
for l.cost > l.capacity {
back := l.root.prev
l.delete(back)
}
}
// Size returns the LRU cache size
func (l *LRU) Size() int {
return l.cost
}
// Set sets the value and increments the reference counter by one for a key.
func (l *LRU) Set(key, value interface{}, cost int) (reference Reference, ok bool) {
var n = &node{key: key, value: value, cost: cost, free: l.free}
if cost > l.capacity {
return n.newReference(), false
}
if old, ok := l.nodes[key]; ok {
var removed bool
var oldCost = old.cost
for l.cost-oldCost+cost > l.capacity {
back := l.root.prev
if old == back {
removed = true
oldCost = 0
}
l.delete(back)
}
l.nodes[key] = n
if removed {
l.insert(n, l.root)
} else {
l.replace(old, n)
old.newReference().Done()
if n != l.root.next {
l.move(n, l.root)
}
}
l.cost = l.cost - oldCost + cost
} else {
for l.cost+cost > l.capacity {
back := l.root.prev
l.delete(back)
}
l.nodes[key] = n
l.insert(n, l.root)
l.cost += cost
}
n.counter++
return n.newReference(), true
}
// Remove removes the value for a key.
func (l *LRU) Remove(key interface{}) (ok bool) {
var n *node
if n, ok = l.nodes[key]; ok {
l.delete(n)
}
return
}
// Get returns the value and increments the reference counter by one for a key.
func (l *LRU) Get(key interface{}) (value interface{}, reference Reference, ok bool) {
var n *node
if n, ok = l.nodes[key]; ok {
if n != l.root.next {
l.move(n, l.root)
}
value = n.value
n.counter++
reference = n.newReference()
}
return
}
func (l *LRU) delete(n *node) {
delete(l.nodes, n.key)
l.remove(n)
n.newReference().Done()
l.cost -= n.cost
}
func (l *LRU) move(n, at *node) {
if n != at {
l.remove(n)
l.insert(n, at)
}
}
func (l *LRU) insert(n, at *node) {
n.prev = at
n.next = at.next
n.prev.next = n
n.next.prev = n
}
func (l *LRU) remove(n *node) {
n.prev.next = n.next
n.next.prev = n.prev
n.next = nil
n.prev = nil
}
func (l *LRU) replace(old, new *node) {
new.next = old.next
new.next.prev = new
new.prev = old.prev
new.prev.next = new
}