Repository navigation
Expand file tree
/
Copy pathhashlru.go
More file actions
359 lines (253 loc) · 6.3 KB
/
Copy pathhashlru.go
File metadata and controls
359 lines (253 loc) · 6.3 KB
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
package hlru
import (
"errors"
"math"
"sync"
)
/*
The HashLRU algorithm maintains two separate maps
and bulk eviction happens only after both the maps fill up
Hence the the callBack function is triggered in bulk and
is not an accurate measure. Use NewWithEvict() with caution.
*/
type HashLRU struct {
maxSize int
size int
oldCache, newCache map[interface{}]interface{}
onEvictedCB func(key, value interface{})
lock sync.RWMutex
}
type KVPair struct {
key, value interface{}
}
// Returns a new hashlru instance
func NewHLRU(maxSize int) (*HashLRU, error) {
return NewWithEvict(maxSize, nil)
}
func NewWithEvict(maxSize int, onEvict func(key, value interface{})) (*HashLRU, error) {
if maxSize <= 0 {
return nil, errors.New("Size must be a postive int")
}
lru := &HashLRU{
maxSize: maxSize,
size: 0,
onEvictedCB: onEvict,
oldCache: make(map[interface{}]interface{}),
newCache: make(map[interface{}]interface{}),
}
return lru, nil
}
/*
update(key, value interface{}) is used internally in Get() and Set()
to impose least recently by pushing all recently accessed keys to the newCache
and the oldCache acts as a back up dump once newCache fills up.
Bulk eviction takes place from the oldCache
*/
func (lru *HashLRU) update(key, value interface{}) {
lru.newCache[key] = value
lru.size++
if lru.size >= lru.maxSize {
lru.size = 0
if lru.onEvictedCB != nil {
for key, value := range lru.oldCache {
lru.onEvictedCB(key, value)
}
}
lru.oldCache = make(map[interface{}]interface{})
for key, value := range lru.newCache {
lru.oldCache[key] = value
}
lru.newCache = make(map[interface{}]interface{})
}
}
// Set a value and update the cache
func (lru *HashLRU) Set(key, value interface{}) {
lru.lock.Lock()
if _, found := lru.newCache[key]; found {
lru.newCache[key] = value
} else {
lru.update(key, value)
}
lru.lock.Unlock()
}
// Get a value and update the cache
func (lru *HashLRU) Get(key interface{}) (interface{}, bool) {
lru.lock.Lock()
if value, found := lru.newCache[key]; found {
lru.lock.Unlock()
return value, found
}
if value, found := lru.oldCache[key]; found {
delete(lru.oldCache, key)
lru.update(key, value)
lru.lock.Unlock()
return value, found
}
lru.lock.Unlock()
return nil, false
}
// Peek the value of a key without updating the cache
func (lru *HashLRU) Peek(key interface{}) (interface{}, bool) {
lru.lock.RLock()
if value, found := lru.newCache[key]; found {
lru.lock.RUnlock()
return value, found
}
if value, found := lru.oldCache[key]; found {
lru.lock.RUnlock()
return value, found
}
lru.lock.RUnlock()
return nil, false
}
// Checks if a key exists in cache
func (lru *HashLRU) Has(key interface{}) bool {
lru.lock.RLock()
_, cacheNew := lru.newCache[key]
_, cacheOld := lru.oldCache[key]
lru.lock.RUnlock()
return cacheNew || cacheOld
}
// Removes a key from the cache
func (lru *HashLRU) Remove(key interface{}) bool {
lru.lock.Lock()
if val, found := lru.newCache[key]; found {
delete(lru.newCache, key)
lru.size--
if lru.onEvictedCB != nil {
lru.onEvictedCB(key, val)
}
lru.lock.Unlock()
return true
}
if val, found := lru.oldCache[key]; found {
delete(lru.oldCache, key)
if lru.onEvictedCB != nil {
lru.onEvictedCB(key, val)
}
lru.lock.Unlock()
return true
}
lru.lock.Unlock()
return false
}
// Returns the number of items in the cache.
func (lru *HashLRU) Len() int {
lru.lock.RLock()
if lru.size == 0 {
lru.lock.RUnlock()
return len(lru.oldCache)
}
oldCacheSize := 0
for key, _ := range lru.oldCache {
if _, found := lru.newCache[key]; !found {
oldCacheSize++
}
}
lru.lock.RUnlock()
return int(math.Min(float64(lru.size+oldCacheSize), float64(lru.maxSize)))
}
// Clears all entries.
func (lru *HashLRU) Clear() {
lru.lock.Lock()
if lru.onEvictedCB != nil {
for key, value := range lru.oldCache {
lru.onEvictedCB(key, value)
}
for key, value := range lru.newCache {
lru.onEvictedCB(key, value)
}
}
lru.oldCache = make(map[interface{}]interface{})
lru.newCache = make(map[interface{}]interface{})
lru.size = 0
lru.lock.Unlock()
}
func (lru *HashLRU) Keys() []interface{} {
lru.lock.RLock()
tempKeys := make([]interface{}, 0)
for key, _ := range lru.oldCache {
// tempKeys = append(tempKeys, key)
if _, found := lru.newCache[key]; !found {
tempKeys = append(tempKeys, key)
}
}
for key, _ := range lru.newCache {
tempKeys = append(tempKeys, key)
}
lru.lock.RUnlock()
return tempKeys
}
func (lru *HashLRU) Vals() []interface{} {
lru.lock.RLock()
tempVals := make([]interface{}, 0)
for key, value := range lru.oldCache {
if _, found := lru.newCache[key]; !found {
tempVals = append(tempVals, value)
}
}
for _, value := range lru.newCache {
tempVals = append(tempVals, value)
}
lru.lock.RUnlock()
return tempVals
}
func (lru *HashLRU) all() []*KVPair {
lru.lock.RLock()
allPairs := []*KVPair{}
for key, value := range lru.oldCache {
if _, found := lru.newCache[key]; !found {
kvPair := new(KVPair)
kvPair.key = key
kvPair.value = value
allPairs = append(allPairs, kvPair)
}
}
for key, value := range lru.newCache {
kvPair := new(KVPair)
kvPair.key = key
kvPair.value = value
allPairs = append(allPairs, kvPair)
}
lru.lock.RUnlock()
return allPairs
}
// Resizes cache, returning number of items deleted
func (lru *HashLRU) Resize(newSize int) (int, error) {
if newSize <= 0 {
return 0, errors.New("Size must be a postive int")
}
totalItems := lru.Len()
removeCount := totalItems - newSize
if removeCount < 0 {
lru.lock.Lock()
for key, value := range lru.oldCache {
lru.newCache[key] = value
}
lru.oldCache = make(map[interface{}]interface{})
lru.size = totalItems
lru.maxSize = newSize
lru.lock.Unlock()
return 0, nil
} else {
allPairs := lru.all()
lru.lock.Lock()
lru.oldCache = make(map[interface{}]interface{})
lru.newCache = make(map[interface{}]interface{})
lru.size = 0
lru.maxSize = newSize
var i = 0
for i < removeCount {
if lru.onEvictedCB != nil {
lru.onEvictedCB(allPairs[i].key, allPairs[i].value)
}
i++
}
for i < len(allPairs) {
lru.oldCache[allPairs[i].key] = allPairs[i].value
i++
}
lru.lock.Unlock()
return removeCount, nil
}
}