-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_cache.go
More file actions
127 lines (110 loc) · 2.93 KB
/
async_cache.go
File metadata and controls
127 lines (110 loc) · 2.93 KB
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
package AsyncCache
import (
"github.com/going/toolkit/to"
"sync"
"time"
)
type cacheHandler struct {
enabledCache bool
mutex sync.Mutex
errAmount int32
}
func (this *cacheHandler) AsyncGetAndUpdateData(f func() interface{}, key string, pEnabledCache bool) interface{} {
var cacheValue interface{} = nil
if this.enabledCache && pEnabledCache {
InstanceContainer.Exception.Try(
func() {
if v, err := InstanceContainer.redisClient.GetBytesSlice(key); v != nil && err == nil {
iSlice := InstanceContainer.serializer.DeserializeToSlice(v)
if len(iSlice) == 2 {
cacheValue = iSlice[1]
if time.Now().Sub(to.Time(iSlice[0])).Minutes() > 5 {
go this.asyncDealCacheTask(f, key, cacheValue, true)
}
}
} else {
InstanceContainer.Loghelper.Error(err)
this.healthDetect()
}
})
InstanceContainer.Exception.Catch(
func(ex interface{}) {
InstanceContainer.Loghelper.Error(ex)
this.healthDetect()
})
if cacheValue != nil {
return cacheValue
}
}
cacheValue = f()
if this.enabledCache && pEnabledCache {
go this.asyncDealCacheTask(f, key, cacheValue, false)
}
return cacheValue
}
func (this *cacheHandler) asyncDealCacheTask(f func() interface{}, key string, v interface{}, isDoF bool) {
InstanceContainer.Exception.Try(
func() {
if this.getLock(key) {
var r interface{} = v
if isDoF {
r = f()
}
iSlice := []interface{}{time.Now(), r}
cacheData := InstanceContainer.serializer.Serialize(iSlice)
InstanceContainer.redisClient.SetBytesSliceWithExpriePX(key, cacheData, 300*1000)
this.releaseLock(key)
}
})
InstanceContainer.Exception.Catch(
func(ex interface{}) {
InstanceContainer.Loghelper.Error(ex)
this.healthDetect()
})
}
func (*cacheHandler) getLock(key string) bool {
lockKey := key + "_lock"
if ok, _ := InstanceContainer.redisClient.SetNXInt(lockKey, 1); ok {
InstanceContainer.redisClient.SetStringWithExpriePX(lockKey, "1", 60)
return true
} else {
return false
}
}
func (*cacheHandler) releaseLock(key string) {
lockKey := key + "_lock"
InstanceContainer.redisClient.RemoveKey(lockKey)
}
func (this *cacheHandler) healthDetect() {
this.errAmount++
if this.errAmount > 100 && this.enabledCache {
this.mutex.Lock()
if this.errAmount > 100 && this.enabledCache {
this.enabledCache = false
this.errAmount = 0
this.mutex.Unlock()
go func() {
hasErr := true
retryFrequency := 5 //重试次数
semaphore := make(chan int)
for hasErr {
semaphore = make(chan int)
go func() {
InstanceContainer.redisClient.SetString("test_key", "ok")
InstanceContainer.redisClient.GetString("test_key")
semaphore <- 1
}()
select {
case <-semaphore:
if retryFrequency--; retryFrequency <= 0 {
hasErr = false
}
case <-time.After(1 * time.Second):
retryFrequency = 5
}
time.Sleep(1 * time.Second)
}
}()
}
}
}