redisCommon.go
4.88 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
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
package sdkredis
import (
"common"
"fmt"
"time"
"github.com/garyburd/redigo/redis"
)
//正式环境
/*var REDIS_HOST string = "r-2ze79cf083318224.redis.rds.aliyuncs.com:6379"
var REDIS_PWD string = "Icesimba858680668866Redis"
var REDIS_DB int = 2
*/
type Redis struct {
Config *common.Config // 配置文件
RedisPool *redis.Pool // 连接池
}
var RedisClient *Redis
func RedisInit(config *common.Config) {
RedisClient = NewRedis(config)
}
func NewRedis(config *common.Config) (r *Redis) {
r = new(Redis)
r.Config = config
fmt.Printf("redis host[%v]port[%v]passwd[%v]dbindex[%d]\n", r.Config.SDKRedis.Address, r.Config.SDKRedis.Port, r.Config.SDKRedis.Password, r.Config.SDKRedis.DBIndex)
/*
var REDIS_HOST string = "r-2ze1633d3a4dc424.redis.rds.aliyuncs.com:6379"
var REDIS_PWD string = "Icesimba8866"
var REDIS_DB int = 2
*/
var REDIS_HOST string = r.Config.SDKRedis.Address + ":" + r.Config.SDKRedis.Port
var REDIS_PWD string = r.Config.SDKRedis.Password
var REDIS_DB int = r.Config.SDKRedis.DBIndex
r.RedisPool = &redis.Pool{
MaxIdle: 50, // 最大的空闲连接数
MaxActive: 100, // 最大的激活连接数
IdleTimeout: 180 * time.Second, // 最大的空闲连接等待时间,超过此时间后,空闲连接将被关闭
//Wait: true, //是否在超过最大连接数的时候等待
Dial: func() (redis.Conn, error) {
conn, err := redis.Dial("tcp", REDIS_HOST)
if err != nil {
return nil, err
}
// 验证密码
if _, err := conn.Do("AUTH", REDIS_PWD); err != nil {
conn.Close()
return nil, err
}
// 选择db
if _, err := conn.Do("SELECT", REDIS_DB); err != nil {
return nil, err
}
return conn, nil
},
TestOnBorrow: func(conn redis.Conn, t time.Time) error {
_, err := conn.Do("PING")
return err
},
}
return
}
/**
* 从连接池获得连接
*/
func (self *Redis) GetConn() redis.Conn {
return self.RedisPool.Get()
}
/**
* 释放redis连接
*/
func RedisClose(conn redis.Conn) {
conn.Close()
}
/**
* 执行redis命令
*/
func RedisCommand(conn redis.Conn, commandName string, args ...interface{}) (reply interface{}, err error) {
reply, err = conn.Do(commandName, args...)
return
}
/**
* 选择数据库
*/
func RedisDBSelect(conn redis.Conn, db int) (reply interface{}, err error) {
reply, err = RedisCommand(conn, "select", db)
return
}
/**
* set key value
*/
func RedisSetKV(conn redis.Conn, key string, value string) (reply interface{}, err error) {
reply, err = RedisCommand(conn, "set", key, value)
return
}
/**
* del key
*/
func RedisDelKey(conn redis.Conn, key string) (reply interface{}, err error) {
reply, err = RedisCommand(conn, "del", key)
return
}
/**
* keys命令
*/
func RedisGetKeys(conn redis.Conn, pattern string) (strlist []string, err error) {
reply, err := RedisCommand(conn, "keys", pattern)
strlist, _ = redis.Strings(reply, nil)
return
}
/**
* get key value
*/
func RedisGetKV(conn redis.Conn, key string) (value string, err error) {
reply, err := RedisCommand(conn, "get", key)
value, _ = redis.String(reply, nil)
return
}
/**
* add sortset
*/
func RedisZAddSortSet(conn redis.Conn, sortsetName string, score float64, key string) (reply interface{}, err error) {
reply, err = RedisCommand(conn, "zadd", sortsetName, score, key)
return
}
/**
* sortset rem
*/
func RedisZSortSetRem(conn redis.Conn, sortsetName string, key string) (reply interface{}, err error) {
reply, err = RedisCommand(conn, "zrem", sortsetName, key)
return
}
/**
* sort正序
*/
func RedisSort(conn redis.Conn, sortsetName string, start int, stop int) (strlist []string, err error) {
reply, err := RedisCommand(conn, "zrange", sortsetName, start, stop, "withscores")
strlist, _ = redis.Strings(reply, nil)
return
}
/**
* sort倒序
*/
func RedisReverseSort(conn redis.Conn, sortsetName string, start int, stop int) (strlist []string, err error) {
reply, err := RedisCommand(conn, "zrevrange", sortsetName, start, stop, "withscores")
strlist, _ = redis.Strings(reply, nil)
return
}
/**
* rpush
*/
func RedisRPush(conn redis.Conn, queueName string, msg string) (reply interface{}, err error) {
reply, err = RedisCommand(conn, "rpush", queueName, msg)
return
}
/**
* lpop
*/
func RedisLPop(conn redis.Conn, queueName string) (msg string, err error) {
reply, err := RedisCommand(conn, "lpop", queueName)
msg, _ = redis.String(reply, nil)
return
}
/**
* incr
*/
func RedisIncr(conn redis.Conn, key string) (reply interface{}, err error) {
reply, err = RedisCommand(conn, "incr", key)
return
}
/**
* incrby
*/
func RedisIncrBy(conn redis.Conn, key string, num int64) (reply interface{}, err error) {
reply, err = RedisCommand(conn, "incrby", key, num)
return
}
/*
* zrank
*/
func RedisZRank(conn redis.Conn, key string, member string) (reply interface{}, err error) {
return RedisCommand(conn, "zrank", key, member)
}