-
Notifications
You must be signed in to change notification settings - Fork 2
/
policy.go
260 lines (215 loc) · 5.37 KB
/
policy.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
package cache
import (
"container/list"
"sync"
"sync/atomic"
)
const (
// Number of cache data store will be 2 ^ concurrencyLevel.
concurrencyLevel = 2
segmentCount = 1 << concurrencyLevel
segmentMask = segmentCount - 1
)
// entry stores cached entry key and value.
type entry struct {
// Structs with first field align to 64 bits will also be aligned to 64.
// https://golang.org/pkg/sync/atomic/#pkg-note-BUG
// hash is the hash value of this entry key
hash uint64
// accessTime is the last time this entry was accessed.
accessTime int64 // Access atomically - must be aligned on 32-bit
// writeTime is the last time this entry was updated.
writeTime int64 // Access atomically - must be aligned on 32-bit
// FIXME: More efficient way to store boolean flags
invalidated int32
loading int32
key Key
value atomic.Value // Store value
// These properties are managed by only cache policy so do not need atomic access.
// accessList is the list (ordered by access time) this entry is currently in.
accessList *list.Element
// writeList is the list (ordered by write time) this entry is currently in.
writeList *list.Element
// listID is ID of the list which this entry is currently in.
listID uint8
}
func newEntry(k Key, v Value, h uint64) *entry {
en := &entry{
key: k,
hash: h,
}
en.setValue(v)
return en
}
func (e *entry) getValue() Value {
return e.value.Load().(Value)
}
func (e *entry) setValue(v Value) {
e.value.Store(v)
}
func (e *entry) getAccessTime() int64 {
return atomic.LoadInt64(&e.accessTime)
}
func (e *entry) setAccessTime(v int64) {
atomic.StoreInt64(&e.accessTime, v)
}
func (e *entry) getWriteTime() int64 {
return atomic.LoadInt64(&e.writeTime)
}
func (e *entry) setWriteTime(v int64) {
atomic.StoreInt64(&e.writeTime, v)
}
func (e *entry) getLoading() bool {
return atomic.LoadInt32(&e.loading) != 0
}
func (e *entry) setLoading(v bool) bool {
if v {
return atomic.CompareAndSwapInt32(&e.loading, 0, 1)
}
return atomic.CompareAndSwapInt32(&e.loading, 1, 0)
}
func (e *entry) getInvalidated() bool {
return atomic.LoadInt32(&e.invalidated) != 0
}
func (e *entry) setInvalidated(v bool) {
if v {
atomic.StoreInt32(&e.invalidated, 1)
} else {
atomic.StoreInt32(&e.invalidated, 0)
}
}
// getEntry returns the entry attached to the given list element.
func getEntry(el *list.Element) *entry {
return el.Value.(*entry)
}
// event is the cache event (add, hit or delete).
type event uint8
const (
eventWrite event = iota
eventAccess
eventDelete
eventClose
)
type entryEvent struct {
entry *entry
event event
}
// cache is a data structure for cache entries.
type cache struct {
size int64 // Access atomically - must be aligned on 32-bit
segs [segmentCount]sync.Map // map[Key]*entry
}
func (c *cache) get(k Key, h uint64) *entry {
seg := c.segment(h)
v, ok := seg.Load(k)
if ok {
return v.(*entry)
}
return nil
}
func (c *cache) getOrSet(v *entry) *entry {
seg := c.segment(v.hash)
en, ok := seg.LoadOrStore(v.key, v)
if ok {
return en.(*entry)
}
atomic.AddInt64(&c.size, 1)
return nil
}
func (c *cache) delete(v *entry) {
seg := c.segment(v.hash)
seg.Delete(v.key)
atomic.AddInt64(&c.size, -1)
}
func (c *cache) len() int {
return int(atomic.LoadInt64(&c.size))
}
func (c *cache) walk(fn func(*entry)) {
for i := range c.segs {
c.segs[i].Range(func(k, v interface{}) bool {
fn(v.(*entry))
return true
})
}
}
func (c *cache) segment(h uint64) *sync.Map {
return &c.segs[h&segmentMask]
}
// policy is a cache policy.
type policy interface {
// init initializes the policy.
init(cache *cache, maximumSize int)
// write handles Write event for the entry.
// It adds new entry and returns evicted entry if needed.
write(entry *entry) *entry
// access handles Access event for the entry.
// It marks then entry recently accessed.
access(entry *entry)
// remove removes the entry.
remove(entry *entry) *entry
// iterate iterates all entries by their access time.
iterate(func(entry *entry) bool)
}
func newPolicy(name string) policy {
switch name {
case "", "slru":
return &slruCache{}
case "lru":
return &lruCache{}
case "tinylfu":
return &tinyLFU{}
default:
panic("cache: unsupported policy " + name)
}
}
// recencyQueue manages cache entries by write time.
type recencyQueue struct {
ls list.List
}
func (w *recencyQueue) init(cache *cache, maximumSize int) {
w.ls.Init()
}
func (w *recencyQueue) write(en *entry) *entry {
if en.writeList == nil {
en.writeList = w.ls.PushFront(en)
} else {
w.ls.MoveToFront(en.writeList)
}
return nil
}
func (w *recencyQueue) access(en *entry) {
}
func (w *recencyQueue) remove(en *entry) *entry {
if en.writeList == nil {
return en
}
w.ls.Remove(en.writeList)
en.writeList = nil
return en
}
func (w *recencyQueue) iterate(fn func(en *entry) bool) {
iterateListFromBack(&w.ls, fn)
}
type discardingQueue struct{}
func (discardingQueue) init(cache *cache, maximumSize int) {
}
func (discardingQueue) write(en *entry) *entry {
return nil
}
func (discardingQueue) access(en *entry) {
}
func (discardingQueue) remove(en *entry) *entry {
return en
}
func (discardingQueue) iterate(fn func(en *entry) bool) {
}
func iterateListFromBack(ls *list.List, fn func(en *entry) bool) {
for el := ls.Back(); el != nil; {
en := getEntry(el)
prev := el.Prev() // Get Prev as fn can delete the entry.
if !fn(en) {
return
}
el = prev
}
}