-
Notifications
You must be signed in to change notification settings - Fork 17
/
cluster.go
1866 lines (1670 loc) · 62 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
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package godis
import (
"errors"
"math/rand"
"strconv"
"strings"
"sync"
"time"
)
const (
masterNodeIndex = 2
)
type redisClusterInfoCache struct {
nodes sync.Map
slots sync.Map
rwLock sync.RWMutex
rLock sync.Mutex
wLock sync.Mutex
rediscovering bool
poolConfig *PoolConfig
connectionTimeout time.Duration
soTimeout time.Duration
password string
}
func newRedisClusterInfoCache(connectionTimeout, soTimeout time.Duration, password string, poolConfig *PoolConfig) *redisClusterInfoCache {
return &redisClusterInfoCache{
poolConfig: poolConfig,
connectionTimeout: connectionTimeout,
soTimeout: soTimeout,
password: password,
}
}
func (r *redisClusterInfoCache) discoverClusterNodesAndSlots(redis *Redis) error {
r.wLock.Lock()
defer r.wLock.Unlock()
r.reset(false)
slots, err := redis.ClusterSlots()
if err != nil {
return err
}
for _, s := range slots {
slotInfo := s.([]interface{})
size := len(slotInfo)
if size <= masterNodeIndex {
continue
}
slotNums := r.getAssignedSlotArray(slotInfo)
for i := masterNodeIndex; i < size; i++ {
hostInfos := slotInfo[i].([]interface{})
if len(hostInfos) <= 0 {
continue
}
host, port := r.generateHostAndPort(hostInfos)
r.setupNodeIfNotExist(false, host, port)
if i == masterNodeIndex {
r.assignSlotsToNode(false, slotNums, host, port)
}
}
}
return nil
}
func (r *redisClusterInfoCache) renewClusterSlots(redis *Redis) error {
r.wLock.Lock()
if r.rediscovering {
return nil
}
defer func() {
r.rediscovering = false
r.wLock.Unlock()
}()
if redis != nil {
return r.discoverClusterSlots(redis)
}
for _, jp := range r.getShuffledNodesPool() {
newRedis, err := jp.GetResource()
if err != nil {
continue
}
err = r.discoverClusterSlots(newRedis)
if err != nil {
continue
}
err = newRedis.Close()
return err
}
return nil
}
func (r *redisClusterInfoCache) discoverClusterSlots(redis *Redis) error {
slots, err := redis.ClusterSlots()
if err != nil {
return err
}
r.slots.Range(func(key, value interface{}) bool {
r.slots.Delete(key)
return true
})
for _, s := range slots {
slotInfo := s.([]interface{})
size := len(slotInfo)
if size <= masterNodeIndex {
continue
}
slotNums := r.getAssignedSlotArray(slotInfo)
hostInfos := slotInfo[masterNodeIndex].([]interface{})
if len(hostInfos) == 0 {
continue
}
host, port := r.generateHostAndPort(hostInfos)
r.assignSlotsToNode(true, slotNums, host, port)
}
return nil
}
func (r *redisClusterInfoCache) reset(lock bool) {
r.nodes.Range(func(key, value interface{}) bool {
if value != nil {
value.(*Pool).Destroy()
}
return true
})
r.nodes.Range(func(key, value interface{}) bool {
r.nodes.Delete(key)
return true
})
r.slots.Range(func(key, value interface{}) bool {
r.slots.Delete(key)
return true
})
}
func (r *redisClusterInfoCache) getAssignedSlotArray(slotInfo []interface{}) []int {
slotNums := make([]int, 0)
for slot := slotInfo[0].(int64); slot <= slotInfo[1].(int64); slot++ {
slotNums = append(slotNums, int(slot))
}
return slotNums
}
func (r *redisClusterInfoCache) generateHostAndPort(hostInfos []interface{}) (string, int) {
return string(hostInfos[0].([]byte)), int(hostInfos[1].(int64))
}
func (r *redisClusterInfoCache) setupNodeIfNotExist(lock bool, host string, port int) *Pool {
nodeKey := host + ":" + strconv.Itoa(port)
existingPool, ok := r.nodes.Load(nodeKey)
if ok && existingPool != nil {
return existingPool.(*Pool)
}
nodePool := NewPool(r.poolConfig, &Option{
Host: host,
Port: port,
ConnectionTimeout: r.connectionTimeout,
SoTimeout: r.soTimeout,
Password: r.password,
})
r.nodes.Store(nodeKey, nodePool)
return nodePool
}
func (r *redisClusterInfoCache) assignSlotToNode(slot int, host string, port int) {
targetPool := r.setupNodeIfNotExist(false, host, port)
r.slots.Store(slot, targetPool)
}
func (r *redisClusterInfoCache) assignSlotsToNode(lock bool, slots []int, host string, port int) {
targetPool := r.setupNodeIfNotExist(false, host, port)
for _, slot := range slots {
r.slots.Store(slot, targetPool)
}
}
func (r *redisClusterInfoCache) getShuffledNodesPool() []*Pool {
pools := make([]*Pool, 0)
r.nodes.Range(func(key, value interface{}) bool {
if value != nil {
pools = append(pools, value.(*Pool))
}
return true
})
r.shuffle(pools)
return pools
}
func (r *redisClusterInfoCache) shuffle(vals []*Pool) {
ra := rand.New(rand.NewSource(time.Now().Unix()))
for len(vals) > 0 {
n := len(vals)
randIndex := ra.Intn(n)
vals[n-1], vals[randIndex] = vals[randIndex], vals[n-1]
vals = vals[:n-1]
}
}
func (r *redisClusterInfoCache) getNode(nodeKey string) *Pool {
if value, ok := r.nodes.Load(nodeKey); ok {
return value.(*Pool)
}
return nil
}
func (r *redisClusterInfoCache) getNodes() map[string]*Pool {
ret := make(map[string]*Pool)
r.nodes.Range(func(key, value interface{}) bool {
if value != nil {
ret[key.(string)] = value.(*Pool)
}
return true
})
return ret
}
func (r *redisClusterInfoCache) getSlotPool(slot int) *Pool {
if value, ok := r.slots.Load(slot); ok {
return value.(*Pool)
}
return nil
}
type redisClusterConnectionHandler struct {
cache *redisClusterInfoCache
}
func newRedisClusterConnectionHandler(nodes []string, connectionTimeout, soTimeout time.Duration, password string, poolConfig *PoolConfig) *redisClusterConnectionHandler {
cache := newRedisClusterInfoCache(connectionTimeout, soTimeout, password, poolConfig)
for _, node := range nodes {
arr := strings.Split(node, ":")
port, err := strconv.Atoi(arr[1])
if err != nil {
continue
}
redis := NewRedis(&Option{
Host: arr[0],
Port: port,
})
if password != "" {
_, err := redis.Auth(password)
if err != nil {
continue
}
}
err = cache.discoverClusterNodesAndSlots(redis)
if err != nil {
continue
}
_ = redis.Close()
break
}
return &redisClusterConnectionHandler{cache: cache}
}
func (r *redisClusterConnectionHandler) getConnection() (*Redis, error) {
pools := r.cache.getShuffledNodesPool()
for _, pool := range pools {
redis, err := pool.GetResource()
if err != nil {
continue
}
result, err := redis.Ping()
if err != nil {
continue
}
if strings.ToUpper(result) == keywordPong.name {
return redis, nil
}
}
return nil, newNoReachableClusterNodeError("no reachable node in cluster")
}
func (r *redisClusterConnectionHandler) getConnectionFromSlot(slot int) (*Redis, error) {
connectionPool := r.cache.getSlotPool(slot)
if connectionPool != nil {
return connectionPool.GetResource()
}
r.renewSlotCache()
connectionPool = r.cache.getSlotPool(slot)
if connectionPool != nil {
return connectionPool.GetResource()
}
return r.getConnection()
}
func (r *redisClusterConnectionHandler) getConnectionFromNode(host string, port int) (*Redis, error) {
return r.cache.setupNodeIfNotExist(true, host, port).GetResource()
}
func (r *redisClusterConnectionHandler) getNodes() map[string]*Pool {
return r.cache.getNodes()
}
func (r *redisClusterConnectionHandler) renewSlotCache(redis ...*Redis) {
if len(redis) == 0 {
_ = r.cache.renewClusterSlots(nil)
return
}
for _, re := range redis {
_ = r.cache.renewClusterSlots(re)
}
}
type redisClusterHashTagUtil struct {
}
func newRedisClusterHashTagUtil() *redisClusterHashTagUtil {
return &redisClusterHashTagUtil{}
}
func (r *redisClusterHashTagUtil) getHashTag(key string) string {
return r.extractHashTag(key, true)
}
func (r *redisClusterHashTagUtil) isClusterCompliantMatchPattern(matchPattern string) bool {
tag := r.extractHashTag(matchPattern, false)
return tag != ""
}
func (r *redisClusterHashTagUtil) extractHashTag(key string, returnKeyOnAbsence bool) string {
s := strings.Index(key, "{")
if s > -1 {
e := strings.Index(key, "}")
if e > -1 && e != s+1 {
return key[s+1 : e]
}
}
if returnKeyOnAbsence {
return key
}
return ""
}
type redisClusterCommand struct {
maxAttempts int
connectionHandler *redisClusterConnectionHandler
execute func(redis *Redis) (interface{}, error)
}
func newRedisClusterCommand(maxAttempts int, connectionHandler *redisClusterConnectionHandler) *redisClusterCommand {
return &redisClusterCommand{maxAttempts: maxAttempts, connectionHandler: connectionHandler}
}
func (r *redisClusterCommand) run(key string) (interface{}, error) {
if key == "" {
return nil, newClusterOperationError("no way to dispatch this command to Redis cluster")
}
return r.runWithRetries([]byte(key), r.maxAttempts, false, nil)
}
func (r *redisClusterCommand) runBatch(keyCount int, keys ...string) (interface{}, error) {
if len(keys) == 0 {
return nil, newClusterOperationError("no way to dispatch this command to Redis cluster")
}
if len(keys) > 1 {
crc16 := newCRC16()
slot := crc16.getStringSlot(keys[0])
for i := 1; i < keyCount; i++ {
nextSlot := crc16.getStringSlot(keys[i])
if nextSlot != slot {
return nil, newClusterOperationError("no way to dispatch this command to Redis cluster,because keys have different slots")
}
}
}
return r.runWithRetries([]byte(keys[0]), r.maxAttempts, false, nil)
}
func (r *redisClusterCommand) runWithAnyNode() (interface{}, error) {
connection, err := r.connectionHandler.getConnection()
if err != nil {
return nil, err
}
result, err := r.execute(connection)
if err != nil {
return nil, err
}
_ = r.releaseConnection(connection)
return result, nil
}
func (r *redisClusterCommand) releaseConnection(redis *Redis) error {
if redis != nil {
return redis.Close()
}
return nil
}
func (r *redisClusterCommand) runWithRetries(key []byte, attempts int, tryRandomNode bool, redirect error) (interface{}, error) {
if attempts <= 0 {
return nil, newClusterMaxAttemptsError("too many cluster redirections")
}
var connection *Redis
var err error
if redirect != nil {
if connection, err = r.processRedirect(redirect); err != nil {
return nil, err
}
} else {
if tryRandomNode {
connection, err = r.connectionHandler.getConnection()
if err != nil {
return nil, err
}
} else {
connection, err = r.connectionHandler.getConnectionFromSlot(int(newCRC16().getByteSlot(key)))
if err != nil {
return nil, err
}
}
}
result, err := r.execute(connection)
defer r.releaseConnection(connection)
if err == nil {
return result, nil
}
// 根据各种error,进行重试或者重新分配slot的逻辑
// 判断 NoReachableClusterNodeException,直接返回错误
// 判断 ConnectionException,重试,当attempt<=1时,重新分配slot
// 判断 RedirectionException,如果是MovedDataException,则重新分配slot,如果是AskDataException,则设置ctx,如果是其他错误,直接返回错误,继续重试
switch err.(type) {
case *NoReachableClusterNodeError:
return nil, err
case *ConnectError:
_ = r.releaseConnection(connection)
if attempts <= 1 {
r.connectionHandler.renewSlotCache()
//return nil, err
}
return r.runWithRetries(key, attempts-1, tryRandomNode, redirect)
case *MovedDataError:
r.connectionHandler.renewSlotCache(connection)
_ = r.releaseConnection(connection)
return r.runWithRetries(key, attempts-1, false, err)
}
return nil, err
}
func (r *redisClusterCommand) processRedirect(redirect error) (*Redis, error) {
switch redirect.(type) {
case *MovedDataError:
dataError := redirect.(*MovedDataError)
connection, err := r.connectionHandler.getConnectionFromNode(dataError.Host, dataError.Port)
if err != nil {
return nil, err
}
return connection, nil
case *AskDataError:
dataError := redirect.(*AskDataError)
connection, err := r.connectionHandler.getConnectionFromNode(dataError.Host, dataError.Port)
if err != nil {
return nil, err
}
_, err = connection.Asking()
if err != nil {
return nil, err
}
return connection, nil
}
return nil, newRedisError("wrong redirect error")
}
//ClusterOption when you create a new cluster instance ,then you need set some option
type ClusterOption struct {
Nodes []string //cluster nodes, for example: []string{"localhost:7000","localhost:7001"}
ConnectionTimeout time.Duration //redis connect timeout
SoTimeout time.Duration //redis read timeout
MaxAttempts int //when operation or socket is not alright,then program will attempt retry
Password string //cluster redis password
PoolConfig *PoolConfig //redis connection pool config
}
//RedisCluster redis cluster tool
type RedisCluster struct {
MaxAttempts int
connectionHandler *redisClusterConnectionHandler
}
//NewRedisCluster constructor
func NewRedisCluster(option *ClusterOption) *RedisCluster {
if option.MaxAttempts <= 0 {
option.MaxAttempts = 5
}
conTimeout := option.ConnectionTimeout
if option.ConnectionTimeout == 0 {
conTimeout = 5 * time.Second
}
soTimeout := option.SoTimeout
if option.SoTimeout == 0 {
soTimeout = 5 * time.Second
}
return &RedisCluster{
MaxAttempts: option.MaxAttempts,
connectionHandler: newRedisClusterConnectionHandler(option.Nodes, conTimeout, soTimeout, option.Password, option.PoolConfig),
}
}
//<editor-fold desc="rediscommands">
//Set set key/value,without timeout
func (r *RedisCluster) Set(key, value string) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.Set(key, value)
}
return ToStrReply(command.run(key))
}
//SetWithParamsAndTime see redis command
func (r *RedisCluster) SetWithParamsAndTime(key, value, nxxx, expx string, time int64) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.SetWithParamsAndTime(key, value, nxxx, expx, time)
}
return ToStrReply(command.run(key))
}
//SetWithParams see redis command
func (r *RedisCluster) SetWithParams(key, value, nxxx string) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.SetWithParams(key, value, nxxx)
}
return ToStrReply(command.run(key))
}
//Get see redis command
func (r *RedisCluster) Get(key string) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.Get(key)
}
return ToStrReply(command.run(key))
}
//Persist see redis command
func (r *RedisCluster) Persist(key string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.Persist(key)
}
return ToInt64Reply(command.run(key))
}
//Type see redis command
func (r *RedisCluster) Type(key string) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.Type(key)
}
return ToStrReply(command.run(key))
}
//Expire see redis command
func (r *RedisCluster) Expire(key string, seconds int) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.Expire(key, seconds)
}
return ToInt64Reply(command.run(key))
}
//PExpire see redis command
func (r *RedisCluster) PExpire(key string, milliseconds int64) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.PExpire(key, milliseconds)
}
return ToInt64Reply(command.run(key))
}
//ExpireAt see redis command
func (r *RedisCluster) ExpireAt(key string, unixtime int64) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.ExpireAt(key, unixtime)
}
return ToInt64Reply(command.run(key))
}
//PExpireAt see redis command
func (r *RedisCluster) PExpireAt(key string, millisecondsTimestamp int64) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.PExpireAt(key, millisecondsTimestamp)
}
return ToInt64Reply(command.run(key))
}
//TTL see redis command
func (r *RedisCluster) TTL(key string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.TTL(key)
}
return ToInt64Reply(command.run(key))
}
//PTTL see redis command
func (r *RedisCluster) PTTL(key string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.PTTL(key)
}
return ToInt64Reply(command.run(key))
}
//SetBitWithBool see redis command
func (r *RedisCluster) SetBitWithBool(key string, offset int64, value bool) (bool, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.SetBitWithBool(key, offset, value)
}
return ToBoolReply(command.run(key))
}
//SetBit see redis command
func (r *RedisCluster) SetBit(key string, offset int64, value string) (bool, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.SetBit(key, offset, value)
}
return ToBoolReply(command.run(key))
}
//GetBit see redis command
func (r *RedisCluster) GetBit(key string, offset int64) (bool, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.GetBit(key, offset)
}
return ToBoolReply(command.run(key))
}
//SetRange see redis command
func (r *RedisCluster) SetRange(key string, offset int64, value string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.SetRange(key, offset, value)
}
return ToInt64Reply(command.run(key))
}
//GetRange see redis command
func (r *RedisCluster) GetRange(key string, startOffset, endOffset int64) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.GetRange(key, startOffset, endOffset)
}
return ToStrReply(command.run(key))
}
//GetSet see redis command
func (r *RedisCluster) GetSet(key, value string) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.GetSet(key, value)
}
return ToStrReply(command.run(key))
}
//SetNx see redis command
func (r *RedisCluster) SetNx(key, value string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.SetNx(key, value)
}
return ToInt64Reply(command.run(key))
}
//SetEx see redis command
func (r *RedisCluster) SetEx(key string, seconds int, value string) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.SetEx(key, seconds, value)
}
return ToStrReply(command.run(key))
}
//PSetEx see redis command
func (r *RedisCluster) PSetEx(key string, milliseconds int64, value string) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.PSetEx(key, milliseconds, value)
}
return ToStrReply(command.run(key))
}
//DecrBy see redis command
func (r *RedisCluster) DecrBy(key string, decrement int64) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.DecrBy(key, decrement)
}
return ToInt64Reply(command.run(key))
}
//Decr see redis command
func (r *RedisCluster) Decr(key string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.Decr(key)
}
return ToInt64Reply(command.run(key))
}
//IncrBy see redis command
func (r *RedisCluster) IncrBy(key string, increment int64) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.IncrBy(key, increment)
}
return ToInt64Reply(command.run(key))
}
//IncrByFloat see redis command
func (r *RedisCluster) IncrByFloat(key string, increment float64) (float64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.IncrByFloat(key, increment)
}
return ToFloat64Reply(command.run(key))
}
//Incr see redis command
func (r *RedisCluster) Incr(key string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.Incr(key)
}
return ToInt64Reply(command.run(key))
}
//Append see redis command
func (r *RedisCluster) Append(key, value string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.Append(key, value)
}
return ToInt64Reply(command.run(key))
}
//SubStr see redis command
func (r *RedisCluster) SubStr(key string, start, end int) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.SubStr(key, start, end)
}
return ToStrReply(command.run(key))
}
//HSet see redis command
func (r *RedisCluster) HSet(key, field string, value string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.HSet(key, field, value)
}
return ToInt64Reply(command.run(key))
}
//HGet see redis command
func (r *RedisCluster) HGet(key, field string) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.HGet(key, field)
}
return ToStrReply(command.run(key))
}
//HSetNx see redis command
func (r *RedisCluster) HSetNx(key, field, value string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.HSetNx(key, field, value)
}
return ToInt64Reply(command.run(key))
}
//HMSet see redis command
func (r *RedisCluster) HMSet(key string, hash map[string]string) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.HMSet(key, hash)
}
return ToStrReply(command.run(key))
}
//HMGet see redis command
func (r *RedisCluster) HMGet(key string, fields ...string) ([]string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.HMGet(key, fields...)
}
return ToStrArrReply(command.run(key))
}
//HIncrBy see redis command
func (r *RedisCluster) HIncrBy(key, field string, value int64) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.HIncrBy(key, field, value)
}
return ToInt64Reply(command.run(key))
}
//HIncrByFloat see redis command
func (r *RedisCluster) HIncrByFloat(key, field string, value float64) (float64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.HIncrByFloat(key, field, value)
}
return ToFloat64Reply(command.run(key))
}
//HExists see redis command
func (r *RedisCluster) HExists(key, field string) (bool, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.HExists(key, field)
}
return ToBoolReply(command.run(key))
}
//HDel see redis command
func (r *RedisCluster) HDel(key string, fields ...string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.HDel(key, fields...)
}
return ToInt64Reply(command.run(key))
}
//HLen see redis command
func (r *RedisCluster) HLen(key string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.HLen(key)
}
return ToInt64Reply(command.run(key))
}
//HKeys see redis command
func (r *RedisCluster) HKeys(key string) ([]string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.HKeys(key)
}
return ToStrArrReply(command.run(key))
}
//HVals see redis command
func (r *RedisCluster) HVals(key string) ([]string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.HVals(key)
}
return ToStrArrReply(command.run(key))
}
//HGetAll see redis command
func (r *RedisCluster) HGetAll(key string) (map[string]string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.HGetAll(key)
}
return ToMapReply(command.run(key))
}
//RPush see redis command
func (r *RedisCluster) RPush(key string, strings ...string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.RPush(key, strings...)
}
return ToInt64Reply(command.run(key))
}
//LPush see redis command
func (r *RedisCluster) LPush(key string, strings ...string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.LPush(key, strings...)
}
return ToInt64Reply(command.run(key))
}
//LLen see redis command
func (r *RedisCluster) LLen(key string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.LLen(key)
}
return ToInt64Reply(command.run(key))
}
//LRange see redis command
func (r *RedisCluster) LRange(key string, start, stop int64) ([]string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.LRange(key, start, stop)
}
return ToStrArrReply(command.run(key))
}
//LTrim see redis command
func (r *RedisCluster) LTrim(key string, start, stop int64) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.LTrim(key, start, stop)
}
return ToStrReply(command.run(key))
}
//LIndex see redis command
func (r *RedisCluster) LIndex(key string, index int64) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.LIndex(key, index)
}
return ToStrReply(command.run(key))
}
//LSet see redis command
func (r *RedisCluster) LSet(key string, index int64, value string) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.LSet(key, index, value)
}
return ToStrReply(command.run(key))
}
//LRem see redis command
func (r *RedisCluster) LRem(key string, count int64, value string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.LRem(key, count, value)
}
return ToInt64Reply(command.run(key))
}
//LPop see redis command
func (r *RedisCluster) LPop(key string) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.LPop(key)
}
return ToStrReply(command.run(key))
}
//RPop see redis command
func (r *RedisCluster) RPop(key string) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.RPop(key)
}
return ToStrReply(command.run(key))
}
//SAdd see redis command
func (r *RedisCluster) SAdd(key string, members ...string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.SAdd(key, members...)
}
return ToInt64Reply(command.run(key))
}
//SMembers see redis command
func (r *RedisCluster) SMembers(key string) ([]string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.SMembers(key)
}
return ToStrArrReply(command.run(key))
}
//SRem see redis command
func (r *RedisCluster) SRem(key string, members ...string) (int64, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.SRem(key, members...)
}
return ToInt64Reply(command.run(key))
}
//SPop see redis command
func (r *RedisCluster) SPop(key string) (string, error) {
command := newRedisClusterCommand(r.MaxAttempts, r.connectionHandler)
command.execute = func(redis *Redis) (interface{}, error) {
return redis.SPop(key)
}
return ToStrReply(command.run(key))
}