forked from qjfoidnh/BaiduPCS-Go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
54 lines (46 loc) · 979 Bytes
/
utils.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
package cachemap
import (
"github.com/qjfoidnh/BaiduPCS-Go/baidupcs/expires"
)
type (
OpFunc func() expires.DataExpires
OpFuncWithError func() (expires.DataExpires, error)
)
func (cm *CacheOpMap) CacheOperation(op string, key interface{}, opFunc OpFunc) (data expires.DataExpires) {
var (
cache = cm.LazyInitCachePoolOp(op)
ok bool
)
cache.LockKey(key)
defer cache.UnlockKey(key)
data, ok = cache.Load(key)
if !ok {
data = opFunc()
if data != nil {
cache.Store(key, data)
}
return
}
return
}
func (cm *CacheOpMap) CacheOperationWithError(op string, key interface{}, opFunc OpFuncWithError) (data expires.DataExpires, err error) {
var (
cache = cm.LazyInitCachePoolOp(op)
ok bool
)
cache.LockKey(key)
defer cache.UnlockKey(key)
data, ok = cache.Load(key)
if !ok {
data, err = opFunc()
if err != nil {
return
}
if data == nil {
// 数据为空时也不存
return
}
cache.Store(key, data)
}
return
}