-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdable.go
348 lines (290 loc) · 8.21 KB
/
cmdable.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package redis
import (
"context"
"github.com/go-redis/redis/v8"
"github.com/team-ide/go-tool/util"
"go.uber.org/zap"
"sort"
"time"
)
func Keys(ctx context.Context, client redis.Cmdable, database int, pattern string, size int64) (keysResult *KeysResult, err error) {
keysResult = &KeysResult{}
var list []string
cmdKeys := client.Keys(ctx, pattern)
list, err = cmdKeys.Result()
if err != nil {
return
}
keysResult.Count = len(list)
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
}
func Exists(ctx context.Context, client redis.Cmdable, key string) (res int64, err error) {
cmdExists := client.Exists(ctx, key)
res, err = cmdExists.Result()
return
}
func ValueType(ctx context.Context, client redis.Cmdable, key string) (ValueType string, err error) {
cmdType := client.Type(ctx, key)
ValueType, err = cmdType.Result()
if err == redis.Nil {
err = nil
return
}
return
}
// Expire 让给定键在指定的秒数之后过期
func Expire(ctx context.Context, client redis.Cmdable, key string, expire int64) (res bool, err error) {
cmd := client.Expire(ctx, key, time.Duration(expire)*time.Second)
res, err = cmd.Result()
if err == redis.Nil {
err = nil
return
}
return
}
// Persist 移除键的过期时间
func Persist(ctx context.Context, client redis.Cmdable, key string) (res bool, err error) {
cmd := client.Persist(ctx, key)
res, err = cmd.Result()
if err == redis.Nil {
err = nil
return
}
return
}
// TTL 查看给定键距离过期还有多少秒
func TTL(ctx context.Context, client redis.Cmdable, key string) (res int64, err error) {
cmd := client.TTL(ctx, key)
r, err := cmd.Result()
if err == redis.Nil {
err = nil
return
}
if err != nil {
return
}
if r > 0 {
res = int64(r / time.Second)
}
return
}
func GetValueInfo(ctx context.Context, client redis.Cmdable, database int, key string, valueStart, valueSize int64) (valueInfo *ValueInfo, err error) {
var valueType string
valueType, err = ValueType(ctx, client, key)
if err != nil {
return
}
valueInfo = &ValueInfo{
Key: key,
Database: database,
}
valueInfo.MemoryUsage, _ = MemoryUsage(ctx, client, key)
valueInfo.TTL, _ = TTL(ctx, client, key)
var value interface{}
if valueType == "none" {
} else if valueType == "string" {
cmd := client.Get(ctx, key)
value, err = cmd.Result()
if err != nil {
util.Logger.Error("Get Error", zap.Any("key", key), zap.Error(err))
return
}
} else if valueType == "list" {
cmd := client.LLen(ctx, key)
valueInfo.ValueCount, err = cmd.Result()
if err != nil {
util.Logger.Error("LLen Error", zap.Any("key", key), zap.Error(err))
return
}
valueInfo.ValueStart = valueStart
valueInfo.ValueEnd = valueInfo.ValueStart + valueSize
var list []string
cmdRange := client.LRange(ctx, key, valueInfo.ValueStart, valueInfo.ValueEnd)
list, err = cmdRange.Result()
if err != nil {
util.Logger.Error("LRange Error", zap.Any("key", key), zap.Error(err))
return
}
if int64(len(list)) <= valueSize || valueSize < 0 {
value = list
} else {
value = list[0:valueSize]
}
} else if valueType == "set" {
cmdSCard := client.SCard(ctx, key)
valueInfo.ValueCount, err = cmdSCard.Result()
if err != nil {
util.Logger.Error("SCard Error", zap.Any("key", key), zap.Error(err))
return
}
valueInfo.ValueStart = valueStart
valueInfo.ValueEnd = valueInfo.ValueStart + valueSize
var list []string
cmd := client.SScan(ctx, key, uint64(valueInfo.ValueStart), "*", valueInfo.ValueEnd)
list, valueInfo.Cursor, err = cmd.Result()
if err != nil {
util.Logger.Error("SScan Error", zap.Any("key", key), zap.Error(err))
return
}
if int64(len(list)) <= valueSize || valueSize < 0 {
value = list
} else {
value = list[0:valueSize]
}
} else if valueType == "hash" {
cmdHLen := client.HLen(ctx, key)
valueInfo.ValueCount, err = cmdHLen.Result()
if err != nil {
util.Logger.Error("HLen Error", zap.Any("key", key), zap.Error(err))
return
}
valueInfo.ValueStart = valueStart * 2
valueInfo.ValueEnd = valueInfo.ValueStart + valueSize*2
cmdHScan := client.HScan(ctx, key, uint64(valueInfo.ValueStart), "*", valueInfo.ValueEnd)
var keyValueList []string
keyValueList, valueInfo.Cursor, err = cmdHScan.Result()
if err != nil {
util.Logger.Error("HScan Error", zap.Any("key", key), zap.Error(err))
return
}
var keyValueListSize = int64(len(keyValueList))
var keyValue = map[string]string{}
for i := int64(0); i < valueSize*2; i++ {
if i >= keyValueListSize {
break
}
filed := keyValueList[i]
filedValue := ""
if i+1 < keyValueListSize {
filedValue = keyValueList[i+1]
}
keyValue[filed] = filedValue
i++
}
value = keyValue
} else {
util.Logger.Warn("valueType not support", zap.Any("valueType", valueType), zap.Any("key", key))
}
valueInfo.ValueType = valueType
valueInfo.Value = value
return
}
func Get(ctx context.Context, client redis.Cmdable, key string) (value string, err error) {
cmd := client.Get(ctx, key)
value, err = cmd.Result()
return
}
func Set(ctx context.Context, client redis.Cmdable, key string, value string) (err error) {
cmd := client.Set(ctx, key, value, time.Duration(0))
_, err = cmd.Result()
return
}
func SAdd(ctx context.Context, client redis.Cmdable, key string, value string) (err error) {
cmd := client.SAdd(ctx, key, value)
_, err = cmd.Result()
return
}
func SRem(ctx context.Context, client redis.Cmdable, key string, value string) (err error) {
cmd := client.SRem(ctx, key, value)
_, err = cmd.Result()
return
}
func LPush(ctx context.Context, client redis.Cmdable, key string, value string) (err error) {
cmd := client.LPush(ctx, key, value)
_, err = cmd.Result()
return
}
func RPush(ctx context.Context, client redis.Cmdable, key string, value string) (err error) {
cmd := client.RPush(ctx, key, value)
_, err = cmd.Result()
return
}
func LSet(ctx context.Context, client redis.Cmdable, key string, index int64, value string) (err error) {
cmd := client.LSet(ctx, key, index, value)
_, err = cmd.Result()
return
}
func LRem(ctx context.Context, client redis.Cmdable, key string, count int64, value string) (err error) {
cmd := client.LRem(ctx, key, count, value)
_, err = cmd.Result()
return
}
func HSet(ctx context.Context, client redis.Cmdable, key string, field string, value string) (err error) {
cmd := client.HSet(ctx, key, field, value)
_, err = cmd.Result()
return
}
func HDel(ctx context.Context, client redis.Cmdable, key string, field string) (err error) {
cmd := client.HDel(ctx, key, field)
_, err = cmd.Result()
return
}
func HGet(ctx context.Context, client redis.Cmdable, key string, field string) (value string, err error) {
cmd := client.HGet(ctx, key, field)
value, err = cmd.Result()
if err == redis.Nil {
err = nil
return
}
return
}
func SetBit(ctx context.Context, client redis.Cmdable, key string, offset int64, value int) (err error) {
cmd := client.SetBit(ctx, key, offset, value)
err = cmd.Err()
return
}
func BitCount(ctx context.Context, client redis.Cmdable, key string) (count int64, err error) {
cmd := client.BitCount(ctx, key, nil)
count, err = cmd.Result()
return
}
func Info(ctx context.Context, client redis.Cmdable) (res string, err error) {
cmd := client.Info(ctx)
res, err = cmd.Result()
return
}
func MemoryUsage(ctx context.Context, client redis.Cmdable, key string) (size int64, err error) {
cmd := client.MemoryUsage(ctx, key)
size, err = cmd.Result()
return
}
func Del(ctx context.Context, client redis.Cmdable, key string) (count int, err error) {
cmd := client.Del(ctx, key)
_, err = cmd.Result()
if err == nil {
count++
}
return
}
func DelPattern(ctx context.Context, client redis.Cmdable, database int, pattern string) (count int, err error) {
count = 0
keysResult, err := Keys(ctx, client, database, pattern, -1)
if err != nil {
return
}
for _, keyInfo := range keysResult.KeyList {
cmd := client.Del(ctx, keyInfo.Key)
_, err = cmd.Result()
if err == nil {
count++
} else {
return
}
}
return
}