forked from gravityslave92/dscache
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlrucache.go
321 lines (279 loc) · 6.8 KB
/
lrucache.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
package dscache
import (
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"unsafe"
)
// Node Structure for Doubly Linked List
type node struct {
key string
payload string
previous, next *node
size uint64
validTill time.Time
}
// LRUCache structure
type lrucache struct {
mu sync.Mutex
keys map[string]*node
listStart *node
listEnd *node
size uint64
maxsize uint64
workerSleep time.Duration
nodeBaseSize uint64
NumEvictions uint64
}
// ErrMaxsize Used when a key + payload is bigger than allowed LRU Cache size
var ErrMaxsize = errors.New("Value is Bigger than Allowed Maxsize")
// newLRUCache Constructor
func newLRUCache(maxsize uint64, workerSleep time.Duration) *lrucache {
lru := new(lrucache)
lru.keys = make(map[string]*node)
lru.size = 0
lru.maxsize = maxsize
lru.workerSleep = workerSleep
lru.nodeBaseSize = lru.calculateBaseNodeSize()
go lru.worker()
return lru
}
// set an element
func (lru *lrucache) set(key, payload string, expires time.Duration) error {
// Verify Size
nodeSize := (uint64(len(key)) + uint64(len(payload))) + lru.nodeBaseSize // Size of node structure is 8
if nodeSize > lru.maxsize {
// Node Exceeds Maxsize
return ErrMaxsize
}
lru.mu.Lock()
defer lru.mu.Unlock()
// Check to see if it was already set
old, ok := lru.keys[key]
if ok {
// Key exists
oldSize := old.size
old.payload = payload
old.size = nodeSize
old.validTill = time.Now().Add(expires)
diff := int64(nodeSize) - int64(oldSize)
if diff > 0 {
atomic.AddUint64(&lru.size, uint64(diff))
} else {
atomic.AddUint64(&lru.size, ^uint64(diff-1))
}
lru.sendToTop(old)
} else {
// create and add Node
n := new(node)
n.key = key
n.payload = payload
n.size = nodeSize
n.validTill = time.Now().Add(expires)
lru.keys[key] = n
atomic.AddUint64(&lru.size, nodeSize)
lru.sendToTop(n)
}
if lru.size > lru.maxsize {
lru.resize()
}
return nil
}
// get an element
func (lru *lrucache) get(key string) (string, bool) {
lru.mu.Lock()
defer lru.mu.Unlock()
n, ok := lru.keys[key]
if !ok {
// It doesn't exist
return "", false
}
if n.validTill.Before(time.Now()) {
// It has expired
lru.delete(n)
return "", false
}
lru.sendToTop(n)
return n.payload, true
}
func (lru *lrucache) purge(key string) bool {
lru.mu.Lock()
defer lru.mu.Unlock()
n, ok := lru.keys[key]
if !ok {
return false
}
lru.delete(n)
return true
}
// worker Expiration worker
//
// Expriration Workers go from the bottom of the list to the top
// And delete all elements that have expired.
// Then they wait for the configured time before starting again.
func (lru *lrucache) worker() {
for {
lru.mu.Lock()
end := lru.listEnd
lru.mu.Unlock()
for end != nil {
if end.validTill.Before(time.Now()) {
lru.mu.Lock()
if end != nil {
nend := end.previous
lru.delete(end)
end = nend
}
lru.mu.Unlock()
} else {
lru.mu.Lock()
end = end.previous
lru.mu.Unlock()
}
}
time.Sleep(lru.workerSleep)
}
}
// sendToTop promote node to top of list
func (lru *lrucache) sendToTop(n *node) {
var listStart = lru.listStart
if listStart == nil {
lru.listStart = n
lru.listEnd = n
return
}
if listStart == n {
return
}
if n.previous != nil {
n.previous.next = n.next
if n.next != nil {
n.next.previous = n.previous
}
}
if lru.listEnd == n {
lru.listEnd = n.previous
}
n.next = listStart
n.previous = nil
listStart.previous = n
lru.listStart = n
}
// resize Resise list by size from the bottom
func (lru *lrucache) resize() {
if lru.size > lru.maxsize {
// Shrink lisk
for lru.size > lru.maxsize {
end := lru.listEnd
lru.delete(end)
}
}
}
// delete Delete node
func (lru *lrucache) delete(n *node) {
if n.next != nil {
n.next.previous = n.previous
}
if n.previous != nil {
n.previous.next = n.next
}
if n == lru.listStart {
lru.listStart = n.next
}
if n == lru.listEnd {
lru.listEnd = n.previous
}
n.previous = nil
n.next = nil
// Test if it's in the keys. (Function might have been called from worker after it has been deleted by another goroutine
// since worker does not lock the structure all the time. The following situation is pausible: A node is selected by worker
// in it's iterations, the lock is released, another routine locks and then deletes that node, then worker finds out the node
// has expired, locks and tries to delete it again, decrementing lru.size 2 times)
if _, ok := lru.keys[n.key]; ok {
delete(lru.keys, n.key)
atomic.AddUint64(&lru.size, ^uint64(n.size-1))
atomic.AddUint64(&lru.NumEvictions, 1)
}
}
// calculateBaseNodeSize Calculate the Byte Size of a single Node
func (lru *lrucache) calculateBaseNodeSize() uint64 {
n := new(node)
size := uint64(unsafe.Sizeof(n.key)) + uint64(unsafe.Sizeof(n.payload)) + uint64(unsafe.Sizeof(n.previous)) + uint64(unsafe.Sizeof(n.next)) + uint64(unsafe.Sizeof(n.size)) + uint64(unsafe.Sizeof(n.validTill))
return size
}
// verifyEndAndStart testing function
//
// For Concurrent tests.
// Verifies that list is the same from listStart to listEnd
func (lru *lrucache) verifyEndAndStart() error {
lru.mu.Lock()
defer lru.mu.Unlock()
start := lru.listStart
if start != nil {
// Get to last element of start
for start.next != nil {
start = start.next
}
end := lru.listEnd
// Compare them
for start.previous != nil {
if end != start {
return errors.New("listStart does not match order of listEnd")
}
end = end.previous
start = start.previous
}
}
return nil
}
// verifyUniqueKey testing function
//
// For Concurrent tests.
// Verifies that list has all unique keys
func (lru *lrucache) verifyUniqueKeys() error {
lru.mu.Lock()
defer lru.mu.Unlock()
test := make(map[string]bool)
start := lru.listStart
for start != nil {
_, ok := test[start.key]
if !ok {
test[start.key] = true
} else {
return errors.New("Duplicated Key in listStart")
}
start = start.next
}
return nil
}
// verifySize testing function
//
// For Concurrent tests.
// Verifies that list size is consistent with actual size
func (lru *lrucache) verifySize() error {
lru.mu.Lock()
defer lru.mu.Unlock()
start := lru.listStart
realSize := uint64(0)
sumSize := uint64(0)
if start != nil {
// Get to last element of start
for start.next != nil {
realSize += uint64(len(start.key)) + uint64(len(start.payload)) + lru.calculateBaseNodeSize()
sumSize += start.size
start = start.next
}
// Compare them
if realSize > lru.maxsize {
err := fmt.Sprintf("realSize: %v > maxsize: %v --- size: %v --sumSize: %v", realSize, lru.maxsize, lru.size, sumSize)
return errors.New(err)
}
if sumSize > lru.maxsize {
err := fmt.Sprintf("sumSize: %v > maxsize: %v --- size: %v ---realSize: %v", sumSize, lru.maxsize, lru.size, realSize)
return errors.New(err)
}
}
return nil
}