/
redis_base.go
284 lines (251 loc) · 7.34 KB
/
redis_base.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
package data
import (
"context"
"encoding/json"
"github.com/go-redis/redis/v8"
"github.com/spf13/cast"
"github.com/star-table/go-common/pkg/errors"
"github.com/star-table/go-common/utils/unsafe"
"github.com/star-table/go-table/internal/data/consts/cache"
)
// 一个闭包函数,用于生成新的类,并添加到返回值里面
type addInterface func() interface{}
type addInterfaceByKey func(k int64) interface{}
type redisBase struct {
data *Data
}
func (t *redisBase) setObject(ctx context.Context, key string, obj interface{}) error {
bts, err := json.Marshal(obj)
if err != nil {
return errors.WithStack(err)
}
return t.data.redisCli.Set(ctx, key, unsafe.BytesString(bts), cache.DefaultDuration).Err()
}
func (t *redisBase) getObject(ctx context.Context, key string, obj interface{}) error {
s, err := t.data.redisCli.Get(ctx, key).Result()
if err != nil {
return err
}
return errors.WithStack(json.Unmarshal(unsafe.StringBytes(s), obj))
}
// getHashValuesByKeys 目前只针对场景函数,没有特别的抽象化
func (t *redisBase) getHashValuesByKeys(ctx context.Context, keys []string, hKeys []string, ids []int64,
addFunc addInterfaceByKey) (map[int64]map[string]string, []int64, error) {
var (
allCmds []*redis.StringStringMapCmd
partCmds []*redis.SliceCmd
notFounds []int64
hashValuesMap map[int64]map[string]string
)
if len(hKeys) > 0 {
partCmds = make([]*redis.SliceCmd, 0, len(keys))
} else {
allCmds = make([]*redis.StringStringMapCmd, 0, len(keys))
}
_, err := t.data.redisCli.Pipelined(ctx, func(pipe redis.Pipeliner) error {
for _, key := range keys {
if len(hKeys) > 0 {
cmd := pipe.HMGet(ctx, key, hKeys...)
partCmds = append(partCmds, cmd)
} else {
cmd := pipe.HGetAll(ctx, key)
allCmds = append(allCmds, cmd)
}
}
return nil
})
if err != nil {
return nil, nil, errors.WithStack(err)
}
if len(hKeys) > 0 {
hashValuesMap, notFounds, err = t.getValuesBySliceCmd(ctx, partCmds, keys, hKeys, ids, addFunc)
} else {
hashValuesMap, notFounds, err = t.getValuesByMapCmd(allCmds, ids, addFunc)
}
return hashValuesMap, notFounds, err
}
func (t *redisBase) getValuesBySliceCmd(ctx context.Context, partCmds []*redis.SliceCmd, keys, hKeys []string,
ids []int64, addFunc addInterfaceByKey) (map[int64]map[string]string, []int64, error) {
notFounds := make([]int64, 0, 1)
result := make(map[int64]map[string]string, len(keys))
for i, cmd := range partCmds {
list, err := cmd.Result()
if err != nil && err != redis.Nil {
return nil, nil, errors.WithStack(err)
}
isAllNil := true
for _, s := range list {
if s != nil {
isAllNil = false
}
}
if isAllNil {
isExist, err := t.data.redisCli.Exists(ctx, keys[i]).Result()
if err != nil {
return nil, nil, errors.WithStack(err)
}
if isExist == 0 {
notFounds = append(notFounds, ids[i])
}
} else {
tempM := make(map[string]string, len(list))
for k, s := range list {
if s != nil {
str := cast.ToString(s)
tempM[hKeys[k]] = str
// 只有需要解析的时候才进入,有些其实只是收集字符串,并没有结构化
if addFunc != nil {
temp := addFunc(ids[i])
err = json.Unmarshal(unsafe.StringBytes(str), temp)
if err != nil {
return nil, nil, errors.Wrapf(err, "[getHashValuesByKeys] unmarshal str:%v error:%v", s, err)
}
}
}
}
result[ids[i]] = tempM
}
}
return result, notFounds, nil
}
func (t *redisBase) getValuesByMapCmd(allCmds []*redis.StringStringMapCmd, ids []int64, addFunc addInterfaceByKey) (
map[int64]map[string]string, []int64, error) {
notFounds := make([]int64, 0, 1)
result := make(map[int64]map[string]string, len(ids))
for i, cmd := range allCmds {
m, err := cmd.Result()
if err != nil && err != redis.Nil {
return nil, nil, errors.WithStack(err)
}
if len(m) == 0 {
notFounds = append(notFounds, ids[i])
} else {
result[ids[i]] = m
if addFunc != nil {
for _, s := range m {
temp := addFunc(ids[i])
err = json.Unmarshal(unsafe.StringBytes(s), temp)
if err != nil {
return nil, nil, errors.Wrapf(err, "[getHashValuesByKeys] unmarshal str:%v error:%v", s, err)
}
}
}
}
}
return result, notFounds, nil
}
func (t *redisBase) getHashValues(ctx context.Context, key string, hKeys []string, nf addInterface) error {
var (
m map[string]string
err error
)
if len(hKeys) == 0 {
m, err = t.data.redisCli.HGetAll(ctx, key).Result()
} else {
var list []interface{}
list, err = t.data.redisCli.HMGet(ctx, key, hKeys...).Result()
if err == nil {
m = make(map[string]string, len(list))
for i, id := range hKeys {
str := cast.ToString(list[i])
if str != "" {
m[id] = cast.ToString(list[i])
}
}
}
}
if err != nil && err != redis.Nil {
return errors.WithStack(err)
}
if len(m) == 0 {
return redis.Nil
}
for _, s := range m {
temp := nf()
err = json.Unmarshal(unsafe.StringBytes(s), temp)
if err != nil {
return errors.Wrapf(err, "[getHashValues] unmarshal str:%v error:%v", s, err)
}
}
return nil
}
func (t *redisBase) setHashValue(ctx context.Context, key, hKey string, v interface{}, justSetByExist bool) error {
// 如果设置了只有存在才设置,检查下是否存在,不存在才设置
if justSetByExist {
count, err := t.data.redisCli.HLen(ctx, key).Result()
if err != nil {
if err == redis.Nil {
return nil
}
return errors.WithStack(err)
}
if count == 0 {
return nil
}
}
bts, _ := json.Marshal(v)
err := t.data.redisCli.HSet(ctx, key, hKey, string(bts)).Err()
if err != nil {
return errors.WithStack(err)
}
err = t.data.redisCli.Expire(ctx, key, cache.DefaultDuration).Err()
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (t *redisBase) setHashValues(ctx context.Context, key string, hashValues map[interface{}]interface{}, justSetByExist bool) error {
// 如果设置了只有存在才设置,检查下是否存在,不存在才设置
if justSetByExist {
count, err := t.data.redisCli.HLen(ctx, key).Result()
if err != nil {
if err == redis.Nil {
return nil
}
return errors.WithStack(err)
}
if count == 0 {
return nil
}
}
values := make([]interface{}, 0, len(hashValues)*2)
for k, c := range hashValues {
if s, ok := c.(string); ok {
values = append(values, k, s)
} else {
bts, _ := json.Marshal(c)
values = append(values, k, unsafe.BytesString(bts))
}
}
err := t.data.redisCli.HMSet(ctx, key, values...).Err()
if err != nil {
return errors.WithStack(err)
}
err = t.data.redisCli.Expire(ctx, key, cache.DefaultDuration).Err()
if err != nil {
return errors.WithStack(err)
}
return nil
}
// setHashValuesPipelined 批量设置hash
func (t *redisBase) setHashValuesPipelined(ctx context.Context, hashValuesMap map[string]map[interface{}]interface{}) error {
_, err := t.data.redisCli.Pipelined(ctx, func(pipe redis.Pipeliner) error {
for key, hashValues := range hashValuesMap {
if len(hashValues) > 0 {
values := make([]interface{}, 0, len(hashValues)*2)
for k, c := range hashValues {
if s, ok := c.(string); ok {
values = append(values, k, s)
} else {
bts, _ := json.Marshal(c)
values = append(values, k, unsafe.BytesString(bts))
}
}
pipe.HMSet(ctx, key, values...)
pipe.Expire(ctx, key, cache.DefaultDuration)
}
}
return nil
})
return err
}