generated from xiachufang/go-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hacache.go
286 lines (247 loc) · 6.78 KB
/
hacache.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
286
package hacache
import (
"errors"
"fmt"
"reflect"
"runtime/debug"
"time"
"github.com/vmihailenco/msgpack/v5"
"github.com/xiachufang/pkg/v2/hacache/storage"
"github.com/xiachufang/pkg/v2/limiter"
"go.uber.org/zap"
)
// SkipCache 当缓存 key 为 SkipCache 值时,跳过缓存
const SkipCache = "__hacache_skip_cache__"
// Event 拉取缓存时,触发的事件类型
type Event interface{}
// EventCacheExpired 缓存过期,但是可以接受,需要执行原始函数进行更新
type EventCacheExpired struct {
Args []interface{}
}
// EventCacheInvalid 缓存无效,需要立即更新
type EventCacheInvalid struct {
// Bytes 原始函数返回的结果,需要放到缓存里
Data interface{}
// Key 缓存 key
Key string
}
// HaCache ha-cache struct
type HaCache struct {
// fnRunLimiter 被缓存的原函数执行并发限制
fnRunLimiter *limiter.Limiter
opt *Options
events chan Event
logger *zap.Logger
}
// CachedValue 缓存值类型
type CachedValue struct {
// protobuf message 序列化之后的 bytes
Bytes []byte
// 缓存创建的时间戳/s
CreateTS int64
}
// FnResult 被缓存函数返回值的通用结构
type FnResult struct {
// Val 原函数返回值
Val interface{}
// Err 原函数返回的 error
Err error
// Ignore 忽略返回值,不设置回缓存
Ignore bool
}
// New return a new ha-cache instance
// nolint: gomnd
func New(opt *Options) (*HaCache, error) {
if opt.Storage == nil {
return nil, errors.New("no storage found")
}
opt.Init()
if reflect.ValueOf(opt.Fn).Type().NumOut() != 1 {
return nil, errors.New("fn return value must be `*hacache.FnResult`")
}
returnType := reflect.TypeOf(opt.Fn).Out(0)
if returnType != reflect.TypeOf((*FnResult)(nil)) {
return nil, errors.New("fn return value must be `*hacache.FnResult`")
}
hc := &HaCache{
fnRunLimiter: limiter.New(opt.FnRunLimit),
opt: opt,
events: make(chan Event, opt.EventBufferSize),
logger: opt.Logger,
}
go hc.worker()
return hc, nil
}
// worker 刷新缓存、更新过期缓存
func (hc *HaCache) worker() {
defer func() {
if v := recover(); v != nil {
CurrentStats.Incr(MWorkerPanic, 1)
hc.logger.Error(fmt.Sprintf("hacache worker paniced: %v, stack: %s", v, string(debug.Stack())))
hc.worker()
}
}()
for {
event := <-hc.events
switch e := event.(type) {
case *EventCacheExpired:
data, err := hc.FnRun(true, e.Args...)
if err != nil || data.Err != nil {
continue
}
if err := hc.Set(hc.GenCacheKey(e.Args...), data.Val); err != nil {
continue
}
case *EventCacheInvalid:
if err := hc.Set(e.Key, e.Data); err != nil {
continue
}
}
}
}
// FnRun 执行原函数,原函数执行时,受并发限制,
// 如果是缓存过期异步更新,触发限流直接跳过;
// 如果是缓存失效同步更新,触发限流服务报错
// 被缓存的函数签名为: func(args ...interface{}) (*FnResult)
func (hc *HaCache) FnRun(background bool, args ...interface{}) (*FnResult, error) {
CurrentStats.Incr(MFnRun, 1)
_, ok := hc.fnRunLimiter.Incr(1)
defer hc.fnRunLimiter.Decr(1)
if !ok {
CurrentStats.Incr(MFnRunLimited, 1)
}
// 异步更新的直接跳过,需要同步更新的返回报错
if !ok && background {
return nil, nil
} else if !ok && !background {
return nil, ErrorFnRunLimited
}
result, err := call(hc.opt.Fn, args...)
if err != nil {
return nil, err
}
v, ok := result[0].Interface().(*FnResult)
if ok {
return v, nil
}
return nil, fmt.Errorf("fnResult type convert error")
}
// GenCacheKey 生成缓存 key
func (hc *HaCache) GenCacheKey(args ...interface{}) string {
result, err := call(hc.opt.GenKeyFn, args...)
if err != nil {
return ""
}
// 生成缓存 key 的函数只有一个 string 返回值
if len(result) != 1 {
return ""
}
if key, ok := result[0].Interface().(string); ok {
return key
}
return ""
}
// Get get cached value
func (hc *HaCache) Get(key string) (*CachedValue, error) {
b, err := hc.opt.Storage.Get(key)
if err != nil {
return nil, err
}
v := new(CachedValue)
err = msgpack.Unmarshal(b, v)
return v, err
}
// Set set `key` to `msg`
func (hc *HaCache) Set(key string, data interface{}) error {
// protobuf message 用 protobuf 序列化
// 带上 create time 时间戳的 struct 用 msgpack 序列化
b, err := hc.opt.Encoder.Encode(data)
if err != nil {
return err
}
value, err := msgpack.Marshal(CachedValue{
Bytes: b,
CreateTS: time.Now().Unix(),
})
if err != nil {
return err
}
return hc.opt.Storage.Set(key, value, 0)
}
// Trigger 触发某个 event (non-blocking)
func (hc *HaCache) Trigger(event Event) {
select {
case hc.events <- event:
return
default:
CurrentStats.Incr(MEventChanBlocked, 1)
}
}
// Do 取缓存结果,如果不存在,则更新缓存
func (hc *HaCache) Do(args ...interface{}) (interface{}, error) {
cacheKey := hc.GenCacheKey(args...)
if cacheKey == "" {
return nil, ErrorInvalidCacheKey
} else if cacheKey == SkipCache {
CurrentStats.Incr(MSkip, 1)
res, err := hc.FnRun(false, args...)
if err != nil {
return nil, err
}
return res.Val, res.Err
}
value, err := hc.Get(cacheKey)
// 这里取缓存出错,一般可认为是没取到缓存,极端情况可能是 Redis 异常,直接穿透到原函数返回,并刷新缓存
// 原函数执行受 FnRunLimiter 并发限制
if err == storage.ErrorCacheMiss {
CurrentStats.Incr(MMiss, 1)
}
// 缓存 miss,执行原函数
if err != nil {
res, err := hc.FnRun(false, args...)
if err != nil || res.Err != nil {
CurrentStats.Incr(MFnRunErr, 1)
return nil, err
}
if !res.Ignore {
hc.Trigger(&EventCacheInvalid{
Data: res.Val,
Key: cacheKey,
})
}
return res.Val, nil
}
expireAt := value.CreateTS + int64(hc.opt.Expiration.Seconds())
now := time.Now().Unix()
// 缓存值在有效期内
if expireAt >= now {
CurrentStats.Incr(MHit, 1)
return hc.opt.Encoder.Decode(value.Bytes)
}
// 缓存过期已经超过了最大可接受时间,需要同步更新缓存,并返回最新内容
if now > (expireAt + int64(hc.opt.MaxAcceptableExpiration.Seconds())) {
CurrentStats.Incr(MMissInvalid, 1)
res, err := hc.FnRun(false, args...)
// 触发限流、或者原函数执行错误,强制返回过期数据,并且跳过缓存更新步骤
if err != nil || res.Err != nil {
CurrentStats.Incr(MInvalidReturned, 1)
return hc.opt.Encoder.Decode(value.Bytes)
}
if !res.Ignore {
hc.Trigger(&EventCacheInvalid{
Data: copyVal(res.Val),
Key: cacheKey,
})
}
return res.Val, nil
}
CurrentStats.Incr(MMissExpired, 1)
// 缓存过期,但是在可接受的过期范围内,返回缓存内容,并触发更新任务
v, err := hc.opt.Encoder.Decode(value.Bytes)
if err == nil {
hc.Trigger(&EventCacheExpired{
Args: args,
})
}
return v, err
}