redis.go
3.8 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
package redis
import (
"errors"
"fmt"
"github.com/gomodule/redigo/redis"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/config"
"time"
)
const (
INFINITE int64 = 0
SECOND int64 = 1
MINUTE int64 = 60
HOUR int64 = 3600
DAY int64 = 24 * HOUR
WEEK int64 = 7 * DAY
MONTH int64 = 30 * DAY
YEAR int64 = 365 * DAY
)
var (
// 连接池
redisPool *redis.Pool
)
func InitWithDb(size int, addr, password, db string) error {
redisPool = &redis.Pool{
MaxIdle: size,
IdleTimeout: 180 * time.Second,
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
Dial: func() (redis.Conn, error) {
return dialWithDB(addr, password, db)
},
}
_, err := ping()
return err
}
func Init(conf config.Redis) error {
redisPool = &redis.Pool{
MaxIdle: conf.MaxIdle,
IdleTimeout: 180 * time.Second,
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
Dial: func() (redis.Conn, error) {
return dial(conf.Addr, conf.Password)
},
}
_, err := ping()
return err
}
func ping() (bool, error) {
c := redisPool.Get()
defer c.Close()
data, err := c.Do("PING")
if err != nil || data == nil {
return false, err
}
return (data == "PONG"), nil
}
func dial(addr, password string) (redis.Conn, error) {
c, err := redis.Dial("tcp", addr)
if err != nil {
return nil, err
}
if password != "" {
if _, err := c.Do("AUTH", password); err != nil {
c.Close()
return nil, err
}
}
return c, err
}
func dialWithDB(addr, password, db string) (redis.Conn, error) {
c, err := dial(addr, password)
if err != nil {
return nil, err
}
if _, err := c.Do("SELECT", db); err != nil {
c.Close()
return nil, err
}
return c, err
}
//判断键是否存在
func Exists(key string) (bool, error) {
if len(key) <= 0 {
return false, errors.New("Empty key")
}
c := redisPool.Get()
defer c.Close()
exists, err := redis.Bool(c.Do("EXISTS", key))
return exists, err
}
//删除指定键
func Del(key string) (bool, error) {
if len(key) <= 0 {
return false, errors.New("Empty key")
}
c := redisPool.Get()
defer c.Close()
return redis.Bool(c.Do("DEL", key))
}
//批量删除指定键
func DelMulti(key ...interface{}) (bool, error) {
if len(key) <= 0 {
return false, errors.New("Invalid argument")
}
c := redisPool.Get()
defer c.Close()
return redis.Bool(c.Do("DEL", key...))
}
// func LikeDeletes(key string) error {
// conn := RedisConn.Get()
// defer conn.Close()
// keys, err := redis.Strings(conn.Do("KEYS", "*"+key+"*"))
// if err != nil {
// return err
// }
// for _, key := range keys {
// _, err = Delete(key)
// if err != nil {
// return err
// }
// }
// return nil
// }
//设置制定键的生存周期
func Expire(key string, timeout int64) error {
if len(key) <= 0 || timeout < 0 {
return fmt.Errorf("Invalid argument: key=[%s] timeout=[%d]", key, timeout)
}
c := redisPool.Get()
defer c.Close()
if timeout == 0 {
return nil
}
_, err := c.Do("EXPIRE", key, timeout)
return err
}
func setExpire(c redis.Conn, key string, timeout int64) error {
if len(key) <= 0 || timeout < 0 {
return fmt.Errorf("Invalid argument: key=[%s] timeout=[%d]", key, timeout)
}
_, err := c.Do("EXPIRE", key, timeout)
return err
}
func DelPattern(pattern string) bool {
if len(pattern) <= 0 {
return false
}
c := redisPool.Get()
defer c.Close()
result, err := redis.Strings(c.Do("Keys", pattern))
if err != nil {
return false
}
// TODO:pipeline
for i := range result {
Del(result[i])
}
return true
}
func GetKeyPattern(pattern string) ([]string, error) {
if len(pattern) < 1 {
return nil, errors.New("Invalid argument")
}
c := redisPool.Get()
defer c.Close()
result, err := redis.Strings(c.Do("Keys", pattern))
if err != nil {
return nil, err
}
return result, nil
}