-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
159 lines (152 loc) · 3.9 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
package main
import (
"log"
"strings"
)
import "github.com/fzzy/radix/redis"
func GetRedisClient() *redis.Client {
app.Tracef("Redis::NewRedis()", "Opening new redis connection")
redisClient, err := redis.Dial("tcp", "redis1.btstat.internal:6379")
if err != nil {
log.Fatalf("Could not connect to redis: %s\n", err)
}
return redisClient
}
func RedisIntCmd(
redisClient *redis.Client,
command string,
args ...string,
) (int, error) {
app.Tracef("RedisIntCmd()", "Executing command: %s %s", command, strings.Join(args, " "))
argsIface := make([]interface{}, len(args))
for index, arg := range args {
argsIface[index] = interface{}(arg)
}
reply := redisClient.Cmd(command, argsIface)
if reply.Type == redis.ErrorReply {
if reply.Err.Error() == "use of closed network connection" {
log.Printf("Reconnecting redis (RedisIntCmd)\n")
redisClient = GetRedisClient()
return RedisIntCmd(redisClient, command, args...)
}
app.Debugf(
"Could not execute '%s %s': %s\n",
command,
strings.Join(args, " "),
reply.Err,
)
return 0, reply.Err
}
val, err := reply.Int()
if err != nil {
app.Debugf(
"Could not parse integer reply from '%s': %s (command: %s %s)\n",
reply.String(),
err,
command,
strings.Join(args, " "),
)
return 0, reply.Err
}
return val, nil
}
func RedisStrCmd(
redisClient *redis.Client,
command string,
args ...string,
) (string, error) {
app.Tracef("RedisStrCmd()", "Executing command: %s %s", command, strings.Join(args, " "))
argsIface := make([]interface{}, len(args))
for index, arg := range args {
argsIface[index] = interface{}(arg)
}
reply := redisClient.Cmd(command, argsIface)
if reply.Type == redis.ErrorReply {
if reply.Err.Error() == "use of closed network connection" {
log.Printf("Reconnecting redis (RedisStrCmd)\n")
redisClient = GetRedisClient()
return RedisStrCmd(redisClient, command, args...)
}
app.Debugf(
"Could not execute '%s %s': %s\n",
command,
strings.Join(args, " "),
reply.Err,
)
return "", reply.Err
}
return reply.String(), nil
}
func RedisStrsCmd(
redisClient *redis.Client,
command string,
args ...string,
) ([]string, error) {
app.Tracef("RedisStrsCmd()", "Executing command: %s %s", command, strings.Join(args, " "))
argsIface := make([]interface{}, len(args))
for index, arg := range args {
argsIface[index] = interface{}(arg)
}
reply := redisClient.Cmd(command, argsIface)
if reply.Type == redis.NilReply {
return []string{}, nil
}
if reply.Type == redis.ErrorReply {
if reply.Err.Error() == "use of closed network connection" {
log.Printf("Reconnecting redis (RedisStrsCmd)\n")
redisClient = GetRedisClient()
return RedisStrsCmd(redisClient, command, args...)
}
if reply.Err.Error() == "wrong type" {
return []string{}, nil
}
app.Debugf(
"Could not execute '%s %s': %s\n",
command,
strings.Join(args, " "),
reply.Err,
)
return []string{}, nil
}
if reply.Type == redis.NilReply {
return []string{}, nil
}
values, err := reply.List()
if err != nil {
app.Debugf(
"Could not execute '%s %s': %s\n",
command,
strings.Join(args, " "),
err,
)
return []string{}, err
}
return values, nil
}
func RedisCmd(
redisClient *redis.Client,
command string,
args ...string,
) (*redis.Reply, error) {
app.Tracef("RedisCmd()", "Executing command: %s %s", command, strings.Join(args, " "))
argsIface := make([]interface{}, len(args))
for index, arg := range args {
argsIface[index] = interface{}(arg)
}
reply := redisClient.Cmd(command, argsIface)
if reply.Type == redis.ErrorReply {
if reply.Err.Error() == "use of closed network connection" {
log.Printf("Reconnecting redis (RedisCmd %s)\n", command)
redisClient = GetRedisClient()
return RedisCmd(redisClient, command, args...)
}
app.Debugf(
"Could not execute '%s %s': %s\n",
command,
strings.Join(args, " "),
reply.Err,
)
return nil, reply.Err
}
return reply, nil
}