-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathCLRUCache.go
176 lines (158 loc) · 3.04 KB
/
CLRUCache.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
package template
import (
"container/list"
"hash/fnv"
"sync"
)
type command int
const (
// MoveToFront define
MoveToFront command = iota
// PushFront define
PushFront
// Delete define
Delete
)
type clear struct {
done chan struct{}
}
// CLRUCache define: High Concurrency LRUCache
type CLRUCache struct {
sync.RWMutex
cap int
list *list.List
buckets []*bucket
bucketMask uint32
deletePairs chan *list.Element
movePairs chan *list.Element
control chan interface{}
}
// Pair define
type Pair struct {
key string
value interface{}
cmd command
}
// New define
func New(capacity int) *CLRUCache {
c := &CLRUCache{
cap: capacity,
list: list.New(),
bucketMask: uint32(1024) - 1,
buckets: make([]*bucket, 1024),
}
for i := 0; i < 1024; i++ {
c.buckets[i] = &bucket{
keys: make(map[string]*list.Element),
}
}
c.restart()
return c
}
// Get define
func (c *CLRUCache) Get(key string) interface{} {
el := c.bucket(key).get(key)
if el == nil {
return nil
}
c.move(el)
return el.Value.(Pair).value
}
// Put define
func (c *CLRUCache) Put(key string, value interface{}) {
el, exist := c.bucket(key).set(key, value)
if exist != nil {
c.deletePairs <- exist
}
c.move(el)
}
func (c *CLRUCache) move(el *list.Element) {
select {
case c.movePairs <- el:
default:
}
}
// Delete define
func (c *CLRUCache) Delete(key string) bool {
el := c.bucket(key).delete(key)
if el != nil {
c.deletePairs <- el
return true
}
return false
}
// Clear define
func (c *CLRUCache) Clear() {
done := make(chan struct{})
c.control <- clear{done: done}
<-done
}
// Count define
func (c *CLRUCache) Count() int {
count := 0
for _, b := range c.buckets {
count += b.pairCount()
}
return count
}
func (c *CLRUCache) bucket(key string) *bucket {
h := fnv.New32a()
h.Write([]byte(key))
return c.buckets[h.Sum32()&c.bucketMask]
}
func (c *CLRUCache) stop() {
close(c.movePairs)
<-c.control
}
func (c *CLRUCache) restart() {
c.deletePairs = make(chan *list.Element, 128)
c.movePairs = make(chan *list.Element, 128)
c.control = make(chan interface{})
go c.worker()
}
func (c *CLRUCache) worker() {
defer close(c.control)
for {
select {
case el, ok := <-c.movePairs:
if !ok {
goto clean
}
if c.doMove(el) && c.list.Len() > c.cap {
el := c.list.Back()
c.list.Remove(el)
c.bucket(el.Value.(Pair).key).delete(el.Value.(Pair).key)
}
case el := <-c.deletePairs:
c.list.Remove(el)
case control := <-c.control:
switch msg := control.(type) {
case clear:
for _, bucket := range c.buckets {
bucket.clear()
}
c.list = list.New()
msg.done <- struct{}{}
}
}
}
clean:
for {
select {
case el := <-c.deletePairs:
c.list.Remove(el)
default:
close(c.deletePairs)
return
}
}
}
func (c *CLRUCache) doMove(el *list.Element) bool {
if el.Value.(Pair).cmd == MoveToFront {
c.list.MoveToFront(el)
return false
}
newel := c.list.PushFront(el.Value.(Pair))
c.bucket(el.Value.(Pair).key).update(el.Value.(Pair).key, newel)
return true
}