-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
718 lines (545 loc) · 16.2 KB
/
redis.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
package gr
import (
"errors"
"strconv"
"time"
)
type Redis struct {
config Config
pool *pool
}
type Config struct {
Address string
Port int
Database string
Password string
MinConnections int
Timeout int
}
func New() (*Redis, error) {
return NewWithConfig(
Config{
Address: "localhost",
Port: 6379,
MinConnections: 2,
},
)
}
func NewWithConfig(config Config) (*Redis, error) {
var r *Redis
pool, err := newPool(&config)
if err == nil {
r = &Redis{
config: config,
pool: pool,
}
}
return r, err
}
var (
NilErr = errors.New("Received a nil response.")
NotConnectedErr = errors.New("Client is not connected.")
NotInitializedErr = errors.New("Client is not initialized. Use redis.New()")
PoolErr = errors.New("Pool error")
ParamErr = errors.New("Error in parameters")
NotEnoughParamsErr = errors.New("Not enough parameters")
ParamsNotTuplesErr = errors.New("Parameters must be tuples (x,y)")
PipelineInputErr = errors.New("")
)
/////////////
//PIPELINING
////////////
func (r *Redis) Pipelined(caller func(*Pipeline)) error {
p := newPipeline(r)
caller(p)
return p.execute(false)
}
//////////////
//TRANSACTION
/////////////
func (r *Redis) Multi(caller func(*Transaction)) error {
t := newTransaction(r)
caller(t)
return t.execute(true)
}
//////////
//PUBSUB
/////////
func (r *Redis) Publish(channel string, message string) (int64, error) {
return r.writeReadInt(rPublish(channel, message))
}
func (r *Redis) Subscribe(caller func(*PubSub), channels ...string) {
r.pubSubStart(false, caller, channels...)
}
func (r *Redis) PSubscribe(caller func(*PubSub), channels ...string) {
r.pubSubStart(true, caller, channels...)
}
func (r *Redis) pubSubStart(withPattern bool, caller func(*PubSub), channels ...string) {
ps, _ := newPubSub(r)
if withPattern {
go ps.pSubscribe(channels...)
} else {
go ps.subscribe(channels...)
}
caller(ps)
ps.unSubscribe()
}
func scanGeneric(s []string, err error) (int64, []string, error) {
c, err := strconv.ParseInt(string(s[0]), 10, 64)
return c, s[1:], err
}
////////
//KEYS
///////
func (r *Redis) Del(keys ...string) (int64, error) {
rs, err := rDel(keys...)
if err != nil {
return 0, err
}
return r.writeReadInt(rs)
}
func (r *Redis) Dump(key string) (string, error) {
return r.writeReadStr(rDump(key))
}
func (r *Redis) Exists(key string) (bool, error) {
return r.writeReadBool(rExists(key))
}
func (r *Redis) Expire(key string, seconds int) (bool, error) {
return r.writeReadBool(rExpire(key, seconds))
}
func (r *Redis) ExpireAt(key string, date time.Time) (bool, error) {
return r.writeReadBool(rExpireAt(key, date))
}
func (r *Redis) Keys(pattern string) ([]string, error) {
return r.writeReadStrArray(rKeys(pattern))
}
func (r *Redis) Migrate(
host string,
port int,
key string,
destinationDB string,
timeout int,
copy bool,
replace bool) (string, error) {
return r.writeReadStr(rMigrate(host, port, key, destinationDB, timeout, copy, replace))
}
func (r *Redis) Move(key string, db string) (bool, error) {
return r.writeReadBool(rMove(key, db))
}
func (r *Redis) ObjectEncoding(arguments ...string) (string, error) {
return r.writeReadStr(rObjectEncoding(arguments...))
}
func (r *Redis) ObjectRefCount(arguments ...string) (int64, error) {
return r.writeReadInt(rObjectRefCount(arguments...))
}
func (r *Redis) ObjectIdleTime(arguments ...string) (int64, error) {
return r.writeReadInt(rObjectIdleTime(arguments...))
}
func (r *Redis) Persist(key string) (bool, error) {
return r.writeReadBool(rPersist(key))
}
func (r *Redis) PExpire(key string, milliseconds int) (bool, error) {
return r.writeReadBool(rPExpire(key, milliseconds))
}
func (r *Redis) PExpireAt(key string, date time.Time) (bool, error) {
return r.writeReadBool(rPExpireAt(key, date))
}
func (r *Redis) PTTL(key string) (int64, error) {
return r.writeReadInt(rPTTL(key))
}
func (r *Redis) RandomKey() (string, error) {
return r.writeReadStr(rRandomKey())
}
func (r *Redis) Rename(key string, newKey string) (string, error) {
return r.writeReadStr(rRename(key, newKey))
}
func (r *Redis) RenameNx(key string, newKey string) (bool, error) {
return r.writeReadBool(rRenameNx(key, newKey))
}
func (r *Redis) Restore(key string, ttl int, value string, replace bool) (string, error) {
return r.writeReadStr(rRestore(key, ttl, value, replace))
}
func (r *Redis) Scan(cursor int, scanParams *ScanParams) (int64, []string, error) {
s, err := r.writeReadStrArray(rScan(cursor, scanParams))
c, sc, err := scanGeneric(s, err)
return c, sc, err
}
func (r *Redis) Sort(key string, sortParams *SortParams) ([]string, error) {
return r.writeReadStrArray(rSort(key, sortParams))
}
func (r *Redis) SortStore(key string, destination string, sortParams *SortParams) (int64, error) {
return r.writeReadInt(rSortStore(key, destination, sortParams))
}
func (r *Redis) TTL(key string) (int64, error) {
return r.writeReadInt(rTTL(key))
}
func (r *Redis) Type(key string) (string, error) {
return r.writeReadStr(rType(key))
}
func (r *Redis) Wait(numSlaves int, timeout int) (int64, error) {
return r.writeReadInt(rWait(numSlaves, timeout))
}
//////////
//STRINGS
func (r *Redis) Append(key string, value string) (int64, error) {
return r.writeReadInt(rAppend(key, value))
}
func (r *Redis) BitCount(key string) (int64, error) {
return r.writeReadInt(rBitCount(key))
}
func (r *Redis) BitOp(bitOperation BitOperation, destKey string, keys ...string) (int64, error) {
rs, err := rBitOp(bitOperation, destKey, keys...)
if err != nil {
return 0, err
}
return r.writeReadInt(rs)
}
func (r *Redis) BitPos(key string, bit bool, startEnd ...int) (int64, error) {
return r.writeReadInt(rBitPos(key, bit, startEnd...))
}
func (r *Redis) Get(key string) (string, error) {
return r.writeReadStr(rGet(key))
}
func (r *Redis) GetBit(key string, offset int) (int64, error) {
return r.writeReadInt(rGetBit(key, offset))
}
func (r *Redis) GetRange(key string, start int, end int) (string, error) {
return r.writeReadStr(rGetRange(key, start, end))
}
func (r *Redis) GetSet(key string, value string) (string, error) {
return r.writeReadStr(rGetSet(key, value))
}
func (r *Redis) Set(key string, value string) (string, error) {
return r.writeReadStr(rSet(key, value))
}
func (r *Redis) SetX(key string, value string, keyExpiration *KeyExpiration, keyExistance *KeyExistance) (string, error) {
rs, err := rSetX(key, value, keyExpiration, keyExistance)
if err != nil {
return "", err
}
return r.writeReadStr(rs)
}
func (r *Redis) Incr(key string) (int64, error) {
return r.writeReadInt(rIncr(key))
}
func (r *Redis) IncrBy(key string, increment int) (int64, error) {
return r.writeReadInt(rIncrBy(key, increment))
}
func (r *Redis) IncrByFloat(key string, increment float64) (float64, error) {
return r.writeReadFloat(rIncrByFloat(key, increment))
}
func (r *Redis) Decr(key string) (int64, error) {
return r.writeReadInt(rDecr(key))
}
func (r *Redis) DecrBy(key string, decrement int) (int64, error) {
return r.writeReadInt(rDecrBy(key, decrement))
}
func (r *Redis) MGet(keys ...string) ([]string, error) {
rs, err := rMGet(keys...)
if err != nil {
return nil, err
}
return r.writeReadStrArray(rs)
}
func (r *Redis) MSet(keyValues ...string) (string, error) {
rs, err := rMSet(keyValues...)
if err != nil {
return "", err
}
return r.writeReadStr(rs)
}
func (r *Redis) MSetNx(keyValues ...string) (bool, error) {
rs, err := rMSetNx(keyValues...)
if err != nil {
return false, err
}
return r.writeReadBool(rs)
}
func (r *Redis) PSetEx(key string, milliseconds int, value string) (string, error) {
return r.writeReadStr(rPSetEx(key, milliseconds, value))
}
func (r *Redis) SetEx(key string, seconds int, value string) (string, error) {
return r.writeReadStr(rSetEx(key, seconds, value))
}
func (r *Redis) SetBit(key string, offset int, value bool) (int64, error) {
return r.writeReadInt(rSetBit(key, offset, value))
}
func (r *Redis) SetNx(key string, value string) (bool, error) {
return r.writeReadBool(rSetNx(key, value))
}
func (r *Redis) SetRange(key string, offset int, value string) (int64, error) {
return r.writeReadInt(rSetRange(key, offset, value))
}
func (r *Redis) StrLen(key string) (int64, error) {
return r.writeReadInt(rStrLen(key))
}
////////
//LISTS
///////
func (r *Redis) LLen(key string) (int64, error) {
return r.writeReadInt(rLLen(key))
}
func (r *Redis) LIndex(key string, index int) (string, error) {
return r.writeReadStr(rLIndex(key, index))
}
func (r *Redis) LPush(key string, values ...string) (int64, error) {
rs, err := rLPush(key, values...)
if err != nil {
return 0, err
}
return r.writeReadInt(rs)
}
func (r *Redis) LPushX(key string, value string) (int64, error) {
return r.writeReadInt(rLPushX(key, value))
}
func (r *Redis) RPush(key string, values ...string) (int64, error) {
rs, err := rRPush(key, values...)
if err != nil {
return 0, err
}
return r.writeReadInt(rs)
}
func (r *Redis) RPushX(key string, value string) (int64, error) {
return r.writeReadInt(rRPushX(key, value))
}
func (r *Redis) LPop(key string) (string, error) {
return r.writeReadStr(rLPop(key))
}
func (r *Redis) RPop(key string) (string, error) {
return r.writeReadStr(rRPop(key))
}
func (r *Redis) LSet(key string, index int, value string) (string, error) {
return r.writeReadStr(rLSet(key, index, value))
}
func (r *Redis) LInsert(key string, location InsertLocation, pivot string, value string) (int64, error) {
rs, err := rLInsert(key, location, pivot, value)
if err != nil {
return 0, err
}
return r.writeReadInt(rs)
}
func (r *Redis) LRange(key string, start int, stop int) ([]string, error) {
return r.writeReadStrArray(rLRange(key, start, stop))
}
func (r *Redis) LRem(key string, count int, value string) (int64, error) {
return r.writeReadInt(rLRem(key, count, value))
}
func (r *Redis) LTrim(key string, start int, stop int) (string, error) {
return r.writeReadStr(rLTrim(key, start, stop))
}
func (r *Redis) RPopLPush(source string, destination string) (string, error) {
return r.writeReadStr(rRPopLPush(source, destination))
}
func (r *Redis) BLPop(timeout int, keys ...string) ([]string, error) {
rs, err := rBLPop(timeout, keys...)
if err != nil {
return nil, err
}
return r.writeReadStrArray(rs)
}
func (r *Redis) BRPop(timeout int, keys ...string) ([]string, error) {
rs, err := rBRPop(timeout, keys...)
if err != nil {
return nil, err
}
return r.writeReadStrArray(rs)
}
func (r *Redis) BRPopLPush(source string, destination string, timeout int) (string, error) {
return r.writeReadStr(rBRPopLPush(source, destination, timeout))
}
///////
//SETS
//////
func (r *Redis) SAdd(key string, values ...string) (int64, error) {
rs, err := rSAdd(key, values...)
if err != nil {
return 0, err
}
return r.writeReadInt(rs)
}
func (r *Redis) SCard(key string) (int64, error) {
return r.writeReadInt(rSCard(key))
}
func (r *Redis) SDiff(keys ...string) ([]string, error) {
rs, err := rSDiff(keys...)
if err != nil {
return nil, err
}
return r.writeReadStrArray(rs)
}
func (r *Redis) SDiffStore(key string, keys ...string) (int64, error) {
rs, err := rSDiffStore(key, keys...)
if err != nil {
return 0, err
}
return r.writeReadInt(rs)
}
func (r *Redis) SInter(keys ...string) ([]string, error) {
rs, err := rSInter(keys...)
if err != nil {
return nil, err
}
return r.writeReadStrArray(rs)
}
func (r *Redis) SInterStore(key string, keys ...string) (int64, error) {
rs, err := rSInterStore(key, keys...)
if err != nil {
return 0, err
}
return r.writeReadInt(rs)
}
func (r *Redis) SIsMember(key string, value string) (bool, error) {
return r.writeReadBool(rSIsMember(key, value))
}
func (r *Redis) SMembers(key string) ([]string, error) {
return r.writeReadStrArray(rSMembers(key))
}
func (r *Redis) SMove(sourceKey string, destinationKey string, value string) (bool, error) {
return r.writeReadBool(rSMove(sourceKey, destinationKey, value))
}
func (r *Redis) SPop(key string) (string, error) {
return r.writeReadStr(rSPop(key))
}
func (r *Redis) SRandMember(key string, count int) ([]string, error) {
return r.writeReadStrArray(rSRandMember(key, count))
}
func (r *Redis) SRem(key string, values ...string) (int64, error) {
rs, err := rSRem(key, values...)
if err != nil {
return 0, err
}
return r.writeReadInt(rs)
}
func (r *Redis) SScan(key string, cursor int, scanParams *ScanParams) (int64, []string, error) {
s, err := r.writeReadStrArray(rSScan(key, cursor, scanParams))
return scanGeneric(s, err)
}
func (r *Redis) SUnion(keys ...string) ([]string, error) {
rs, err := rSUnion(keys...)
if err != nil {
return nil, err
}
return r.writeReadStrArray(rs)
}
func (r *Redis) SUnionStore(key string, keys ...string) (int64, error) {
rs, err := rSUnionStore(key, keys...)
if err != nil {
return 0, err
}
return r.writeReadInt(rs)
}
////////
//HASHES
///////
func (r *Redis) HDel(key string, fields ...string) (int64, error) {
rs, err := rHDel(key, fields...)
if err != nil {
return 0, err
}
return r.writeReadInt(rs)
}
func (r *Redis) HExists(key string, field string) (bool, error) {
return r.writeReadBool(rHExists(key, field))
}
func (r *Redis) HGet(key string, field string) (string, error) {
return r.writeReadStr(rHGet(key, field))
}
func (r *Redis) HGetAll(key string) ([]string, error) {
return r.writeReadStrArray(rHGetAll(key))
}
func (r *Redis) HIncrBy(key string, field string, increment int) (int64, error) {
return r.writeReadInt(rHIncrBy(key, field, increment))
}
func (r *Redis) HIncrByFloat(key string, field string, increment float64) (float64, error) {
return r.writeReadFloat(rHIncrByFloat(key, field, increment))
}
func (r *Redis) HKeys(key string) ([]string, error) {
return r.writeReadStrArray(rHKeys(key))
}
func (r *Redis) HLen(key string) (int64, error) {
return r.writeReadInt(rHLen(key))
}
func (r *Redis) HMGet(key string, fields ...string) ([]string, error) {
rs, err := rHMGet(key, fields...)
if err != nil {
return nil, err
}
return r.writeReadStrArray(rs)
}
func (r *Redis) HMSet(key string, fieldValues ...string) (string, error) {
rs, err := rHMSet(key, fieldValues...)
if err != nil {
return "", err
}
return r.writeReadStr(rs)
}
func (r *Redis) HSet(key string, field string, value string) (bool, error) {
return r.writeReadBool(rHSet(key, field, value))
}
func (r *Redis) HSetNx(key string, field string, value string) (bool, error) {
return r.writeReadBool(rHSetNx(key, field, value))
}
/* .3.0.2
func (r *Redis) HStrLen(key string, field string) (int64, error) {
return r.writeReadInt(rHStrLen(key, field))
}
*/
func (r *Redis) HVals(key string) ([]string, error) {
return r.writeReadStrArray(rHVals(key))
}
func (r *Redis) HScan(key string, cursor int, scanParams *ScanParams) (int64, []string, error) {
s, err := r.writeReadStrArray(rHScan(key, cursor, scanParams))
return scanGeneric(s, err)
}
/////////////
//HYPERLOGLOG
/////////////
func (r *Redis) PFAdd(key string, elements ...string) (int64, error) {
rs, err := rPFAdd(key, elements...)
if err != nil {
return 0, err
}
return r.writeReadInt(rs)
}
func (r *Redis) PFCount(keys ...string) (int64, error) {
rs, err := rPFCount(keys...)
if err != nil {
return 0, err
}
return r.writeReadInt(rs)
}
func (r *Redis) PFMerge(destkey string, sourcekeys ...string) (string, error) {
rs, err := rPFMerge(destkey, sourcekeys...)
if err != nil {
return "", err
}
return r.writeReadStr(rs)
}
/////
////
func (r *Redis) writeReadGeneric(cmds [][]byte) (result *redisResponse, err error) {
conn := r.pool.get()
defer r.pool.put(conn)
result, err = writeRead(cmds, conn)
return
}
func (r *Redis) writeReadStr(cmds [][]byte) (result string, err error) {
result, err = readString(r.writeReadGeneric(cmds))
return
}
func (r *Redis) writeReadStrArray(cmds [][]byte) (result []string, err error) {
result, err = readStringArray(r.writeReadGeneric(cmds))
return
}
func (r *Redis) writeReadInt(cmds [][]byte) (result int64, err error) {
result, err = readInt64(r.writeReadGeneric(cmds))
return
}
func (r *Redis) writeReadFloat(cmds [][]byte) (result float64, err error) {
result, err = readFloat64(r.writeReadGeneric(cmds))
return
}
func (r *Redis) writeReadBool(cmds [][]byte) (result bool, err error) {
result, err = readBool(r.writeReadGeneric(cmds))
return
}