-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrings.go
369 lines (350 loc) · 10.6 KB
/
strings.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package red
import (
"context"
"errors"
xerr "github.com/goclub/error"
xtime "github.com/goclub/time"
"strconv"
"time"
)
type APPEND struct {
Key string
Value string
}
func (data APPEND) Do(ctx context.Context, client Connecter) (length uint64, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"APPEND", data.Key, data.Value}
value, _, err := client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
length = uint64(value)
return
}
type BITCOUNT struct {
Key string
// start and end offset unit byte (8bit)
StartByte OptionInt64
EndByte OptionInt64
}
func (data BITCOUNT) Do(ctx context.Context, client Connecter) (length uint64, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"BITCOUNT", data.Key}
if data.StartByte.Valid {
args = append(args, strconv.FormatInt(data.StartByte.Int64, 10))
}
if data.EndByte.Valid {
args = append(args, strconv.FormatInt(data.EndByte.Int64, 10))
}
value, _, err := client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
length = uint64(value)
return
}
type BITFIELD struct {
Key string
Args []string
}
func (data BITFIELD) Do(ctx context.Context, client Connecter) (reply []OptionInt64, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"BITFIELD", data.Key}
args = append(args, data.Args...)
return client.DoArrayIntegerReply(ctx, args)
}
// bit operation
type BITOP struct {
AND bool
OR bool
XOR bool
NOT bool
DestKey string
Key string
Keys []string
}
func (data BITOP) Do(ctx context.Context, client Connecter) (size uint64, err error) {
args := []string{"BITOP"}
if data.AND {
args = append(args, "AND")
}
if data.OR {
args = append(args, "OR")
}
if data.XOR {
args = append(args, "XOR")
}
if data.NOT {
args = append(args, "NOT")
}
args = append(args, data.DestKey)
if data.Key != "" {
data.Keys = []string{data.Key}
}
if len(data.Keys) == 0 { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args = append(args, data.Keys...)
value,_, err := client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
size = uint64(value)
return
}
type BITPOS struct {
Key string
Bit uint8
Start OptionUint64
End OptionUint64
}
func (data BITPOS) Do(ctx context.Context, client Connecter) (position int64, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"BITPOS", data.Key, strconv.FormatUint(uint64(data.Bit), 10)}
if data.Start.Valid {
args = append(args, strconv.FormatUint(data.Start.Uint64, 10))
}
if data.End.Valid {
args = append(args, strconv.FormatUint(data.End.Uint64, 10))
}
position, _, err = client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
return
}
type DECR struct {
Key string
}
func (data DECR) Do(ctx context.Context, client Connecter) (newValue int64, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"DECR", data.Key}
newValue,_, err = client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
return
}
type DECRBY struct {
Key string
Decrement int64
}
func (data DECRBY) Do(ctx context.Context, client Connecter) (newValue int64, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"DECRBY", data.Key, strconv.FormatInt(data.Decrement, 10)}
newValue,_, err = client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
return
}
type GET struct {
Key string
}
func (data GET) Do(ctx context.Context, client Connecter) (value string, isNil bool, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"GET", data.Key}
return client.DoStringReply(ctx, args)
}
type GETBIT struct {
Key string
Offset uint64
}
func (data GETBIT) Do(ctx context.Context, client Connecter) (value uint8, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"GETBIT", data.Key, strconv.FormatUint(data.Offset, 10)}
reply, _, err := client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
value = uint8(reply)
return
}
type GETDEL struct {
Key string
}
func (data GETDEL) Do(ctx context.Context, client Connecter) (value string,isNil bool, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"GETDEL", data.Key}
return client.DoStringReply(ctx, args)
}
type GETEX struct {
Key string
Expire time.Duration
ExpireAt time.Time
PERSIST bool
}
func (data GETEX) Do(ctx context.Context, client Connecter) (value string,isNil bool, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"GETEX", data.Key}
if data.Expire != 0 {
px := strconv.FormatInt(data.Expire.Milliseconds(), 10)
args = append(args, "PX", px)
}
if data.ExpireAt.IsZero() == false {
args = append(args, "PXAT", strconv.FormatInt(xtime.UnixMilli(data.ExpireAt), 10))
}
if data.PERSIST {
args = append(args, "PERSIST")
}
return client.DoStringReply(ctx, args)
}
type GETRANGE struct {
Key string
Start int64
End int64
}
func (data GETRANGE) Do(ctx context.Context, client Connecter) (value string, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"GETRANGE", data.Key, strconv.FormatInt(data.Start, 10), strconv.FormatInt(data.End, 10)}
value, _, err = client.DoStringReply(ctx, args) ; if err != nil {
return
}
return
}
type INCR struct {
Key string
}
func (data INCR) Do(ctx context.Context, client Connecter) (newValue int64, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"INCR", data.Key}
newValue,_, err = client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
return
}
type INCRBY struct {
Key string
Increment int64
}
func (data INCRBY) Do(ctx context.Context, client Connecter) (newValue int64, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"INCRBY", data.Key, strconv.FormatInt(data.Increment, 10)}
newValue,_, err = client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
return
}
type INCRBYFLOAT struct {
Key string
Increment string `eg:"strconv.FormatFloat(value, 'f', 2, 64)"`
}
func (data INCRBYFLOAT) Do(ctx context.Context, client Connecter) (newValue float64, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"INCRBYFLOAT", data.Key, data.Increment}
reply, _, err := client.DoStringReply(ctx, args) ; if err != nil {
return
}
return strconv.ParseFloat(reply, 64)
}
type MGET struct {
Keys []string
}
func (data MGET) Do(ctx context.Context, client Connecter) (values []OptionString, err error) {
if len(data.Keys) == 0 { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"MGET"}
args = append(args, data.Keys...)
values, err = client.DoArrayStringReply(ctx, args) ; if err != nil {
return
}
return
}
type MSET struct {
KeysValues []KeyValue
}
func (data MSET) Do(ctx context.Context, client Connecter) (err error) {
args := []string{"MSET"}
if len(data.KeysValues) == 0 { err = xerr.New("goclub/redis: KeysValues can not be empty slice") ; return}
for _, KeyValue := range data.KeysValues {
args = append(args, KeyValue.Key, KeyValue.Value)
}
_, _, err = client.DoStringReply(ctx, args) ; if err != nil {
return
}
return
}
type MSETNX struct {
KeysValues []KeyValue
}
func (data MSETNX) Do(ctx context.Context, client Connecter) (result int8, err error) {
if len(data.KeysValues) == 0 { err = xerr.New("goclub/redis: KeysValues can not be empty slice") ; return}
args := []string{"MSETNX"}
for _, KeyValue := range data.KeysValues {
args = append(args, KeyValue.Key, KeyValue.Value)
}
reply, _, err := client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
result = int8(reply)
return
}
type SET struct {
NeverExpire bool
Key string
Value string
Expire time.Duration
ExpireAt time.Time // >= 6.2: Added the GET, EXAT and PXAT option. (ExpireAt)
KeepTTL bool // >= 6.0: Added the KEEPTTL option.
XX bool
NX bool
GET bool
}
var ErrSetForgetTimeToLive = errors.New("goclub/redis: SET maybe you forget set field Expire or ExpireAt or KeepTTL or NeverExpire")
func (data SET) Do(ctx context.Context, client Connecter) (reply string, isNil bool ,err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"SET", data.Key, data.Value}
// 只有在明确 NeverExpire 时候才允许 Expire 留空
if data.NeverExpire == false && data.Expire == 0 && data.ExpireAt.IsZero() && data.KeepTTL == false {
err = ErrSetForgetTimeToLive
return
}
if data.Expire != 0 {
px := strconv.FormatInt(data.Expire.Milliseconds(), 10)
args = append(args, "PX", px)
}
if data.ExpireAt.IsZero() == false {
args = append(args, "PXAT", strconv.FormatInt(xtime.UnixMilli(data.ExpireAt), 10))
}
if data.KeepTTL {
args = append(args, "KEEPTTL")
}
if data.NX {
args = append(args, "NX")
}
if data.XX {
args = append(args, "XX")
}
if data.GET {
args = append(args, "GET")
}
return client.DoStringReply(ctx, args)
}
type SETBIT struct {
Key string
Offset uint64
Value uint8
}
func (data SETBIT) Do(ctx context.Context, client Connecter) (originalValue uint8, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"SETBIT", data.Key, strconv.FormatUint(data.Offset, 10), strconv.FormatUint(uint64(data.Value), 10)}
reply, _, err := client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
originalValue = uint8(reply)
return
}
type SETRANGE struct {
Key string
Offset int64
Value string
}
func (data SETRANGE) Do(ctx context.Context, client Connecter) (length int64, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"SETRANGE", data.Key, strconv.FormatInt(data.Offset, 10), data.Value}
length, _, err = client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
return
}
type STRLEN struct {
Key string
}
func (data STRLEN) Do(ctx context.Context, client Connecter) (length int64, err error) {
if data.Key == "" { err = xerr.New("goclub/redis: key can not be empty string") ; return}
args := []string{"STRLEN", data.Key}
length, _, err = client.DoIntegerReply(ctx, args) ; if err != nil {
return
}
return
}