-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucketInternal.go
170 lines (155 loc) · 3.68 KB
/
bucketInternal.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
package jac
import (
"runtime"
"time"
)
// back-end methods
// Return a declare bucketInternal with a given default expiration duration and cleanup
// interval. If the expiration duration is less than one (or NoExpiration),
// the items in the bucketInternal never expire (by default), and must be deleted
// manually. If the cleanup interval is less than one, expired items are not
// deleted from the bucketInternal before calling c.deleteExpired().
func declare(defaultExpiration, cleanupInterval time.Duration) *bucketInternalPtr {
items := make(map[string]Item)
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items)
}
func (c *bucketInternal) set(k string, x interface{}, d time.Duration) {
var e int64
if d == DefaultExpiration {
d = c.defaultExpiration
}
if d > 0 {
e = time.Now().Add(d).UnixNano()
}
c.items[k] = Item{
Object: x,
Expiration: e,
}
}
func (c *bucketInternal) get(k string) (interface{}, bool) {
item, found := c.items[k]
if !found {
return nil, false
}
// "Inlining" of expired
if item.Expiration > 0 {
if time.Now().UnixNano() > item.Expiration {
return nil, false
}
}
return item.Object, true
}
func (c *bucketInternal) delete(k string) (interface{}, bool) {
if c.onEvicted != nil {
if v, found := c.items[k]; found {
delete(c.items, k)
return v.Object, true
}
}
delete(c.items, k)
return nil, false
}
func (c *bucketInternal) deleteExpired() {
var evictedItems []keyAndValue
now := time.Now().UnixNano()
c.mu.Lock()
for k, v := range c.items {
// "Inlining" of expired
if v.Expiration > 0 && now > v.Expiration {
ov, evicted := c.delete(k)
if evicted {
evictedItems = append(evictedItems, keyAndValue{k, ov})
}
}
}
c.mu.Unlock()
for _, v := range evictedItems {
c.onEvicted(v.key, v.value)
}
}
func (c *bucketInternal) bucketItems() map[string]Item {
c.mu.RLock()
defer c.mu.RUnlock()
m := make(map[string]Item, len(c.items))
now := time.Now().UnixNano()
for k, v := range c.items {
// "Inlining" of expired
if v.Expiration > 0 {
if now > v.Expiration {
continue
}
}
m[k] = v
}
return m
}
func (j *janitor) run(c *bucketInternal) {
ticker := time.NewTicker(j.Interval)
for {
select {
case <-ticker.C:
c.deleteExpired()
case <-j.stop:
ticker.Stop()
return
}
}
}
func stopJanitor(c *bucketInternalPtr) {
c.janitor.stop <- true
}
func runJanitor(c *bucketInternal, ci time.Duration) {
j := &janitor{
Interval: ci,
stop: make(chan bool),
}
c.janitor = j
go j.run(c)
}
func newCache(de time.Duration, m map[string]Item) *bucketInternal {
if de == 0 {
de = -1
}
c := &bucketInternal{
defaultExpiration: de,
items: m,
}
return c
}
func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]Item) *bucketInternalPtr {
c := newCache(de, m)
// This trick ensures that the janitor goroutine (which--granted it
// was enabled--is running deleteExpired on c forever) does not keep
// the returned C object from being garbage collected. When it is
// garbage collected, the finalizer stops the janitor goroutine, after
// which c can be collected.
C := &bucketInternalPtr{c}
if ci > 0 {
runJanitor(c, ci)
runtime.SetFinalizer(C, stopJanitor)
}
return C
}
func (item Item) expired() bool {
if item.Expiration == 0 {
return false
}
return time.Now().UnixNano() > item.Expiration
}
func (c *Bucket) set(k, v string, t time.Duration, bck bool) {
if k == "" {
return
}
if c.writer != nil && bck {
select {
case c.writer <- backupData{
data: [2]string{k, v},
file: c.file,
}:
//default:
case <-time.After(time.Duration(options.LoadDelayMs) * time.Millisecond):
//println("lost")
}
}
c.bucket.set(k, v, t)
}