-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.go
325 lines (258 loc) · 7.29 KB
/
cluster.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
package redis
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"github.com/go-redis/redis/v8"
"github.com/team-ide/go-tool/util"
"io/ioutil"
"sort"
"time"
)
func CreateClusterService(servers []string, username string, auth string, certPath string) (service *ClusterService, err error) {
service = &ClusterService{
servers: servers,
username: username,
auth: auth,
certPath: certPath,
}
err = service.init()
return
}
type ClusterService struct {
servers []string
auth string
username string
certPath string
redisCluster *redis.ClusterClient
lastUseTime int64
}
func (this_ *ClusterService) init() (err error) {
options := &redis.ClusterOptions{
Addrs: this_.servers,
DialTimeout: 100 * time.Second,
ReadTimeout: 100 * time.Second,
WriteTimeout: 100 * time.Second,
Username: this_.username,
Password: this_.auth,
}
if this_.certPath != "" {
certPool := x509.NewCertPool()
var pemCerts []byte
pemCerts, err = ioutil.ReadFile(this_.certPath)
if err != nil {
return
}
if !certPool.AppendCertsFromPEM(pemCerts) {
err = errors.New("证书[" + this_.certPath + "]解析失败")
return
}
TLSClientConfig := &tls.Config{
InsecureSkipVerify: true,
}
TLSClientConfig.RootCAs = certPool
options.TLSConfig = TLSClientConfig
}
redisCluster := redis.NewClusterClient(options)
this_.redisCluster = redisCluster
return
}
func (this_ *ClusterService) GetWaitTime() int64 {
return 10 * 60 * 1000
}
func (this_ *ClusterService) GetLastUseTime() int64 {
return this_.lastUseTime
}
func (this_ *ClusterService) SetLastUseTime() {
this_.lastUseTime = util.GetNowTime()
}
func (this_ *ClusterService) Stop() {
_ = this_.redisCluster.Close()
}
func (this_ *ClusterService) GetClient(ctx context.Context, database int) (client redis.Cmdable, err error) {
defer func() {
this_.lastUseTime = util.GetNowTime()
}()
client = this_.redisCluster
if ctx != nil && database >= 0 {
return
}
return
}
func (this_ *ClusterService) Info(ctx context.Context) (res string, err error) {
client, err := this_.GetClient(ctx, 0)
if err != nil {
return
}
return Info(ctx, client)
}
func (this_ *ClusterService) Keys(ctx context.Context, database int, pattern string, size int64) (keysResult *KeysResult, err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return ClusterKeys(ctx, client.(*redis.ClusterClient), database, pattern, size)
}
func (this_ *ClusterService) Expire(ctx context.Context, database int, key string, expire int64) (res bool, err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return Expire(ctx, client, key, expire)
}
func (this_ *ClusterService) TTL(ctx context.Context, database int, key string) (res int64, err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return TTL(ctx, client, key)
}
func (this_ *ClusterService) Persist(ctx context.Context, database int, key string) (res bool, err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return Persist(ctx, client, key)
}
func (this_ *ClusterService) Exists(ctx context.Context, database int, key string) (res int64, err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return Exists(ctx, client, key)
}
func (this_ *ClusterService) Get(ctx context.Context, database int, key string, valueStart, valueSize int64) (valueInfo *ValueInfo, err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return Get(ctx, client, database, key, valueStart, valueSize)
}
func (this_ *ClusterService) Set(ctx context.Context, database int, key string, value string) (err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return Set(ctx, client, key, value)
}
func (this_ *ClusterService) SAdd(ctx context.Context, database int, key string, value string) (err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return SAdd(ctx, client, key, value)
}
func (this_ *ClusterService) SRem(ctx context.Context, database int, key string, value string) (err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return SRem(ctx, client, key, value)
}
func (this_ *ClusterService) LPush(ctx context.Context, database int, key string, value string) (err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return LPush(ctx, client, key, value)
}
func (this_ *ClusterService) RPush(ctx context.Context, database int, key string, value string) (err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return RPush(ctx, client, key, value)
}
func (this_ *ClusterService) LSet(ctx context.Context, database int, key string, index int64, value string) (err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return LSet(ctx, client, key, index, value)
}
func (this_ *ClusterService) LRem(ctx context.Context, database int, key string, count int64, value string) (err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return LRem(ctx, client, key, count, value)
}
func (this_ *ClusterService) HSet(ctx context.Context, database int, key string, field string, value string) (err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return HSet(ctx, client, key, field, value)
}
func (this_ *ClusterService) HDel(ctx context.Context, database int, key string, field string) (err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return HDel(ctx, client, key, field)
}
func (this_ *ClusterService) Del(ctx context.Context, database int, key string) (count int, err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
return Del(ctx, client, key)
}
func (this_ *ClusterService) DelPattern(ctx context.Context, database int, pattern string) (count int, err error) {
client, err := this_.GetClient(ctx, database)
if err != nil {
return
}
keysResult, err := ClusterKeys(ctx, client.(*redis.ClusterClient), database, pattern, -1)
if err != nil {
return
}
count = 0
for _, keyInfo := range keysResult.KeyList {
_, err = Del(ctx, client, keyInfo.Key)
if err == nil {
count++
}
}
return
}
type KeysResult struct {
Count int `json:"count"`
KeyList []*KeyInfo `json:"keyList"`
}
type KeyInfo struct {
Database int `json:"database"`
Key string `json:"key"`
}
func ClusterKeys(ctx context.Context, client *redis.ClusterClient, database int, pattern string, size int64) (keysResult *KeysResult, err error) {
keysResult = &KeysResult{}
var list []string
err = client.ForEachMaster(ctx, func(ctx context.Context, client *redis.Client) (err error) {
var ls []string
cmd := client.Keys(ctx, pattern)
ls, err = cmd.Result()
if err != nil {
return
}
keysResult.Count += len(ls)
list = append(list, ls...)
return
})
sor := sort.StringSlice(list)
sor.Sort()
var keys []string
if int64(keysResult.Count) <= size || size < 0 {
keys = list
} else {
keys = list[0:size]
}
for _, key := range keys {
info := &KeyInfo{
Key: key,
Database: database,
}
keysResult.KeyList = append(keysResult.KeyList, info)
}
return
}