/
redis.go
330 lines (294 loc) · 9.14 KB
/
redis.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
package pack
import (
"fmt"
libRedis "github.com/gomodule/redigo/redis"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
"math"
"strconv"
"strings"
"github.com/go-opener/ctxflow/v2/layer"
"github.com/go-opener/ctxflow/v2/puzzle"
)
const (
_CHUNK_SIZE = 32
)
type IRedis interface {
layer.IFlow
}
type Redis struct {
layer.Flow
RedisClient puzzle.IRedis
}
//默认第一个参数为db,可利用该特性批量处理事务
func (entity *Redis) PreUse(args ...interface{}) {
entity.RedisClient = puzzle.GetRedisClient()
entity.Flow.PreUse(args...)
}
//将interface转为string
func ToStr(value interface{}) (s string) {
switch v := value.(type) {
case bool:
s = strconv.FormatBool(v)
case float32:
s = strconv.FormatFloat(float64(v), 'f', -1, 32)
case float64:
s = strconv.FormatFloat(v, 'f', -1, 64)
case int:
s = strconv.FormatInt(int64(v), 10)
case int8:
s = strconv.FormatInt(int64(v), 10)
case int16:
s = strconv.FormatInt(int64(v), 10)
case int32:
s = strconv.FormatInt(int64(v), 10)
case int64:
s = strconv.FormatInt(v, 10)
case uint:
s = strconv.FormatUint(uint64(v), 10)
case uint8:
s = strconv.FormatUint(uint64(v), 10)
case uint16:
s = strconv.FormatUint(uint64(v), 10)
case uint32:
s = strconv.FormatUint(uint64(v), 10)
case uint64:
s = strconv.FormatUint(v, 10)
case string:
s = v
case []byte:
s = string(v)
default:
s = fmt.Sprintf("%v", v)
}
return s
}
func (entity *Redis) SetDebug(commandName string, args ...interface{}){
sv := entity.GetContext().Query("_debug")
if sv == "yayazhongtai"{
debug := commandName
for _,arg := range args{
debug += " " + ToStr(arg)
}
if v,ok :=entity.GetContext().Get("debug");ok{
entity.GetContext().Set("debug",append(v.([]string),debug))
}else{
entity.GetContext().Set("debug",[]string{debug})
}
}
}
func (entity *Redis) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
entity.SetDebug(commandName,args...)
return entity.RedisClient.Do(entity.GetContext(), commandName, args...)
}
func (entity *Redis) Expire(key string, time int64, opt ...int) (bool, error) {
if len(opt) > 0 {
noCache := opt[0]
if noCache == 1 {
return false, nil
}
}
return libRedis.Bool(entity.Do("EXPIRE", key, time))
}
func (entity *Redis) Exists(key string) (bool, error) {
return libRedis.Bool(entity.Do("EXISTS", key))
}
func (entity *Redis) Del(keys ...interface{}) (int64, error) {
return libRedis.Int64(entity.Do("DEL", keys...))
}
func (entity *Redis) Ttl(key string) (int64, error) {
return libRedis.Int64(entity.Do("TTL", key))
}
func (entity *Redis) Pttl(key string) (int64, error) {
return libRedis.Int64(entity.Do("PTTL", key))
}
func (entity *Redis) HSet(key, field string, val interface{}, opt ...int) (int, error) {
if len(opt) > 0 {
noCache := opt[0]
if noCache == 1 {
return 0, nil
}
}
valStr := parseToString(val)
return libRedis.Int(entity.Do("HSET", key, field, valStr))
}
func parseToString(value interface{}) string {
switch value.(type) {
case string:
return value.(string)
default:
b, e := jsoniter.Marshal(value)
if e != nil {
return ""
}
return string(b)
}
}
func (entity *Redis) HGet(key, field string, opt ...int) ([]byte, error) {
if len(opt) > 0 {
noCache := opt[0]
if noCache == 1 {
return nil, nil
}
}
if res, err := libRedis.Bytes(entity.Do("HGET", key, field)); err == libRedis.ErrNil {
return nil, nil
} else {
return res, err
}
}
func (entity *Redis) HMGet(key string, fields ...string) ([][]byte, error) {
//1.初始化返回结果
res := make([][]byte, 0, len(fields))
var resErr error
//2.将多个key分批获取(每次32个)
pageNum := int(math.Ceil(float64(len(fields)) / float64(_CHUNK_SIZE)))
for i := 0; i < pageNum; i++ {
//2.1创建分批切片 []string
var end int
if i == (pageNum - 1) {
end = len(fields)
} else {
end = (i + 1) * _CHUNK_SIZE
}
chunk := fields[i*_CHUNK_SIZE : end]
//2.2分批切片的类型转换 => [][]byte
chunkLength := len(chunk)
fieldList := make([]interface{}, 0, chunkLength)
for _, v := range chunk {
fieldList = append(fieldList, v)
}
cacheRes, err := libRedis.ByteSlices(entity.Do("HMGET", libRedis.Args{}.Add(key).AddFlat(fieldList)...))
if err != nil {
for i := 0; i < chunkLength; i++ {
res = append(res, nil)
}
entity.LogWarn("cache_mget_error: ", err)
continue
} else {
res = append(res, cacheRes...)
}
}
return res, resErr
}
// HMSet 将一个map存到Redis hash
func (entity *Redis) HMSet(key string, fvmap map[string]interface{}) error {
_, err := entity.Do("HMSET", libRedis.Args{}.Add(key).AddFlat(fvmap)...)
return err
}
func (entity *Redis) HKeys(key string) ([][]byte, error) {
if res, err := libRedis.ByteSlices(entity.Do("HKEYS", key)); err == libRedis.ErrNil {
return nil, nil
} else {
return res, err
}
}
func (entity *Redis) HGetAll(key string) ([][]byte, error) {
if res, err := libRedis.ByteSlices(entity.Do("HGETALL", key)); err == libRedis.ErrNil {
return nil, nil
} else {
return res, err
}
}
func (entity *Redis) HLen(key string) (int64, error) {
if res, err := libRedis.Int64(entity.Do("HLEN", key)); err == libRedis.ErrNil {
return 0, nil
} else {
return res, err
}
}
func (entity *Redis) HVals(key string) ([][]byte, error) {
if res, err := libRedis.ByteSlices(entity.Do("HVALS", key)); err == libRedis.ErrNil {
return nil, nil
} else {
return res, err
}
}
func (entity *Redis) HIncrBy(key, field string, value int64) (int64, error) {
return libRedis.Int64(entity.Do("HINCRBY", key, field, value))
}
func (entity *Redis) HExists(key string, field string) (bool, error) {
if res, err := libRedis.Bool(entity.Do("HEXISTS", key, field)); err == libRedis.ErrNil {
return false, nil
} else {
return res, err
}
}
func (entity *Redis) Get(key string) ([]byte, error) {
if res, err := libRedis.Bytes(entity.Do("GET", key)); err == libRedis.ErrNil {
return nil, nil
} else {
return res, err
}
}
func (entity *Redis) MGet(keys ...string) [][]byte {
//1.初始化返回结果
res := make([][]byte, 0, len(keys))
//2.将多个key分批获取(每次32个)
pageNum := int(math.Ceil(float64(len(keys)) / float64(_CHUNK_SIZE)))
for n := 0; n < pageNum; n++ {
//2.1创建分批切片 []string
var end int
if n != (pageNum - 1) {
end = (n + 1) * _CHUNK_SIZE
} else {
end = len(keys)
}
chunk := keys[n*_CHUNK_SIZE : end]
//2.2分批切片的类型转换 => []interface{}
chunkLength := len(chunk)
keyList := make([]interface{}, 0, chunkLength)
for _, v := range chunk {
keyList = append(keyList, v)
}
cacheRes, err := libRedis.ByteSlices(entity.Do("MGET", keyList...))
if err != nil {
for i := 0; i < len(keyList); i++ {
res = append(res, nil)
}
} else {
res = append(res, cacheRes...)
}
}
return res
}
func (entity *Redis) MSet(values ...interface{}) error {
_, err := entity.Do("MSET", values...)
return err
}
func (entity *Redis) Set(key string, value interface{}, expire ...int64) error {
var res string
var err error
if expire == nil {
res, err = libRedis.String(entity.Do("SET", key, value))
} else {
res, err = libRedis.String(entity.Do("SET", key, value, "EX", expire[0]))
}
if err != nil {
return err
} else if strings.ToLower(res) != "ok" {
return errors.New("set result not OK")
}
return nil
}
func (entity *Redis) SetEx(key string, value interface{}, expire int64) error {
return entity.Set(key, value, expire)
}
func (entity *Redis) Append(key string, value interface{}) (int, error) {
return libRedis.Int(entity.Do("APPEND", key, value))
}
func (entity *Redis) Incr(key string) (int64, error) {
return libRedis.Int64(entity.Do("INCR", key))
}
func (entity *Redis) IncrBy(key string, value int64) (int64, error) {
return libRedis.Int64(entity.Do("INCRBY", key, value))
}
func (entity *Redis) IncrByFloat(key string, value float64) (float64, error) {
return libRedis.Float64(entity.Do("INCRBYFLOAT", key, value))
}
func (entity *Redis) Decr(key string) (int64, error) {
return libRedis.Int64(entity.Do("DECR", key))
}
func (entity *Redis) DecrBy(key string, value int64) (int64, error) {
return libRedis.Int64(entity.Do("DECRBY", key, value))
}