This repository has been archived by the owner on Apr 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lfucache.go
368 lines (306 loc) · 8.04 KB
/
lfucache.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
package lfucache // import "github.com/calmh/deprecated_lfucache"
import (
"errors"
)
// Cache is an LFU cache structure.
type Cache struct {
capacity int
length int
frequencyList *frequencyNode
index map[interface{}]*node
evictedChans []chan<- interface{}
stats Statistics
}
// Statistics contains current item counts and operation counters.
type Statistics struct {
LenFreq0 int // Number of items at frequency zero, i.e Inserted but not Accessed
Inserts int // Number of Insert()s
Hits int // Number of hits (Access() to item)
Misses int // Number of misses (Access() to non-existant key)
Evictions int // Number of evictions (due to size constraints on Insert(), or EvictIf() calls)
Deletes int // Number of Delete()s.
FreqListLen int // Current length of frequency list, i.e. the number of distinct usage levels
}
// The "frequencyNode" and "node" types make up the two levels of linked lists
// that we use to keep track of the usage per node. It could be argued that we
// should use the built in list type instead of implementing the linked list
// structure again directly in the nodes. I tried that and while it results in
// slightly less code, the extra layer of indirection and resulting type
// assertions make the code less readable. Also the Access() method becomes
// several times slower and requires a heap allocation per call. All in all,
// this was preferable.
type frequencyNode struct {
usage int
prev *frequencyNode
next *frequencyNode
head *node
tail *node // most recently inserted
}
type node struct {
key interface{}
value interface{}
parent *frequencyNode
next *node
prev *node
}
var (
errZeroSizeCache = errors.New("create zero-sized cache")
errEmptyLFU = errors.New("lfu on empty cache")
)
// New initializes a new LFU Cache structure with the specified capacity.
func New(capacity int) *Cache {
if capacity == 0 {
panic(errZeroSizeCache)
}
return &Cache{
capacity: capacity,
index: make(map[interface{}]*node, capacity),
frequencyList: &frequencyNode{},
}
}
// Resize the cache to a new capacity. When shrinking, items may get evicted.
func (c *Cache) Resize(capacity int) {
c.capacity = capacity
for c.length > c.capacity {
c.evict(c.lfu())
}
}
// Insert inserts an item into the cache. If the key already exists, the
// existing item is evicted and the new one inserted. The key type is
// restricted to those acceptable as map keys
// (http://golang.org/ref/spec#Map_types).
func (c *Cache) Insert(key interface{}, value interface{}) {
if debug {
c.check()
}
if n, ok := c.index[key]; ok {
c.evict(n)
}
if c.length == c.capacity {
c.evict(c.lfu())
}
n := &node{key: key, value: value}
c.index[key] = n
c.moveNodeToFn(n, c.frequencyList)
c.length++
c.stats.Inserts++
if debug {
c.check()
}
}
// Delete deletes an item from the cache and returns true. Does nothing and
// returns false if the key was not present in the cache.
func (c *Cache) Delete(key interface{}) bool {
if debug {
c.check()
}
n, ok := c.index[key]
if ok {
c.deleteNode(n)
c.stats.Deletes++
}
if debug {
c.check()
}
return ok
}
// Access an item in the cache. Returns "value, ok" similar to map indexing.
// Increases the item's use count.
func (c *Cache) Access(key interface{}) (interface{}, bool) {
if debug {
c.check()
}
n, ok := c.index[key]
if !ok {
c.stats.Misses++
return nil, false
}
nextUsage := n.parent.usage + 1
var nextFn *frequencyNode
if n.parent.next == nil || n.parent.next.usage != nextUsage {
nextFn = c.newFrequencyNode(nextUsage, n.parent)
} else {
nextFn = n.parent.next
}
c.moveNodeToFn(n, nextFn)
c.stats.Hits++
if debug {
c.check()
}
return n.value, true
}
// Len returns the number of items currently stored in the cache.
func (c *Cache) Len() int {
return c.length
}
// Cap returns the maximum number of items the cache will hold.
func (c *Cache) Cap() int {
return c.capacity
}
// Statistics returns the cache statistics.
func (c *Cache) Statistics() Statistics {
if debug {
c.check()
}
c.stats.LenFreq0 = c.items0()
c.stats.FreqListLen = c.numFrequencyNodes()
return c.stats
}
// Evictions registers a channel used to report items that get evicted from
// the cache. Only items evicted due to LFU or EvictIf() will be sent on the
// channel, not items removed by calling Delete(). The channel must be
// unregistered using UnregisterEvictions() prior to ceasing reads in order to
// avoid deadlocking evictions.
func (c *Cache) Evictions(e chan<- interface{}) {
if debug {
c.check()
}
c.evictedChans = append(c.evictedChans, e)
}
// UnregisterEvictions removes the channel from the list of channels to be
// notified on item eviction. Must be called when there is no longer a reader
// for the channel in question.
func (c *Cache) UnregisterEvictions(e chan<- interface{}) {
if debug {
c.check()
}
var i int
var found bool
for i = range c.evictedChans {
if c.evictedChans[i] == e {
found = true
break
}
}
if found {
copy(c.evictedChans[i:], c.evictedChans[i+1:])
c.evictedChans[len(c.evictedChans)-1] = nil
c.evictedChans = c.evictedChans[:len(c.evictedChans)-1]
}
}
// EvictIf applies test to each item in the cache and evicts it if the test
// returns true. Returns the number of items that were evicted.
func (c *Cache) EvictIf(test func(interface{}) bool) int {
if debug {
c.check()
}
cnt := 0
for _, n := range c.index {
if test(n.value) {
c.evict(n)
cnt++
}
}
if debug {
c.check()
}
return cnt
}
// evict evicts a node from the cache by removing it from the structure and
// notifying any interested eviction listeners
func (c *Cache) evict(n *node) {
for i := range c.evictedChans {
c.evictedChans[i] <- n.value
}
c.deleteNode(n)
c.stats.Evictions++
}
// deleteNode deletes a node from the cache, also deleting the frequency node
// if it became empty
func (c *Cache) deleteNode(n *node) {
if n.prev != nil {
n.prev.next = n.next
}
if n.next != nil {
n.next.prev = n.prev
}
fn := n.parent
if fn.head == n {
fn.head = n.next
}
if fn.tail == n {
fn.tail = n.prev
}
if fn.usage != 0 && fn.head == nil {
c.deleteFrequencyNode(fn)
}
delete(c.index, n.key)
c.length--
}
// lfu returns the least frequently used node in the cache, prefering the
// oldest if there are multiple nodes with the same lowest usage count
func (c *Cache) lfu() *node {
for fn := c.frequencyList; fn != nil; fn = fn.next {
if fn.head != nil {
return fn.head
}
}
panic(errEmptyLFU)
}
// newFrequencyNode inserts a new frequency node after the specified prev node
func (c *Cache) newFrequencyNode(usage int, prev *frequencyNode) *frequencyNode {
fn := &frequencyNode{
usage: usage,
prev: prev,
next: prev.next,
}
if fn.next != nil {
fn.next.prev = fn
}
prev.next = fn
return fn
}
// deleteFrequencyNode removes a new frequency node from the list
func (c *Cache) deleteFrequencyNode(fn *frequencyNode) {
if fn.next != nil {
fn.next.prev = fn.prev
}
fn.prev.next = fn.next
}
// moveNodeToFn moves a node to become a child of a frequency node, while
// properly removing it from any current frequency node
func (c *Cache) moveNodeToFn(n *node, fn *frequencyNode) {
if n.prev != nil {
n.prev.next = n.next
}
if n.next != nil {
n.next.prev = n.prev
}
if n.parent != nil {
if n.parent.head == n {
n.parent.head = n.next
}
if n.parent.tail == n {
n.parent.tail = n.prev
}
if n.parent.head == nil && n.parent.usage != 0 {
c.deleteFrequencyNode(n.parent)
}
}
n.prev = nil
n.next = nil
n.parent = fn
if fn.tail != nil {
n.prev = fn.tail
n.prev.next = n
}
if fn.head == nil {
fn.head = n
}
fn.tail = n
}
// items0 returns the number of items at the head of the node list (usage
// count zero)
func (c *Cache) items0() (count int) {
for n := c.frequencyList.head; n != nil; n = n.next {
count++
}
return
}
// numFrequencyNodes returns the number of frequency nodes in the cache
func (c *Cache) numFrequencyNodes() (count int) {
for fn := c.frequencyList; fn != nil; fn = fn.next {
count++
}
return
}