-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.go
285 lines (242 loc) · 6.17 KB
/
group.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package lcache
import (
"context"
"errors"
"fmt"
"log"
"sync"
"time"
"github.com/juguagua/lcache/singleflight"
"github.com/sirupsen/logrus"
)
var (
lock sync.RWMutex
groups = sync.Map{}
)
// Getter 加载键值的回调函数接口
type Getter interface {
Get(ctx context.Context, key string) ([]byte, error)
}
// GetterFunc 函数类型实现 Getter 接口
type GetterFunc func(ctx context.Context, key string) ([]byte, error)
func (f GetterFunc) Get(ctx context.Context, key string) ([]byte, error) {
return f(ctx, key)
}
// Group 是一个缓存命名空间
type Group struct {
name string
getter Getter
mainCache cache
peers PeerPicker
loadGroup sync.WaitGroup // 防止缓存击穿
loadMap sync.Map // key -> *call
expiration time.Duration // 缓存过期时间,0表示永不过期
loader *singleflight.Group
}
// call 正在进行中的请求
type call struct {
wg sync.WaitGroup
val []byte
err error
}
// NewGroup 创建一个新的 Group 实例
func NewGroup(name string, cacheBytes int64, getter Getter, opts ...Option) *Group {
if getter == nil {
panic("nil Getter")
}
lock.Lock()
defer lock.Unlock()
g := &Group{
name: name,
getter: getter,
mainCache: cache{
cacheBytes: cacheBytes,
},
loader: &singleflight.Group{},
}
// 应用选项
for _, opt := range opts {
opt(g)
}
groups.Store(name, g)
return g
}
// Option 定义Group的配置选项
type Option func(*Group)
// WithExpiration 设置缓存过期时间
func WithExpiration(d time.Duration) Option {
return func(g *Group) {
g.expiration = d
}
}
// WithPeers 设置分布式节点
func WithPeers(peers PeerPicker) Option {
return func(g *Group) {
g.peers = peers
}
}
func GetGroup(name string) *Group {
if g, ok := groups.Load(name); ok {
return g.(*Group)
}
return nil
}
// Get 从缓存获取数据
func (g *Group) Get(ctx context.Context, key string) (ByteView, error) {
if key == "" {
return ByteView{}, errors.New("key is required")
}
// 从本地缓存获取
if v, ok := g.mainCache.get(key); ok {
logrus.Debugf("[LCache] hit local cache, group: %s, key: %s", g.name, key)
return v, nil
}
// 尝试从其他节点获取或加载
return g.load(ctx, key)
}
// Set 设置缓存值
func (g *Group) Set(ctx context.Context, key string, value []byte) error {
if key == "" {
return errors.New("key is required")
}
if len(value) == 0 {
return errors.New("value is required")
}
// 检查是否是从其他节点同步过来的请求
if ctx.Value("from_peer") != nil {
// 如果是从其他节点同步来的,只更新本地缓存,不再向其他节点同步
view := ByteView{b: cloneBytes(value)}
if g.expiration > 0 {
g.mainCache.addWithExpiration(key, view, time.Now().Add(g.expiration))
} else {
g.mainCache.add(key, view)
}
return nil
}
// 本地发起的请求,需要同步到其他节点
view := ByteView{b: cloneBytes(value)}
if g.expiration > 0 {
g.mainCache.addWithExpiration(key, view, time.Now().Add(g.expiration))
} else {
g.mainCache.add(key, view)
}
// 如果是分布式模式,同步到其他节点
if g.peers != nil {
if peer, ok, self := g.peers.PickPeer(key); ok && !self {
go func() {
ctx := context.WithValue(context.Background(), "from_peer", true)
if err := peer.Set(ctx, g.name, key, value); err != nil {
logrus.Errorf("[LCache] failed to sync set to peer: %v", err)
}
}()
}
}
return nil
}
// Delete 删除缓存值
func (g *Group) Delete(ctx context.Context, key string) error {
if key == "" {
return errors.New("key is required")
}
g.mainCache.delete(key)
// 如果是分布式模式,同步删除到其他节点
if g.peers != nil {
if peer, ok, self := g.peers.PickPeer(key); ok && !self {
if _, err := peer.Delete(g.name, key); err != nil {
logrus.Errorf("[LCache] failed to sync delete to peer: %v", err)
return err
}
}
}
return nil
}
// Clear 清空缓存
func (g *Group) Clear() {
g.mainCache.Clear()
}
// load 加载数据
func (g *Group) load(ctx context.Context, key string) (value ByteView, err error) {
// 使用 loadGroup 确保并发请求只加载一次
view, err := g.loadSingle(ctx, key)
if err != nil {
return view, err
}
// 设置到本地缓存
if g.expiration > 0 {
g.mainCache.addWithExpiration(key, view, time.Now().Add(g.expiration))
} else {
g.mainCache.add(key, view)
}
return view, nil
}
// loadSingle 确保并发请求只加载一次
func (g *Group) loadSingle(ctx context.Context, key string) (value ByteView, err error) {
// 检查是否已有相同的请求在处理
if c, ok := g.loadMap.Load(key); ok {
call := c.(call)
call.wg.Wait()
return ByteView{b: call.val}, call.err
}
// 创建新的请求
c := call{}
c.wg.Add(1)
g.loadMap.Store(key, c)
defer func() {
g.loadMap.Delete(key)
}()
// 尝试从远程节点获取
if g.peers != nil {
if peer, ok, self := g.peers.PickPeer(key); ok && !self {
value, err := g.getFromPeer(ctx, peer, key)
if err == nil {
c.val = value.b
c.err = nil
c.wg.Done()
return value, nil
}
logrus.Warnf("[LCache] failed to get from peer: %v", err)
}
}
// 从数据源加载
bytes, err := g.getter.Get(ctx, key)
if err != nil {
c.err = err
c.wg.Done()
return ByteView{}, err
}
value = ByteView{b: cloneBytes(bytes)}
c.val = value.b
c.wg.Done()
return value, nil
}
// getFromPeer 从其他节点获取数据
func (g *Group) getFromPeer(ctx context.Context, peer Peer, key string) (ByteView, error) {
bytes, err := peer.Get(g.name, key)
if err != nil {
return ByteView{}, fmt.Errorf("failed to get from peer: %v", err)
}
return ByteView{b: bytes}, nil
}
// RegisterPeers 注册PeerPicker
func (g *Group) RegisterPeers(peers PeerPicker) {
if g.peers != nil {
panic("RegisterPeers called more than once")
}
g.peers = peers
}
// Stats 返回缓存统计信息
func (g *Group) Stats() map[string]interface{} {
return map[string]interface{}{
"name": g.name,
"cacheSize": g.mainCache.Len(),
"expiration": g.expiration,
}
}
// DestroyGroup 销毁指定名称的缓存组
func DestroyGroup(name string) {
g := GetGroup(name)
if g != nil {
groups.Delete(name)
log.Printf("Destroyed cache group [%s]", name)
}
}