-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorator.go
384 lines (350 loc) · 9.61 KB
/
decorator.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package gofnext
import (
"context"
"encoding/json"
"reflect"
"sync"
"time"
"github.com/ahuigo/gofnext/serial"
)
type Config struct {
/* Set cache's TTL time:
if TTL==0, use permanent cache;
if TTL>0, cache's live time is TTL
*/
TTL time.Duration
/* Set error cache's TTL time:
if ErrTTL=0, do not cache error;
if ErrTTL>0, error cache's live time is ErrTTL;
if ErrTTL=-1, error cache's live time is controlled by TTL
*/
ErrTTL time.Duration
CacheMap CacheMap
NeedDumpKey bool
HashKeyPointerAddr bool
HashKeyFunc func(args ...any) []byte
}
type cachedFn[K1, K2, K3 any, V any] struct {
needDumpKey bool
hashKeyPointerAddr bool
hashKeyFunc func(args ...any) []byte
cacheMap CacheMap
pkeyLockMap sync.Map
keyLen int
getFunc func(K1, K2, K3) (V, error)
}
func (c *cachedFn[K1, K2, K3, V]) setConfigs(configs ...*Config) *cachedFn[K1, K2, K3, V] {
if len(configs) > 0 {
return c.setConfig(configs[0])
} else {
return c.setConfig(nil)
}
}
func (c *cachedFn[K1, K2, K3, V]) setConfig(config *Config) *cachedFn[K1, K2, K3, V] {
// default value
if config == nil {
config = &Config{}
}
if config.CacheMap == nil {
config.CacheMap = newCacheMapMem(config.TTL)
}
// init value
c.hashKeyPointerAddr = config.HashKeyPointerAddr
c.needDumpKey = config.NeedDumpKey
c.cacheMap = config.CacheMap
if config.ErrTTL < -1 {
panic("ErrTTL should not be less than -1")
}
if config.TTL < 0 {
panic("TTL should not be less than 0")
}
c.cacheMap.SetErrTTL(config.ErrTTL)
if config.TTL > 0 {
c.cacheMap.SetTTL(config.TTL)
}
// init hashKeyFuncMethod
if config.HashKeyFunc != nil {
c.hashKeyFunc = config.HashKeyFunc
} else {
cacheMapRefV := reflect.ValueOf(c.cacheMap)
methodValue := cacheMapRefV.MethodByName("HashKeyFunc")
if methodValue.IsValid() {
c.hashKeyFunc = methodValue.Interface().(func(...any) []byte)
// c.hashKeyFunc = func(keys ...any) []byte {
// reflectKeys := make([]reflect.Value, len(keys))
// for i, key := range keys {
// reflectKeys[i] = reflect.ValueOf(key)
// }
// result := methodValue.Call(reflectKeys)
// if len(result) > 0 {
// if bytes, ok := result[0].Interface().([]byte); ok {
// return bytes
// }
// }
// return nil
// }
}
}
return c
}
// Cache Function with 3 parameter(with error)
func CacheFn3Err[K1 any, K2 any, K3 any, V any](
getFunc func(K1, K2, K3) (V, error),
configs ...*Config,
) func(K1, K2, K3) (V, error) {
getFunc0 := func(k1 K1, k2 K2, k3 K3) (V, error) {
return getFunc(k1, k2, k3)
}
ins := &cachedFn[K1, K2, K3, V]{getFunc: getFunc0, keyLen: 3}
ins.setConfigs(configs...)
return ins.invoke3err
}
// Cache Function with 3 parameter
func CacheFn3[K1 any, K2 any, K3 any, V any](
getFunc func(K1, K2, K3) V,
configs ...*Config,
) func(K1, K2, K3) V {
getFunc0 := func(k1 K1, k2 K2, k3 K3) (V, error) {
return getFunc(k1, k2, k3), nil
}
ins := &cachedFn[K1, K2, K3, V]{getFunc: getFunc0, keyLen: 3}
ins.setConfigs(configs...)
return ins.invoke3
}
// Cache Function with 2 parameter(with error)
func CacheFn2Err[K1 any, K2 any, V any](
getFunc func(K1, K2) (V, error),
configs ...*Config,
) func(K1, K2) (V, error) {
getFunc0 := func(k1 K1, k2 K2, k3 int8) (V, error) {
return getFunc(k1, k2)
}
ins := &cachedFn[K1, K2, int8, V]{getFunc: getFunc0, keyLen: 2}
ins.setConfigs(configs...)
return ins.invoke2err
}
// Cache Function with 2 parameter
func CacheFn2[K1 any, K2 any, V any](
getFunc func(K1, K2) V,
configs ...*Config,
) func(K1, K2) V {
getFunc0 := func(k1 K1, k2 K2, k3 any) (V, error) {
return getFunc(k1, k2), nil
}
ins := &cachedFn[K1, K2, any, V]{getFunc: getFunc0, keyLen: 2}
ins.setConfigs(configs...)
return ins.invoke2
}
// Cache Function with 1 parameter(with error)
func CacheFn1Err[K any, V any](
getFunc func(K) (V, error),
configs ...*Config,
) func(K) (V, error) {
getFunc0 := func(key K, k2 context.Context, k3 any) (V, error) {
return getFunc(key)
}
ins := &cachedFn[K, context.Context, any, V]{getFunc: getFunc0, keyLen: 1}
ins.setConfigs(configs...)
x := ins.invoke1err
return x
}
// Cache Function with 1 parameter
func CacheFn1[K any, V any](
getFunc func(K) V,
configs ...*Config,
) func(K) V {
getFunc0 := func(k1 K, k2 context.Context, k3 int8) (V, error) {
return getFunc(k1), nil
}
ins := &cachedFn[K, context.Context, int8, V]{getFunc: getFunc0, keyLen: 1}
ins.setConfigs(configs...)
return ins.invoke1
}
// Cache Function with 0 parameter(with error)
func CacheFn0Err[V any](
getFunc func() (V, error),
configs ...*Config,
) func() (V, error) {
getFunc0 := func(ctx context.Context, i int8, a byte) (V, error) {
return getFunc()
}
ins := &cachedFn[context.Context, int8, byte, V]{getFunc: getFunc0, keyLen: 0}
ins.setConfigs(configs...)
return ins.invoke0err
}
// Cache Function with 0 parameter
func CacheFn0[V any](
getFunc func() V,
configs ...*Config,
) func() V {
getFunc0 := func(ctx context.Context, i int8, a byte) (V, error) {
return getFunc(), nil
}
ins := &cachedFn[context.Context, int8, byte, V]{getFunc: getFunc0, keyLen: 0}
ins.setConfigs(configs...)
return ins.invoke0
}
// Invoke cached function with no parameter
func (c *cachedFn[any, int, A, V]) invoke0() V {
var k1 any
var k2 int
var k3 A
retv, _ := c.invoke3err(k1, k2, k3)
return retv
}
// Invoke cached function with no parameter(with error)
func (c *cachedFn[any, int8, A, V]) invoke0err() (V, error) {
var k1 any
var k2 int8
// k2 = 0 // error: cannot use 0 (untyped int constant) as uint8 value in assignment
var k3 A
return c.invoke3err(k1, k2, k3)
}
// Invoke cached function with 1 parameter
func (c *cachedFn[K1, K2, K3, V]) invoke1(k1 K1) V {
var k2 K2
var k3 K3
val, _ := c.invoke3err(k1, k2, k3)
return val
}
func (c *cachedFn[K1, K2, K3, V]) invoke1err(k1 K1) (V, error) {
var k2 K2
var k3 K3
return c.invoke3err(k1, k2, k3)
}
// Invoke cached function with 2 parameter
func (c *cachedFn[K1, K2, A, V]) invoke2(key1 K1, key2 K2) (retv V) {
var a A
retv, _ = c.invoke3err(key1, key2, a)
return retv
}
func (c *cachedFn[K1, K2, K3, V]) invoke2err(k1 K1, k2 K2) (V, error) {
var k3 K3
return c.invoke3err(k1, k2, k3)
}
func (c *cachedFn[K1, K2, K3, V]) invoke3(key1 K1, key2 K2, key3 K3) (retv V) {
retv, _ = c.invoke3err(key1, key2, key3)
return retv
}
var _isHashKey map[any]int
func isHashableKey(key any, cmpPtr bool) (canHash bool) {
defer func() {
if err := recover(); err != nil {
canHash = false
}
}()
_ = _isHashKey[key]
if cmpPtr {
return true
}
return reflect.ValueOf(key).Kind() != reflect.Pointer
}
func (c *cachedFn[K1, K2, K3, V]) hashKeyFuncWrap(key1 K1, key2 K2, key3 K3) (pkey any) {
// outer hash key func
if c.hashKeyFunc != nil {
if c.keyLen == 3 {
pkey = string(c.hashKeyFunc(key1, key2, key3))
} else if c.keyLen == 2 {
pkey = string(c.hashKeyFunc(key1, key2))
} else if c.keyLen == 1 {
pkey = string(c.hashKeyFunc(key1))
} else {
pkey = 0
}
return pkey
}
// inner hash key func
needHashPtrAddr := c.hashKeyPointerAddr
needDumpKey := c.needDumpKey
if c.keyLen == 3 {
if _, hasCtx := any(key1).(context.Context); hasCtx {
pkey = [2]any{key2, key3}
if !needDumpKey {
needDumpKey = !isHashableKey(key2, needHashPtrAddr) || !isHashableKey(key3, needHashPtrAddr)
}
} else {
pkey = [3]any{key1, key2, key3}
if !needDumpKey {
needDumpKey = !isHashableKey(key1, needHashPtrAddr) || !isHashableKey(key2, needHashPtrAddr) || !isHashableKey(key3, needHashPtrAddr)
}
}
} else if c.keyLen == 2 {
if _, hasCtx := any(key1).(context.Context); hasCtx {
pkey = key2
if !needDumpKey {
needDumpKey = !isHashableKey(key2, needHashPtrAddr)
}
} else {
pkey = [2]any{key1, key2}
if !needDumpKey {
needDumpKey = !isHashableKey(key1, needHashPtrAddr) || !isHashableKey(key2, needHashPtrAddr)
}
}
} else if c.keyLen == 1 {
if _, hasCtx := any(key1).(context.Context); hasCtx {
pkey = 0
} else {
pkey = key1
if !needDumpKey {
needDumpKey = !isHashableKey(key1, needHashPtrAddr)
}
}
} else {
pkey = 0
}
if needDumpKey {
pkey = serial.String(pkey, needHashPtrAddr)
}
return pkey
}
// Invoke cached function with 2 parameter
func (c *cachedFn[K1, K2, K3, V]) invoke3err(key1 K1, key2 K2, key3 K3) (retv V, err error) {
// 1. generate pkey
var pkey any = c.hashKeyFuncWrap(key1, key2, key3)
// 2. require lock for each pkey(go routine safe)
var tmpOnce sync.RWMutex
pkeyLock := &tmpOnce
pkeyLockInter, loaded := c.pkeyLockMap.LoadOrStore(pkey, pkeyLock)
if loaded {
pkeyLock = pkeyLockInter.(*sync.RWMutex)
}
// 3. check cache
checkCacheCount := 0
checkCache:
checkCacheCount++
pkeyLock.RLock()
value, hasCache, err := c.cacheMap.Load(pkey)
pkeyLock.RUnlock()
// 3.1 check if marshal needed
if hasCache && c.cacheMap.NeedMarshal() {
err2 := json.Unmarshal(value.([]byte), &retv)
if err == nil {
err = err2
}
return retv, err
}
// 4. Execute getFunc(only once)
if !hasCache {
// 4.1 try lock
// If 100 goroutines call the same function at the same time,
// only one goroutine can execute the getFunc.
isLocked := pkeyLock.TryLock()
if !isLocked {
if checkCacheCount < 3 {
// wait for other goroutine to finish
time.Sleep(time.Millisecond * 10)
// Avoid all goroutines calling TryCount simultaneously, which may lead to failure.
sleepRandom(0, 50*time.Millisecond)
// try lock again
goto checkCache
}
pkeyLock.Lock()
}
defer pkeyLock.Unlock()
// 4.2 check cache again
val, err := c.getFunc(key1, key2, key3)
c.cacheMap.Store(pkey, &val, err)
return val, err
}
return *(value).(*V), err
}