-
Notifications
You must be signed in to change notification settings - Fork 2
/
bucket.go
252 lines (230 loc) · 5.45 KB
/
bucket.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
package mace
import (
"container/heap"
"log"
"sync"
"time"
)
type MaceBucket struct {
sync.RWMutex
name string
items map[string]*MaceItem
leakqueue *leakQueue
leakTimer *time.Timer
leakInterval time.Duration
logger *log.Logger
loadItems func(string) *MaceItem
onAddItem func(*MaceItem)
onDeleteItem func(*MaceItem)
accessMax int
}
func (bucket *MaceBucket) Name() string {
return bucket.name
}
func (bucket *MaceBucket) Count() int {
bucket.RLock()
r := len(bucket.items)
bucket.RUnlock()
return r
}
func (bucket *MaceBucket) SetDataLoader(f func(string) *MaceItem) {
bucket.Lock()
bucket.loadItems = f
bucket.Unlock()
}
func (bucket *MaceBucket) SetOnAddItem(f func(*MaceItem)) {
bucket.Lock()
bucket.onAddItem = f
bucket.Unlock()
}
func (bucket *MaceBucket) SetOnDeleteItem(f func(*MaceItem)) {
bucket.Lock()
bucket.onDeleteItem = f
bucket.Unlock()
}
func (bucket *MaceBucket) SetLogger(logger *log.Logger) {
bucket.Lock()
bucket.logger = logger
bucket.Unlock()
}
func (bucket *MaceBucket) leakCheck() {
bucket.Lock()
if bucket.leakTimer != nil {
bucket.leakTimer.Stop()
}
if bucket.leakInterval > 0 {
bucket.log("Expiration check triggered after " + bucket.leakInterval.String() + " for bucket" + bucket.name)
} else {
bucket.log("Expiration check installed on bucket", bucket.name)
}
invalidL := []*disposeItem{}
cur := time.Now()
l := bucket.leakqueue
for {
if l.Len() == 0 {
break
}
it := heap.Pop(l)
if cur.Sub(it.(*disposeItem).disposeTime) <= 0 {
heap.Push(l, it.(*disposeItem))
break
}
invalidL = append(invalidL, it.(*disposeItem))
}
// fetch current time for comparison
// used to create next timer callback
// Change this to Heap so that cleaning is after
// at expense of more space usage
// Per item timestamp + pointer to item
for _, itemP := range invalidL {
key := itemP.value
bucket.delete(key)
}
bucket.leakInterval = 0 * time.Millisecond
bucket.Unlock()
bucket.RLock()
if bucket.leakqueue.Len() > 0 {
itemMin := heap.Pop(l)
dur := itemMin.(*disposeItem).disposeTime
bucket.leakInterval = dur.Sub(cur)
heap.Push(l, itemMin.(*disposeItem))
bucket.leakTimer = time.AfterFunc(bucket.leakInterval, func() {
go bucket.leakCheck()
})
}
bucket.RUnlock()
}
func (bucket *MaceBucket) delete(key string) (*MaceItem, error) {
// Needs to be called within a lock
v, ok := bucket.items[key]
if !ok {
return nil, ErrKeyNotFound
}
deleteCallback := bucket.onDeleteItem
if deleteCallback != nil {
// TODO: clone item before calling this routine
// Secondary advantage is ablility to run this as separate
// go routine
deleteCallback(v)
}
delete(bucket.items, key)
return v, nil
}
func (bucket *MaceBucket) Delete(key string) (*MaceItem, error) {
bucket.Lock()
v, ok := bucket.items[key]
if !ok {
bucket.Unlock()
return nil, ErrKeyNotFound
}
deleteCallback := bucket.onDeleteItem
bucket.Unlock()
if deleteCallback != nil {
// TODO: clone item before calling this routine
// Secondary advantage is ablility to run this as separate
// go routine
deleteCallback(v)
}
bucket.Lock()
bucket.log("Deleting item with key: " + key + " created on " + v.Created().String())
if v.Alive() != 0 {
dispose := v.dispose
if dispose != nil {
heap.Remove(bucket.leakqueue, dispose.index)
}
}
delete(bucket.items, key)
bucket.Unlock()
return v, nil
}
func (bucket *MaceBucket) Set(key string, data interface{},
alive time.Duration) *MaceItem {
// If the key already exists
if bucket.Exists(key) {
bucket.Delete(key)
}
item := NewMaceItem(key, data, alive)
bucket.Lock()
bucket.log("Adding item with key: " + key +
" which will be alive for:" + alive.String())
bucket.items[key] = item
if item.alive != 0 {
heap.Push(bucket.leakqueue, item.dispose)
}
expiry := bucket.leakInterval
addCallback := bucket.onAddItem
bucket.Unlock()
if addCallback != nil {
// TODO: clone item and call addCallback as a go routine
addCallback(item)
}
// Leak check set or run
if alive > 0 && (expiry == 0 || alive < expiry) {
bucket.leakCheck()
}
return item
}
func (bucket *MaceBucket) Exists(key string) bool {
bucket.RLock()
v, ok := bucket.items[key]
bucket.RUnlock()
return ok && v != nil
}
func (bucket *MaceBucket) KeepAlive(key string) error {
bucket.Lock()
v, ok := bucket.items[key]
v.KeepAlive()
// We care to update LeakQueue only if it has Alive duration
// set
if ok {
if v.Alive() != 0 {
bucket.leakqueue.update(v.dispose)
}
bucket.Unlock()
return nil
}
bucket.Unlock()
return ErrKeyNotFound
}
func (bucket *MaceBucket) Get(key string) (*MaceItem, error) {
bucket.RLock()
v, ok := bucket.items[key]
loadItems := bucket.loadItems
bucket.RUnlock()
if ok {
bucket.KeepAlive(key)
if bucket.accessMax != 0 && v.AccessCount() >= bucket.accessMax {
bucket.Delete(key)
}
return v, nil
}
if loadItems != nil {
item := loadItems(key)
if item != nil {
bucket.Set(key, item.data, item.Alive())
return item, nil
}
return nil, ErrKeyNotFoundOrLoadable
}
return nil, ErrKeyNotFound
}
func (bucket *MaceBucket) Flush() {
bucket.Lock()
bucket.log("Flushing the cache bucket: " + bucket.name)
bucket.items = make(map[string]*MaceItem)
l := leakQueue{}
heap.Init(&l)
bucket.leakqueue = &l
bucket.leakInterval = 0
if bucket.leakTimer != nil {
bucket.leakTimer.Stop()
}
bucket.Unlock()
return
}
func (bucket *MaceBucket) log(v ...interface{}) {
if bucket.logger == nil {
return
}
bucket.logger.Println(v)
}