/
client.go
3841 lines (3569 loc) · 121 KB
/
client.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 redis
import (
"context"
"fmt"
"sync"
"time"
"github.com/abulo/ratel/v2/core/logger"
"github.com/abulo/ratel/v2/util"
"github.com/go-redis/redis/v8"
"github.com/pkg/errors"
)
// Config 配置
type Config struct {
Type bool //是否集群
Hosts []string //IP
Password string //密码
Database int //数据库
PoolSize int //连接池大小
KeyPrefix string
DisableMetric bool // 关闭指标采集
DisableTrace bool // 关闭链路追踪
}
// New 新连接
func New(config *Config) *Client {
opts := Options{}
if config.Type {
opts.Type = ClientCluster
} else {
opts.Type = ClientNormal
}
opts.Hosts = config.Hosts
opts.KeyPrefix = config.KeyPrefix
if config.PoolSize > 0 {
opts.PoolSize = config.PoolSize
} else {
opts.PoolSize = 64
}
if config.Database > 0 {
opts.Database = config.Database
} else {
opts.Database = 0
}
if config.Password != "" {
opts.Password = config.Password
}
// if !config.DisableMetric {
opts.DisableMetric = config.DisableMetric
// }
// if !config.DisableTrace {
opts.DisableTrace = config.DisableTrace
// }
client := NewClient(opts)
ctx := context.TODO()
if err := client.Ping(ctx).Err(); err != nil {
logger.Logger.Panic(err.Error())
}
return client
}
// RedisNil means nil reply, .e.g. when key does not exist.
const RedisNil = redis.Nil
// Client a struct representing the redis client
type Client struct {
opts Options
client *redis.Client
clusterClient *redis.ClusterClient
fmtString string
clientType ClientType
}
// NewClient 新客户端
func NewClient(opts Options) *Client {
r := &Client{opts: opts}
switch opts.Type {
// 群集客户端
case ClientCluster:
// NewClusterClient 返回一个 Redis 集群客户端
tc := redis.NewClusterClient(opts.GetClusterConfig())
ctx := context.TODO()
if !opts.DisableTrace || !opts.DisableMetric {
_ = tc.ForEachShard(ctx, func(ctx context.Context, shard *redis.Client) error {
shard.AddHook(OpenTraceHook{
DisableMetric: opts.DisableMetric,
DisableTrace: opts.DisableTrace,
DB: opts.Database,
Addr: util.Implode(";", opts.Hosts),
})
return nil
})
}
r.clusterClient = tc
r.clientType = ClientCluster
// 标准客户端也是默认值
case ClientNormal:
fallthrough
default:
// NewClient 根据 Options 指定的 Redis Server 返回一个客户端。
tc := redis.NewClient(opts.GetNormalConfig())
if !opts.DisableTrace || !opts.DisableMetric {
tc.AddHook(OpenTraceHook{
DisableMetric: opts.DisableMetric,
DisableTrace: opts.DisableTrace,
DB: opts.Database,
Addr: util.Implode(";", opts.Hosts),
})
}
r.client = tc
r.clientType = ClientNormal
}
r.fmtString = opts.KeyPrefix + "%s"
return r
}
// // IsCluster 判断是否集群
// func (r *Client) IsCluster() bool {
// if r.opts.Type == ClientCluster {
// return true
// }
// return false
// }
// Prefix 返回前缀+键
func (r *Client) Prefix(key string) string {
return fmt.Sprintf(r.fmtString, key)
}
// k 格式化并返回带前缀的密钥
func (r *Client) k(key string) string {
return fmt.Sprintf(r.fmtString, key)
}
// ks 使用前缀格式化并返回一组键
func (r *Client) ks(key ...string) []string {
keys := make([]string, len(key))
for i, k := range key {
keys[i] = r.k(k)
}
return keys
}
// GetClient 返回客户端
func (r *Client) GetClient() (res redis.Cmdable) {
switch r.clientType {
case ClientCluster:
res = r.clusterClient
case ClientNormal:
res = r.client
}
return res
}
// MGetByPipeline gets multiple values from keys,Pipeline is used when
// redis is a cluster,This means higher IO performance
// params: keys ...string
// return: []string, error
func (r *Client) MGetByPipeline(ctx context.Context, keys ...string) ([]string, error) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
var res []string
if r.clientType == ClientCluster {
start := time.Now()
pipeLineLen := 100
pipeCount := len(keys)/pipeLineLen + 1
pipes := make([]redis.Pipeliner, pipeCount)
for i := 0; i < pipeCount; i++ {
pipes[i] = r.client.Pipeline()
}
for i, k := range keys {
p := pipes[i%pipeCount]
p.Get(ctx, r.k(k))
}
logger.Logger.Debug("process cost: ", time.Since(start))
start = time.Now()
var wg sync.WaitGroup
var lock sync.Mutex
errors := make(chan error, pipeCount)
for _, p := range pipes {
p := p
wg.Add(1)
go func() {
defer wg.Done()
cmders, err := p.Exec(ctx)
if err != nil {
select {
case errors <- err:
default:
}
return
}
lock.Lock()
defer lock.Unlock()
for _, cmder := range cmders {
result, _ := cmder.(*redis.StringCmd).Result()
res = append(res, result)
}
}()
}
wg.Wait()
logger.Logger.Debug("exec cost: ", time.Since(start))
if len(errors) > 0 {
return nil, <-errors
}
return res, nil
}
vals, err := r.client.MGet(ctx, keys...).Result()
if redis.Nil != err && nil != err {
return nil, err
}
for _, item := range vals {
res = append(res, fmt.Sprintf("%s", item))
}
return res, err
}
// Pipeline 获取管道
func (r *Client) Pipeline() (res redis.Pipeliner) {
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Pipeline()
case ClientNormal:
res = r.client.Pipeline()
}
return res
}
// Pipelined 管道
func (r *Client) Pipelined(ctx context.Context, fn func(redis.Pipeliner) error) (res []redis.Cmder, err error) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res, err = r.clusterClient.Pipelined(ctx, fn)
case ClientNormal:
res, err = r.client.Pipelined(ctx, fn)
}
return res, err
}
// TxPipeline 获取管道
func (r *Client) TxPipeline() (res redis.Pipeliner) {
switch r.clientType {
case ClientCluster:
res = r.clusterClient.TxPipeline()
case ClientNormal:
res = r.client.TxPipeline()
}
return res
}
// TxPipelined 管道
func (r *Client) TxPipelined(ctx context.Context, fn func(redis.Pipeliner) error) (res []redis.Cmder, err error) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res, err = r.clusterClient.TxPipelined(ctx, fn)
case ClientNormal:
res, err = r.client.TxPipelined(ctx, fn)
}
return res, err
}
// Command 返回有关所有Redis命令的详细信息的Array回复
func (r *Client) Command(ctx context.Context) (res *redis.CommandsInfoCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Command(ctx)
case ClientNormal:
res = r.client.Command(ctx)
}
return res
}
// ClientGetName returns the name of the connection.
func (r *Client) ClientGetName(ctx context.Context) (res *redis.StringCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.ClientGetName(ctx)
case ClientNormal:
res = r.client.ClientGetName(ctx)
}
return res
}
// Echo 批量字符串回复
func (r *Client) Echo(ctx context.Context, message interface{}) (res *redis.StringCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Echo(ctx, message)
case ClientNormal:
res = r.client.Echo(ctx, message)
}
return res
}
// Ping 使用客户端向 Redis 服务器发送一个 PING ,如果服务器运作正常的话,会返回一个 PONG 。
// 通常用于测试与服务器的连接是否仍然生效,或者用于测量延迟值。
// 如果连接正常就返回一个 PONG ,否则返回一个连接错误。
func (r *Client) Ping(ctx context.Context) (res *redis.StatusCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Ping(ctx)
case ClientNormal:
res = r.client.Ping(ctx)
}
return res
}
// Quit 关闭连接
func (r *Client) Quit(ctx context.Context) (res *redis.StatusCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Quit(ctx)
case ClientNormal:
res = r.client.Quit(ctx)
}
return res
}
// Del 删除给定的一个或多个 key 。
// 不存在的 key 会被忽略。
func (r *Client) Del(ctx context.Context, keys ...string) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Del(ctx, r.ks(keys...)...)
case ClientNormal:
res = r.client.Del(ctx, r.ks(keys...)...)
}
return res
}
// Unlink 这个命令非常类似于DEL:它删除指定的键。就像DEL键一样,如果它不存在,它将被忽略。但是,该命令在不同的线程中执行实际的内存回收,所以它不会阻塞,而DEL是。这是命令名称的来源:命令只是将键与键空间断开连接。实际删除将在以后异步发生。
func (r *Client) Unlink(ctx context.Context, keys ...string) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Unlink(ctx, r.ks(keys...)...)
case ClientNormal:
res = r.client.Unlink(ctx, r.ks(keys...)...)
}
return res
}
// Dump 序列化给定 key ,并返回被序列化的值,使用 RESTORE 命令可以将这个值反序列化为 Redis 键。
// 如果 key 不存在,那么返回 nil 。
// 否则,返回序列化之后的值。
func (r *Client) Dump(ctx context.Context, key string) (res *redis.StringCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Dump(ctx, r.k(key))
case ClientNormal:
res = r.client.Dump(ctx, r.k(key))
}
return res
}
// Exists 检查给定 key 是否存在。
// 若 key 存在,返回 1 ,否则返回 0 。
func (r *Client) Exists(ctx context.Context, key ...string) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Exists(ctx, r.ks(key...)...)
case ClientNormal:
res = r.client.Exists(ctx, r.ks(key...)...)
}
return res
}
// Expire 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除。
// 设置成功返回 1 。
// 当 key 不存在或者不能为 key 设置生存时间时(比如在低于 2.1.3 版本的 Redis 中你尝试更新 key 的生存时间),返回 0 。
func (r *Client) Expire(ctx context.Context, key string, expiration time.Duration) (res *redis.BoolCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Expire(ctx, r.k(key), expiration)
case ClientNormal:
res = r.client.Expire(ctx, r.k(key), expiration)
}
return res
}
// ExpireAt EXPIREAT 的作用和 EXPIRE 类似,都用于为 key 设置生存时间。
// 命令用于以 UNIX 时间戳(unix timestamp)格式设置 key 的过期时间
func (r *Client) ExpireAt(ctx context.Context, key string, tm time.Time) (res *redis.BoolCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.ExpireAt(ctx, r.k(key), tm)
case ClientNormal:
res = r.client.ExpireAt(ctx, r.k(key), tm)
}
return res
}
// Keys 查找所有符合给定模式 pattern 的 key 。
func (r *Client) Keys(ctx context.Context, pattern string) (res *redis.StringSliceCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Keys(ctx, r.k(pattern))
case ClientNormal:
res = r.client.Keys(ctx, r.k(pattern))
}
return res
}
// Migrate 将 key 原子性地从当前实例传送到目标实例的指定数据库上,一旦传送成功, key 保证会出现在目标实例上,而当前实例上的 key 会被删除。
func (r *Client) Migrate(ctx context.Context, host, port, key string, db int, timeout time.Duration) (res *redis.StatusCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Migrate(ctx, host, port, r.k(key), db, timeout)
case ClientNormal:
res = r.client.Migrate(ctx, host, port, r.k(key), db, timeout)
}
return res
}
// Move 将当前数据库的 key 移动到给定的数据库 db 当中。
// 如果当前数据库(源数据库)和给定数据库(目标数据库)有相同名字的给定 key ,或者 key 不存在于当前数据库,那么 MOVE 没有任何效果。
func (r *Client) Move(ctx context.Context, key string, db int) (res *redis.BoolCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Move(ctx, r.k(key), db)
case ClientNormal:
res = r.client.Move(ctx, r.k(key), db)
}
return res
}
// ObjectRefCount 返回给定 key 引用所储存的值的次数。此命令主要用于除错。
func (r *Client) ObjectRefCount(ctx context.Context, key string) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.ObjectRefCount(ctx, r.k(key))
case ClientNormal:
res = r.client.ObjectRefCount(ctx, r.k(key))
}
return res
}
// ObjectEncoding 返回给定 key 锁储存的值所使用的内部表示(representation)。
func (r *Client) ObjectEncoding(ctx context.Context, key string) (res *redis.StringCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.ObjectEncoding(ctx, r.k(key))
case ClientNormal:
res = r.client.ObjectEncoding(ctx, r.k(key))
}
return res
}
// ObjectIdleTime 返回给定 key 自储存以来的空转时间(idle, 没有被读取也没有被写入),以秒为单位。
func (r *Client) ObjectIdleTime(ctx context.Context, key string) (res *redis.DurationCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.ObjectIdleTime(ctx, r.k(key))
case ClientNormal:
res = r.client.ObjectIdleTime(ctx, r.k(key))
}
return res
}
// Persist 移除给定 key 的生存时间,将这个 key 从『易失的』(带生存时间 key )转换成『持久的』(一个不带生存时间、永不过期的 key )。
// 当生存时间移除成功时,返回 1 .
// 如果 key 不存在或 key 没有设置生存时间,返回 0 。
func (r *Client) Persist(ctx context.Context, key string) (res *redis.BoolCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Persist(ctx, r.k(key))
case ClientNormal:
res = r.client.Persist(ctx, r.k(key))
}
return res
}
// PExpire 毫秒为单位设置 key 的生存时间
func (r *Client) PExpire(ctx context.Context, key string, expiration time.Duration) (res *redis.BoolCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.PExpire(ctx, r.k(key), expiration)
case ClientNormal:
res = r.client.PExpire(ctx, r.k(key), expiration)
}
return res
}
// PExpireAt 这个命令和 expireat 命令类似,但它以毫秒为单位设置 key 的过期 unix 时间戳,而不是像 expireat 那样,以秒为单位。
// 如果生存时间设置成功,返回 1 。 当 key 不存在或没办法设置生存时间时,返回 0
func (r *Client) PExpireAt(ctx context.Context, key string, tm time.Time) (res *redis.BoolCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.PExpireAt(ctx, r.k(key), tm)
case ClientNormal:
res = r.client.PExpireAt(ctx, r.k(key), tm)
}
return res
}
// PTTL 这个命令类似于 TTL 命令,但它以毫秒为单位返回 key 的剩余生存时间,而不是像 TTL 命令那样,以秒为单位。
// 当 key 不存在时,返回 -2 。
// 当 key 存在但没有设置剩余生存时间时,返回 -1 。
// 否则,以毫秒为单位,返回 key 的剩余生存时间。
func (r *Client) PTTL(ctx context.Context, key string) (res *redis.DurationCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.PTTL(ctx, r.k(key))
case ClientNormal:
res = r.client.PTTL(ctx, r.k(key))
}
return res
}
// RandomKey 从当前数据库中随机返回(不删除)一个 key 。
func (r *Client) RandomKey(ctx context.Context) (res *redis.StringCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.RandomKey(ctx)
case ClientNormal:
res = r.client.RandomKey(ctx)
}
return res
}
// Rename 将 key 改名为 newkey 。
// 当 key 和 newkey 相同,或者 key 不存在时,返回一个错误。
// 当 newkey 已经存在时, RENAME 命令将覆盖旧值。
func (r *Client) Rename(ctx context.Context, key, newkey string) (res *redis.StatusCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Rename(ctx, r.k(key), r.k(newkey))
case ClientNormal:
res = r.client.Rename(ctx, r.k(key), r.k(newkey))
}
return res
}
// RenameNX 当且仅当 newkey 不存在时,将 key 改名为 newkey 。
// 当 key 不存在时,返回一个错误。
func (r *Client) RenameNX(ctx context.Context, key, newkey string) (res *redis.BoolCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.RenameNX(ctx, r.k(key), r.k(newkey))
case ClientNormal:
res = r.client.RenameNX(ctx, r.k(key), r.k(newkey))
}
return res
}
// Restore 反序列化给定的序列化值,并将它和给定的 key 关联。
// 参数 ttl 以毫秒为单位为 key 设置生存时间;如果 ttl 为 0 ,那么不设置生存时间。
// RESTORE 在执行反序列化之前会先对序列化值的 RDB 版本和数据校验和进行检查,如果 RDB 版本不相同或者数据不完整的话,那么 RESTORE 会拒绝进行反序列化,并返回一个错误。
func (r *Client) Restore(ctx context.Context, key string, ttl time.Duration, value string) (res *redis.StatusCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Restore(ctx, r.k(key), ttl, value)
case ClientNormal:
res = r.client.Restore(ctx, r.k(key), ttl, value)
}
return res
}
// RestoreReplace -> Restore
func (r *Client) RestoreReplace(ctx context.Context, key string, ttl time.Duration, value string) (res *redis.StatusCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.RestoreReplace(ctx, r.k(key), ttl, value)
case ClientNormal:
res = r.client.RestoreReplace(ctx, r.k(key), ttl, value)
}
return res
}
// Sort 返回或保存给定列表、集合、有序集合 key 中经过排序的元素。
// 排序默认以数字作为对象,值被解释为双精度浮点数,然后进行比较。
func (r *Client) Sort(ctx context.Context, key string, sort *redis.Sort) (res *redis.StringSliceCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Sort(ctx, r.k(key), sort)
case ClientNormal:
res = r.client.Sort(ctx, r.k(key), sort)
}
return res
}
// SortStore -> Sort
func (r *Client) SortStore(ctx context.Context, key, store string, sort *redis.Sort) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.SortStore(ctx, r.k(key), store, sort)
case ClientNormal:
res = r.client.SortStore(ctx, r.k(key), store, sort)
}
return res
}
// SortInterfaces -> Sort
func (r *Client) SortInterfaces(ctx context.Context, key string, sort *redis.Sort) (res *redis.SliceCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.SortInterfaces(ctx, r.k(key), sort)
case ClientNormal:
res = r.client.SortInterfaces(ctx, r.k(key), sort)
}
return res
}
// Touch 更改键的上次访问时间。返回指定的现有键的数量。
func (r *Client) Touch(ctx context.Context, keys ...string) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Touch(ctx, r.ks(keys...)...)
case ClientNormal:
res = r.client.Touch(ctx, r.ks(keys...)...)
}
return res
}
// TTL 以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)。
// 当 key 不存在时,返回 -2 。
// 当 key 存在但没有设置剩余生存时间时,返回 -1 。
// 否则,以秒为单位,返回 key 的剩余生存时间。
func (r *Client) TTL(ctx context.Context, key string) (res *redis.DurationCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.TTL(ctx, r.k(key))
case ClientNormal:
res = r.client.TTL(ctx, r.k(key))
}
return res
}
// Type 返回 key 所储存的值的类型。
func (r *Client) Type(ctx context.Context, key string) (res *redis.StatusCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Type(ctx, r.k(key))
case ClientNormal:
res = r.client.Type(ctx, r.k(key))
}
return res
}
// Scan 命令及其相关的 SSCAN 命令、 HSCAN 命令和 ZSCAN 命令都用于增量地迭代(incrementally iterate)一集元素
func (r *Client) Scan(ctx context.Context, cursor uint64, match string, count int64) (res *redis.ScanCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Scan(ctx, cursor, r.k(match), count)
case ClientNormal:
res = r.client.Scan(ctx, cursor, r.k(match), count)
}
return res
}
// SScan 详细信息请参考 SCAN 命令。
func (r *Client) SScan(ctx context.Context, key string, cursor uint64, match string, count int64) (res *redis.ScanCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.SScan(ctx, r.k(key), cursor, match, count)
case ClientNormal:
res = r.client.SScan(ctx, r.k(key), cursor, match, count)
}
return res
}
// HScan 详细信息请参考 SCAN 命令。
func (r *Client) HScan(ctx context.Context, key string, cursor uint64, match string, count int64) (res *redis.ScanCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.HScan(ctx, r.k(key), cursor, match, count)
case ClientNormal:
res = r.client.HScan(ctx, r.k(key), cursor, match, count)
}
return res
}
// ZScan 详细信息请参考 SCAN 命令。
func (r *Client) ZScan(ctx context.Context, key string, cursor uint64, match string, count int64) (res *redis.ScanCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.ZScan(ctx, r.k(key), cursor, match, count)
case ClientNormal:
res = r.client.ZScan(ctx, r.k(key), cursor, match, count)
}
return res
}
// Append 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。
// 如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。
func (r *Client) Append(ctx context.Context, key, value string) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Append(ctx, r.k(key), value)
case ClientNormal:
res = r.client.Append(ctx, r.k(key), value)
}
return res
}
// BitCount 计算给定字符串中,被设置为 1 的比特位的数量。
// 一般情况下,给定的整个字符串都会被进行计数,通过指定额外的 start 或 end 参数,可以让计数只在特定的位上进行。
// start 和 end 参数的设置和 GETRANGE 命令类似,都可以使用负数值:比如 -1 表示最后一个位,而 -2 表示倒数第二个位,以此类推。
// 不存在的 key 被当成是空字符串来处理,因此对一个不存在的 key 进行 BITCOUNT 操作,结果为 0 。
func (r *Client) BitCount(ctx context.Context, key string, bitCount *redis.BitCount) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.BitCount(ctx, r.k(key), bitCount)
case ClientNormal:
res = r.client.BitCount(ctx, r.k(key), bitCount)
}
return res
}
// BitOpAnd -> BitCount
func (r *Client) BitOpAnd(ctx context.Context, destKey string, keys ...string) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.BitOpAnd(ctx, r.k(destKey), r.ks(keys...)...)
case ClientNormal:
res = r.client.BitOpAnd(ctx, r.k(destKey), r.ks(keys...)...)
}
return res
}
// BitOpOr -> BitCount
func (r *Client) BitOpOr(ctx context.Context, destKey string, keys ...string) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.BitOpOr(ctx, r.k(destKey), r.ks(keys...)...)
case ClientNormal:
res = r.client.BitOpOr(ctx, r.k(destKey), r.ks(keys...)...)
}
return res
}
// BitOpXor -> BitCount
func (r *Client) BitOpXor(ctx context.Context, destKey string, keys ...string) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.BitOpXor(ctx, r.k(destKey), r.ks(keys...)...)
case ClientNormal:
res = r.client.BitOpXor(ctx, r.k(destKey), r.ks(keys...)...)
}
return res
}
// BitOpNot -> BitCount
func (r *Client) BitOpNot(ctx context.Context, destKey string, key string) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.BitOpXor(ctx, r.k(destKey), r.k(key))
case ClientNormal:
res = r.client.BitOpXor(ctx, r.k(destKey), r.k(key))
}
return res
}
// BitPos -> BitCount
func (r *Client) BitPos(ctx context.Context, key string, bit int64, pos ...int64) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.BitPos(ctx, r.k(key), bit, pos...)
case ClientNormal:
res = r.client.BitPos(ctx, r.k(key), bit, pos...)
}
return res
}
// BitField -> BitCount
func (r *Client) BitField(ctx context.Context, key string, args ...interface{}) (res *redis.IntSliceCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.BitField(ctx, r.k(key), args...)
case ClientNormal:
res = r.client.BitField(ctx, r.k(key), args...)
}
return res
}
// Decr 将 key 中储存的数字值减一。
// 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 DECR 操作。
// 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
// 本操作的值限制在 64 位(bit)有符号数字表示之内。
// 关于递增(increment) / 递减(decrement)操作的更多信息,请参见 INCR 命令。
// 执行 DECR 命令之后 key 的值。
func (r *Client) Decr(ctx context.Context, key string) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Decr(ctx, r.k(key))
case ClientNormal:
res = r.client.Decr(ctx, r.k(key))
}
return res
}
// DecrBy 将 key 所储存的值减去减量 decrement 。
// 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 DECRBY 操作。
// 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
// 本操作的值限制在 64 位(bit)有符号数字表示之内。
// 关于更多递增(increment) / 递减(decrement)操作的更多信息,请参见 INCR 命令。
// 减去 decrement 之后, key 的值。
func (r *Client) DecrBy(ctx context.Context, key string, value int64) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.DecrBy(ctx, r.k(key), value)
case ClientNormal:
res = r.client.DecrBy(ctx, r.k(key), value)
}
return res
}
// Get 返回 key 所关联的字符串值。
// 如果 key 不存在那么返回特殊值 nil 。
// 假如 key 储存的值不是字符串类型,返回一个错误,因为 GET 只能用于处理字符串值。
// 当 key 不存在时,返回 nil ,否则,返回 key 的值。
// 如果 key 不是字符串类型,那么返回一个错误。
func (r *Client) Get(ctx context.Context, key string) (res *redis.StringCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.Get(ctx, r.k(key))
case ClientNormal:
res = r.client.Get(ctx, r.k(key))
}
return res
}
// GetBit 对 key 所储存的字符串值,获取指定偏移量上的位(bit)。
// 当 offset 比字符串值的长度大,或者 key 不存在时,返回 0 。
// 字符串值指定偏移量上的位(bit)。
func (r *Client) GetBit(ctx context.Context, key string, offset int64) (res *redis.IntCmd) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
switch r.clientType {
case ClientCluster:
res = r.clusterClient.GetBit(ctx, r.k(key), offset)
case ClientNormal:
res = r.client.GetBit(ctx, r.k(key), offset)
}
return res
}
// GetRange 返回 key 中字符串值的子字符串,字符串的截取范围由 start 和 end 两个偏移量决定(包括 start 和 end 在内)。
// 负数偏移量表示从字符串最后开始计数, -1 表示最后一个字符, -2 表示倒数第二个,以此类推。