-
Notifications
You must be signed in to change notification settings - Fork 55
/
lru.go
222 lines (200 loc) · 5.21 KB
/
lru.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
package zcache
import (
"sync"
"time"
"unsafe"
"github.com/sohaha/zlsgo/ztime"
"golang.org/x/sync/singleflight"
)
// FastCache concurrent LRU cache structure
type FastCache struct {
gsf singleflight.Group
callback handler
locks []sync.Mutex
insts [][2]*lruCache
expiration time.Duration
mask int32
}
type Options struct {
Callback func(ActionKind, string, uintptr)
Expiration time.Duration
Bucket uint16
Cap uint16
LRU2Cap uint16
}
// NewFast Fast LRU cache
func NewFast(opt ...func(o *Options)) *FastCache {
o := Options{
Cap: 1 << 10,
Bucket: 4,
}
for _, f := range opt {
f(&o)
}
var mask uint16
if o.Bucket > 0 && o.Bucket&(o.Bucket-1) == 0 {
mask = o.Bucket - 1
} else {
o.Bucket |= o.Bucket >> 1
o.Bucket |= o.Bucket >> 2
o.Bucket |= o.Bucket >> 4
mask = o.Bucket | (o.Bucket >> 8)
}
c := &FastCache{
locks: make([]sync.Mutex, mask+1),
insts: make([][2]*lruCache, mask+1),
callback: o.Callback,
mask: int32(mask),
}
for i := range c.insts {
c.insts[i][0] = &lruCache{dlList: make([][2]uint16, uint32(o.Cap)+1), nodes: make([]node, o.Cap), hashmap: make(map[string]uint16, o.Cap), last: 0}
if o.LRU2Cap > 0 {
c.insts[i][1] = &lruCache{dlList: make([][2]uint16, uint32(o.LRU2Cap)+1), nodes: make([]node, o.LRU2Cap), hashmap: make(map[string]uint16, o.LRU2Cap), last: 0}
}
}
if o.Expiration > 0 {
c.expiration = o.Expiration
}
return c
}
func (l *FastCache) set(k string, v *interface{}, b []byte, expiration ...time.Duration) {
if l.callback != nil {
if v != nil {
l.callback(SET, k, uintptr(unsafe.Pointer(v)))
} else {
l.callback(SET, k, uintptr(unsafe.Pointer(&b)))
}
}
idx := hasher(k) & l.mask
var expireAt int64
if len(expiration) > 0 {
if expiration[0] == -1 {
} else if expiration[0] > 0 {
expireAt = ztime.Clock()*1000 + int64(expiration[0])
} else if l.expiration > 0 {
expireAt = ztime.Clock()*1000 + int64(l.expiration)
}
} else if l.expiration > 0 {
expireAt = ztime.Clock()*1000 + int64(l.expiration)
}
l.locks[idx].Lock()
l.insts[idx][0].put(k, v, b, expireAt)
l.locks[idx].Unlock()
}
// Set an item into cache
func (l *FastCache) Set(key string, val interface{}, expiration ...time.Duration) {
l.set(key, &val, nil, expiration...)
}
// SetBytes an item into cache
func (l *FastCache) SetBytes(key string, b []byte) {
l.set(key, nil, b)
}
// Get value of key from cache with result
func (l *FastCache) Get(key string) (interface{}, bool) {
if i, b, ok := l.get(key); ok {
if i != nil {
return *i, true
}
return b, true
}
return nil, false
}
// GetBytes value of key from cache with result
func (l *FastCache) GetBytes(key string) ([]byte, bool) {
if i, b, ok := l.get(key); ok {
if b != nil {
return b, true
}
b, ok = (*i).([]byte)
return b, ok
}
return nil, false
}
// ProvideGet get value of key from cache with result and provide default value
func (l *FastCache) ProvideGet(key string, provide func() (interface{}, bool), expiration ...time.Duration) (interface{}, bool) {
if i, _, ok := l.get(key); ok && i != nil {
return *i, true
}
_, _, _ = l.gsf.Do(key, func() (value interface{}, err error) {
value, ok := provide()
if ok {
l.Set(key, value, expiration...)
}
return
})
return l.Get(key)
}
func (l *FastCache) getValue(key string, idx, level int32) (*node, int) {
n, s := l.insts[idx][level].get(key)
if s > 0 && !n.isDelete && (n.expireAt == 0 || (ztime.Clock()*1000 <= n.expireAt)) {
return n, s
}
return nil, 0
}
func (l *FastCache) get(key string) (i *interface{}, b []byte, loaded bool) {
idx := hasher(key) & l.mask
l.locks[idx].Lock()
n, s := (*node)(nil), 0
if l.insts[idx][1] == nil {
n, s = l.getValue(key, idx, 0)
} else {
e := int64(0)
if n, s, e = l.insts[idx][0].delete(key); s <= 0 {
n, s = l.getValue(key, idx, 1)
} else {
l.insts[idx][1].put(key, n.value.value, n.value.byteValue, e)
}
}
if s <= 0 {
l.locks[idx].Unlock()
if l.callback != nil {
l.callback(GET, key, uintptr(0))
}
return
}
i, b = n.value.value, n.value.byteValue
l.locks[idx].Unlock()
if l.callback != nil {
if i != nil {
l.callback(GET, key, uintptr(unsafe.Pointer(i)))
} else {
var b interface{} = b
l.callback(GET, key, uintptr(unsafe.Pointer(&b)))
}
}
return i, b, true
}
// Delete item by key from cache
func (l *FastCache) Delete(key string) {
idx := hasher(key) & l.mask
l.locks[idx].Lock()
n, s, e := l.insts[idx][0].delete(key)
if l.insts[idx][1] != nil {
if n2, s2, e2 := l.insts[idx][1].delete(key); n2 != nil && (n == nil || e < e2) {
n, s = n2, s2
}
}
if s > 0 {
if l.callback != nil {
if n.value.value != nil {
l.callback(DELETE, key, uintptr(unsafe.Pointer(n.value.value)))
} else {
l.callback(DELETE, key, uintptr(unsafe.Pointer(&n.value.byteValue)))
}
}
n.value.value, n.value.byteValue = nil, nil
} else if l.callback != nil {
l.callback(DELETE, key, uintptr(0))
}
l.locks[idx].Unlock()
}
// ForEach walk through all items in cache
func (l *FastCache) ForEach(walker func(key string, iface interface{}) bool) {
for i := range l.insts {
l.locks[i].Lock()
if l.insts[i][0].forEach(walker); l.insts[i][1] != nil {
l.insts[i][1].forEach(walker)
}
l.locks[i].Unlock()
}
}