/
redis.go
328 lines (263 loc) · 7.49 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
// nolint
package redis
import (
"fmt"
"time"
"github.com/zhufuyi/pkg/logger"
"github.com/gomodule/redigo/redis"
)
// 当前应用允许最大连接数,最大不能超过redis极限连接数
// 如果多个项目共用同一个redis,要考虑每个项目限制连接数,防止过量连接造成redis卡死
var maxActiveCount = 2800
var pool *RedisPool
// ErrNil redis空值
var ErrNil = redis.ErrNil
// GetConn 对redis操作前,先要调用此函数从redis连接池获取一个连接,要对结果做错误判断,防止获取nil的连接,然后对空指针操作造成panic
func GetConn() (RedisConn, error) {
if pool == nil {
logger.Panic("redis pool is nil, go to connect redis first, eg: redis.NewRedisPool(server, password string)")
}
// 超出redis承受的极限最大连接数,直接拦截,并返回错误
if pool.ActiveCount() > maxActiveCount {
return nil, fmt.Errorf("redis connect clients exceeded the limit of %d", maxActiveCount)
}
return pool.Get(), nil
}
// RedisPool redis连接池
type RedisPool struct {
redis.Pool
}
// Get 从连接池获取redis连接
func (r *RedisPool) Get() RedisConn {
conn := r.Pool.Get()
return &DefaultRedisConn{Conn: conn}
}
// RedisConn redis接口
type RedisConn interface {
redis.Conn
WithLog() RedisConn
}
// DefaultRedisConn 默认redis连接信息
type DefaultRedisConn struct {
redis.Conn
printLog bool
}
// WithLog 设置打印日志
func (d *DefaultRedisConn) WithLog() RedisConn {
d.printLog = true
return d
}
// Do The Do method combines the functionality of the Send, Flush and Receive methods.
func (d *DefaultRedisConn) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
result, err := d.Conn.Do(commandName, args...)
if err != nil {
if d.printLog {
d.printLog = false
logger.WithFields(
logger.Err(err),
logger.String("command", joinCommandAndArgs(commandName, args...)),
).Error("redis do error")
}
return result, err
}
if d.printLog {
d.printLog = false
logger.WithFields(
logger.String("command", joinCommandAndArgs(commandName, args...)),
anyField("result", result),
).Info("redis do success")
}
return result, err
}
// Send writes the command to the client's output buffer.
func (d *DefaultRedisConn) Send(commandName string, args ...interface{}) error {
err := d.Conn.Send(commandName, args...)
if err != nil {
if d.printLog {
d.printLog = false
logger.WithFields(
logger.Err(err),
logger.String("command", joinCommandAndArgs(commandName, args...)),
).Error("redis send error")
}
return err
}
if d.printLog {
d.printLog = false
logger.WithFields(
logger.String("command", joinCommandAndArgs(commandName, args...)),
).Info("redis send success")
}
return err
}
// Flush flushes the output buffer to the Redis server.
func (d *DefaultRedisConn) Flush() error {
err := d.Conn.Flush()
if err != nil {
if d.printLog {
d.printLog = false
logger.WithFields(logger.Err(err)).Error("redis flush error")
}
return err
}
if d.printLog {
d.printLog = false
logger.WithFields().Info("redis flush success")
}
return nil
}
// Receive receives a single reply from the Redis server
func (d *DefaultRedisConn) Receive() (reply interface{}, err error) {
result, err := d.Conn.Receive()
if err != nil {
if d.printLog {
d.printLog = false
logger.WithFields(logger.Err(err)).Error("redis receive error")
}
return result, err
}
if d.printLog {
d.printLog = false
logger.WithFields(anyField("result", result)).Info("redis receive success")
}
return result, err
}
// NewRedisPool connect redis,if test ping failed,return error
func NewRedisPool(server, password string) error {
pool = &RedisPool{
Pool: redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", server)
if err != nil {
return nil, err
}
if _, err = c.Do("AUTH", password); err != nil {
c.Close()
return nil, err
}
_, _ = c.Do("select", 0)
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
},
}
rconn, _ := GetConn()
defer rconn.Close()
_, err := rconn.Do("PING")
return err
}
// NewRedisPoolWithNoAuth connect redis with no auth, if test ping failed,return error
func NewRedisPoolWithNoAuth(server string) error {
pool = &RedisPool{
Pool: redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", server)
if err != nil {
return nil, err
}
_, _ = c.Do("select", 0)
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
},
}
rconn, _ := GetConn()
defer rconn.Close()
_, err := rconn.Do("PING")
return err
}
// ------------------------------------------- 重新包装函数 ---------------------------------------
// Int 整型
func Int(reply interface{}, err error) (int, error) {
return redis.Int(reply, err)
}
// Int64 64位整型
func Int64(reply interface{}, err error) (int64, error) {
return redis.Int64(reply, err)
}
// Uint64 64位非负数整型
func Uint64(reply interface{}, err error) (uint64, error) {
return redis.Uint64(reply, err)
}
// Float64 64位浮点数
func Float64(reply interface{}, err error) (float64, error) {
return redis.Float64(reply, err)
}
// String 字符串类型
func String(reply interface{}, err error) (string, error) {
return redis.String(reply, err)
}
// Bytes 字节流类型
func Bytes(reply interface{}, err error) ([]byte, error) {
return redis.Bytes(reply, err)
}
// Bool 布尔类型
func Bool(reply interface{}, err error) (bool, error) {
return redis.Bool(reply, err)
}
// Values 任意类型
func Values(reply interface{}, err error) ([]interface{}, error) {
return redis.Values(reply, err)
}
// Strings 字符串slice类型
func Strings(reply interface{}, err error) ([]string, error) {
return redis.Strings(reply, err)
}
// ByteSlices 字节流slice类型
func ByteSlices(reply interface{}, err error) ([][]byte, error) {
return redis.ByteSlices(reply, err)
}
// Ints 整型slice类型
func Ints(reply interface{}, err error) ([]int, error) {
return redis.Ints(reply, err)
}
// StringMap kv都是字符串的map类型
func StringMap(result interface{}, err error) (map[string]string, error) {
return redis.StringMap(result, err)
}
// IntMap key字符串,val为int的map类型
func IntMap(result interface{}, err error) (map[string]int, error) {
return redis.IntMap(result, err)
}
// Int64Map key字符串,val为int64的map类型
func Int64Map(result interface{}, err error) (map[string]int64, error) {
return redis.Int64Map(result, err)
}
// -------------------------------------------------------------------------------------------------
// 转换类型
func anyField(key string, a interface{}) logger.Field {
switch a.(type) {
case []byte:
return logger.String(key, string(a.([]byte)))
case string:
return logger.String(key, a.(string))
case []interface{}:
value := []string{}
for _, v := range a.([]interface{}) {
if _, ok := v.([]byte); ok {
value = append(value, fmt.Sprintf("%s", v))
}
}
return logger.Any(key, value)
default:
return logger.String(key, fmt.Sprintf("%v", a))
}
}
// 把连接命令和参数转换为字符串
func joinCommandAndArgs(commandName string, args ...interface{}) string {
commandArgs := commandName
for _, arg := range args {
commandArgs += fmt.Sprintf(" %v", arg)
}
return commandArgs
}