1
2
3 package example_commands_test
4
5 import (
6 "context"
7 "fmt"
8
9 "github.com/redis/go-redis/v9"
10 )
11
12
13
14
15 func UNUSED(v ...interface{}) {}
16
17
18
19 func ExampleClient_xadd() {
20 ctx := context.Background()
21
22 rdb := redis.NewClient(&redis.Options{
23 Addr: "localhost:6379",
24 Password: "",
25 DB: 0,
26 })
27
28
29
30 rdb.FlushDB(ctx)
31 rdb.Del(ctx, "race:france")
32
33
34
35 res1, err := rdb.XAdd(ctx, &redis.XAddArgs{
36 Stream: "race:france",
37 Values: map[string]interface{}{
38 "rider": "Castilla",
39 "speed": 30.2,
40 "position": 1,
41 "location_id": 1,
42 },
43 }).Result()
44
45 if err != nil {
46 panic(err)
47 }
48
49
50
51 res2, err := rdb.XAdd(ctx, &redis.XAddArgs{
52 Stream: "race:france",
53 Values: map[string]interface{}{
54 "rider": "Norem",
55 "speed": 28.8,
56 "position": 3,
57 "location_id": 1,
58 },
59 }).Result()
60
61 if err != nil {
62 panic(err)
63 }
64
65
66
67 res3, err := rdb.XAdd(ctx, &redis.XAddArgs{
68 Stream: "race:france",
69 Values: map[string]interface{}{
70 "rider": "Prickett",
71 "speed": 29.7,
72 "position": 2,
73 "location_id": 1,
74 },
75 }).Result()
76
77 if err != nil {
78 panic(err)
79 }
80
81
82
83
84
85 UNUSED(res1, res2, res3)
86
87
88 xlen, err := rdb.XLen(ctx, "race:france").Result()
89
90 if err != nil {
91 panic(err)
92 }
93
94 fmt.Println(xlen)
95
96
97
98 }
99
100 func ExampleClient_racefrance1() {
101 ctx := context.Background()
102
103 rdb := redis.NewClient(&redis.Options{
104 Addr: "localhost:6379",
105 Password: "",
106 DB: 0,
107 })
108
109
110
111 rdb.FlushDB(ctx)
112 rdb.Del(ctx, "race:france")
113
114
115 _, err := rdb.XAdd(ctx, &redis.XAddArgs{
116 Stream: "race:france",
117 Values: map[string]interface{}{
118 "rider": "Castilla",
119 "speed": 30.2,
120 "position": 1,
121 "location_id": 1,
122 },
123 ID: "1692632086370-0",
124 }).Result()
125
126 if err != nil {
127 panic(err)
128 }
129
130 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
131 Stream: "race:france",
132 Values: map[string]interface{}{
133 "rider": "Norem",
134 "speed": 28.8,
135 "position": 3,
136 "location_id": 1,
137 },
138 ID: "1692632094485-0",
139 }).Result()
140
141 if err != nil {
142 panic(err)
143 }
144
145 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
146 Stream: "race:france",
147 Values: map[string]interface{}{
148 "rider": "Prickett",
149 "speed": 29.7,
150 "position": 2,
151 "location_id": 1,
152 },
153 ID: "1692632102976-0",
154 }).Result()
155
156 if err != nil {
157 panic(err)
158 }
159
160
161 res4, err := rdb.XRangeN(ctx, "race:france", "1691765278160-0", "+", 2).Result()
162
163 if err != nil {
164 panic(err)
165 }
166
167 fmt.Println(res4)
168
169
170
171
172 res5, err := rdb.XRead(ctx, &redis.XReadArgs{
173 Streams: []string{"race:france", "0"},
174 Count: 100,
175 Block: 300,
176 }).Result()
177
178 if err != nil {
179 panic(err)
180 }
181
182 fmt.Println(res5)
183
184
185
186
187 res6, err := rdb.XAdd(ctx, &redis.XAddArgs{
188 Stream: "race:france",
189 Values: map[string]interface{}{
190 "rider": "Castilla",
191 "speed": 29.9,
192 "position": 1,
193 "location_id": 2,
194 },
195 }).Result()
196
197 if err != nil {
198 panic(err)
199 }
200
201
202
203
204
205 res7, err := rdb.XLen(ctx, "race:france").Result()
206
207 if err != nil {
208 panic(err)
209 }
210
211 fmt.Println(res7)
212
213
214
215 UNUSED(res6)
216
217
218
219
220
221
222 }
223
224 func ExampleClient_raceusa() {
225 ctx := context.Background()
226
227 rdb := redis.NewClient(&redis.Options{
228 Addr: "localhost:6379",
229 Password: "",
230 DB: 0,
231 })
232
233
234
235 rdb.FlushDB(ctx)
236 rdb.Del(ctx, "race:usa")
237
238
239
240 res8, err := rdb.XAdd(ctx, &redis.XAddArgs{
241 Stream: "race:usa",
242 Values: map[string]interface{}{
243 "racer": "Castilla",
244 },
245 ID: "0-1",
246 }).Result()
247
248 if err != nil {
249 panic(err)
250 }
251
252 fmt.Println(res8)
253
254 res9, err := rdb.XAdd(ctx, &redis.XAddArgs{
255 Stream: "race:usa",
256 Values: map[string]interface{}{
257 "racer": "Norem",
258 },
259 ID: "0-2",
260 }).Result()
261
262 if err != nil {
263 panic(err)
264 }
265
266 fmt.Println(res9)
267
268
269
270 res10, err := rdb.XAdd(ctx, &redis.XAddArgs{
271 Values: map[string]interface{}{
272 "racer": "Prickett",
273 },
274 ID: "0-1",
275 }).Result()
276
277 if err != nil {
278
279
280 }
281
282
283
284 res11, err := rdb.XAdd(ctx, &redis.XAddArgs{
285 Stream: "race:usa",
286 Values: map[string]interface{}{
287 "racer": "Prickett",
288 },
289 ID: "0-*",
290 }).Result()
291
292 if err != nil {
293 panic(err)
294 }
295
296 fmt.Println(res11)
297
298
299
300 UNUSED(res10)
301
302
303
304
305
306
307 }
308
309 func ExampleClient_racefrance2() {
310 ctx := context.Background()
311
312 rdb := redis.NewClient(&redis.Options{
313 Addr: "localhost:6379",
314 Password: "",
315 DB: 0,
316 })
317
318
319
320 rdb.FlushDB(ctx)
321 rdb.Del(ctx, "race:france")
322
323
324 _, err := rdb.XAdd(ctx, &redis.XAddArgs{
325 Stream: "race:france",
326 Values: map[string]interface{}{
327 "rider": "Castilla",
328 "speed": 30.2,
329 "position": 1,
330 "location_id": 1,
331 },
332 ID: "1692632086370-0",
333 }).Result()
334
335 if err != nil {
336 panic(err)
337 }
338
339 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
340 Stream: "race:france",
341 Values: map[string]interface{}{
342 "rider": "Norem",
343 "speed": 28.8,
344 "position": 3,
345 "location_id": 1,
346 },
347 ID: "1692632094485-0",
348 }).Result()
349
350 if err != nil {
351 panic(err)
352 }
353
354 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
355 Stream: "race:france",
356 Values: map[string]interface{}{
357 "rider": "Prickett",
358 "speed": 29.7,
359 "position": 2,
360 "location_id": 1,
361 },
362 ID: "1692632102976-0",
363 }).Result()
364
365 if err != nil {
366 panic(err)
367 }
368
369 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
370 Stream: "race:france",
371 Values: map[string]interface{}{
372 "rider": "Castilla",
373 "speed": 29.9,
374 "position": 1,
375 "location_id": 2,
376 },
377 ID: "1692632147973-0",
378 }).Result()
379
380 if err != nil {
381 panic(err)
382 }
383
384 res12, err := rdb.XRange(ctx, "race:france", "-", "+").Result()
385
386 if err != nil {
387 panic(err)
388 }
389
390 fmt.Println(res12)
391
392
393
394
395 res13, err := rdb.XRange(ctx, "race:france",
396 "1692632086369", "1692632086371",
397 ).Result()
398
399 if err != nil {
400 panic(err)
401 }
402
403 fmt.Println(res13)
404
405
406
407
408 res14, err := rdb.XRangeN(ctx, "race:france", "-", "+", 2).Result()
409
410 if err != nil {
411 panic(err)
412 }
413
414 fmt.Println(res14)
415
416
417
418
419 res15, err := rdb.XRangeN(ctx, "race:france",
420 "(1692632094485-0", "+", 2,
421 ).Result()
422
423 if err != nil {
424 panic(err)
425 }
426
427 fmt.Println(res15)
428
429
430
431
432 res16, err := rdb.XRangeN(ctx, "race:france",
433 "(1692632147973-0", "+", 2,
434 ).Result()
435
436 if err != nil {
437 panic(err)
438 }
439
440 fmt.Println(res16)
441
442
443
444
445 res17, err := rdb.XRevRangeN(ctx, "race:france", "+", "-", 1).Result()
446
447 if err != nil {
448 panic(err)
449 }
450
451 fmt.Println(res17)
452
453
454
455
456 res18, err := rdb.XRead(ctx, &redis.XReadArgs{
457 Streams: []string{"race:france", "0"},
458 Count: 2,
459 }).Result()
460
461 if err != nil {
462 panic(err)
463 }
464
465 fmt.Println(res18)
466
467
468
469
470
471
472
473
474
475
476
477 }
478
479 func ExampleClient_xgroupcreate() {
480 ctx := context.Background()
481
482 rdb := redis.NewClient(&redis.Options{
483 Addr: "localhost:6379",
484 Password: "",
485 DB: 0,
486 })
487
488
489
490 rdb.FlushDB(ctx)
491 rdb.Del(ctx, "race:france")
492
493
494 _, err := rdb.XAdd(ctx, &redis.XAddArgs{
495 Stream: "race:france",
496 Values: map[string]interface{}{
497 "rider": "Castilla",
498 "speed": 30.2,
499 "position": 1,
500 "location_id": 1,
501 },
502 ID: "1692632086370-0",
503 }).Result()
504
505 if err != nil {
506 panic(err)
507 }
508
509
510 res19, err := rdb.XGroupCreate(ctx, "race:france", "france_riders", "$").Result()
511
512 if err != nil {
513 panic(err)
514 }
515
516 fmt.Println(res19)
517
518
519
520
521 }
522
523 func ExampleClient_xgroupcreatemkstream() {
524 ctx := context.Background()
525
526 rdb := redis.NewClient(&redis.Options{
527 Addr: "localhost:6379",
528 Password: "",
529 DB: 0,
530 })
531
532
533
534 rdb.FlushDB(ctx)
535 rdb.Del(ctx, "race:italy")
536
537
538
539 res20, err := rdb.XGroupCreateMkStream(ctx,
540 "race:italy", "italy_riders", "$",
541 ).Result()
542
543 if err != nil {
544 panic(err)
545 }
546
547 fmt.Println(res20)
548
549
550
551
552 }
553
554 func ExampleClient_xgroupread() {
555 ctx := context.Background()
556
557 rdb := redis.NewClient(&redis.Options{
558 Addr: "localhost:6379",
559 Password: "",
560 DB: 0,
561 })
562
563
564
565 rdb.FlushDB(ctx)
566 rdb.Del(ctx, "race:italy")
567
568
569 _, err := rdb.XGroupCreateMkStream(ctx,
570 "race:italy", "italy_riders", "$",
571 ).Result()
572
573 if err != nil {
574 panic(err)
575 }
576
577
578 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
579 Stream: "race:italy",
580 Values: map[string]interface{}{"rider": "Castilla"},
581 }).Result()
582
583
584 if err != nil {
585 panic(err)
586 }
587
588 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
589 Stream: "race:italy",
590 Values: map[string]interface{}{"rider": "Royce"},
591 }).Result()
592
593
594 if err != nil {
595 panic(err)
596 }
597
598 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
599 Stream: "race:italy",
600 Values: map[string]interface{}{"rider": "Sam-Bodden"},
601 }).Result()
602
603
604 if err != nil {
605 panic(err)
606 }
607
608 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
609 Stream: "race:italy",
610 Values: map[string]interface{}{"rider": "Prickett"},
611 }).Result()
612
613
614 if err != nil {
615 panic(err)
616 }
617
618 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
619 Stream: "race:italy",
620 Values: map[string]interface{}{"rider": "Norem"},
621 }).Result()
622
623
624 if err != nil {
625 panic(err)
626 }
627
628
629
630 res21, err := rdb.XReadGroup(ctx, &redis.XReadGroupArgs{
631 Streams: []string{"race:italy", ">"},
632 Group: "italy_riders",
633 Consumer: "Alice",
634 Count: 1,
635 }).Result()
636
637 if err != nil {
638 panic(err)
639 }
640
641
642
643
644
645
646 UNUSED(res21)
647
648
649 xlen, err := rdb.XLen(ctx, "race:italy").Result()
650
651 if err != nil {
652 panic(err)
653 }
654
655 fmt.Println(xlen)
656
657
658
659 }
660
661 func ExampleClient_raceitaly() {
662 ctx := context.Background()
663
664 rdb := redis.NewClient(&redis.Options{
665 Addr: "localhost:6379",
666 Password: "",
667 DB: 0,
668 })
669
670
671
672 rdb.FlushDB(ctx)
673 rdb.Del(ctx, "race:italy")
674 rdb.XGroupDestroy(ctx, "race:italy", "italy_riders")
675
676
677 _, err := rdb.XGroupCreateMkStream(ctx,
678 "race:italy", "italy_riders", "$",
679 ).Result()
680
681 if err != nil {
682 panic(err)
683 }
684
685 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
686 Stream: "race:italy",
687 Values: map[string]interface{}{"rider": "Castilla"},
688 ID: "1692632639151-0",
689 }).Result()
690
691 if err != nil {
692 panic(err)
693 }
694
695 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
696 Stream: "race:italy",
697 Values: map[string]interface{}{"rider": "Royce"},
698 ID: "1692632647899-0",
699 }).Result()
700
701 if err != nil {
702 panic(err)
703 }
704
705 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
706 Stream: "race:italy",
707 Values: map[string]interface{}{"rider": "Sam-Bodden"},
708 ID: "1692632662819-0",
709 }).Result()
710
711 if err != nil {
712 panic(err)
713 }
714
715 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
716 Stream: "race:italy",
717 Values: map[string]interface{}{"rider": "Prickett"},
718 ID: "1692632670501-0",
719 }).Result()
720
721 if err != nil {
722 panic(err)
723 }
724
725 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
726 Stream: "race:italy",
727 Values: map[string]interface{}{"rider": "Norem"},
728 ID: "1692632678249-0",
729 }).Result()
730
731 if err != nil {
732 panic(err)
733 }
734
735 _, err = rdb.XReadGroup(ctx, &redis.XReadGroupArgs{
736 Streams: []string{"race:italy", ">"},
737 Group: "italy_riders",
738 Consumer: "Alice",
739 Count: 1,
740 }).Result()
741
742 if err != nil {
743 panic(err)
744 }
745
746 res22, err := rdb.XReadGroup(ctx, &redis.XReadGroupArgs{
747 Streams: []string{"race:italy", "0"},
748 Group: "italy_riders",
749 Consumer: "Alice",
750 }).Result()
751
752 if err != nil {
753 panic(err)
754 }
755
756 fmt.Println(res22)
757
758
759
760
761 res23, err := rdb.XAck(ctx,
762 "race:italy", "italy_riders", "1692632639151-0",
763 ).Result()
764
765 if err != nil {
766 panic(err)
767 }
768
769 fmt.Println(res23)
770
771 res24, err := rdb.XReadGroup(ctx, &redis.XReadGroupArgs{
772 Streams: []string{"race:italy", "0"},
773 Group: "italy_riders",
774 Consumer: "Alice",
775 }).Result()
776
777 if err != nil {
778 panic(err)
779 }
780
781 fmt.Println(res24)
782
783
784
785
786 res25, err := rdb.XReadGroup(ctx, &redis.XReadGroupArgs{
787 Streams: []string{"race:italy", ">"},
788 Group: "italy_riders",
789 Consumer: "Bob",
790 Count: 2,
791 }).Result()
792
793 if err != nil {
794 panic(err)
795 }
796
797 fmt.Println(res25)
798
799
800
801
802
803 res26, err := rdb.XPending(ctx, "race:italy", "italy_riders").Result()
804
805 if err != nil {
806 panic(err)
807 }
808
809 fmt.Println(res26)
810
811
812
813
814 res27, err := rdb.XPendingExt(ctx, &redis.XPendingExtArgs{
815 Stream: "race:italy",
816 Group: "italy_riders",
817 Start: "-",
818 End: "+",
819 Count: 10,
820 }).Result()
821
822 if err != nil {
823 panic(err)
824 }
825
826
827
828
829
830
831 res28, err := rdb.XRange(ctx, "race:italy",
832 "1692632647899-0", "1692632647899-0",
833 ).Result()
834
835 if err != nil {
836 panic(err)
837 }
838
839 fmt.Println(res28)
840
841
842
843 res29, err := rdb.XClaim(ctx, &redis.XClaimArgs{
844 Stream: "race:italy",
845 Group: "italy_riders",
846 Consumer: "Alice",
847 MinIdle: 0,
848 Messages: []string{"1692632647899-0"},
849 }).Result()
850
851 if err != nil {
852 panic(err)
853 }
854
855 fmt.Println(res29)
856
857
858
859 res30, res30a, err := rdb.XAutoClaim(ctx, &redis.XAutoClaimArgs{
860 Stream: "race:italy",
861 Group: "italy_riders",
862 Consumer: "Alice",
863 Start: "0-0",
864 Count: 1,
865 }).Result()
866
867 if err != nil {
868 panic(err)
869 }
870
871 fmt.Println(res30)
872 fmt.Println(res30a)
873
874
875
876 res31, res31a, err := rdb.XAutoClaim(ctx, &redis.XAutoClaimArgs{
877 Stream: "race:italy",
878 Group: "italy_riders",
879 Consumer: "Lora",
880 Start: "(1692632662819-0",
881 Count: 1,
882 }).Result()
883
884 if err != nil {
885 panic(err)
886 }
887
888 fmt.Println(res31)
889 fmt.Println(res31a)
890
891
892
893 res32, err := rdb.XInfoStream(ctx, "race:italy").Result()
894
895 if err != nil {
896 panic(err)
897 }
898
899 fmt.Println(res32)
900
901
902
903
904 res33, err := rdb.XInfoGroups(ctx, "race:italy").Result()
905
906 if err != nil {
907 panic(err)
908 }
909
910 fmt.Println(res33)
911
912
913
914
915 res34, err := rdb.XInfoConsumers(ctx, "race:italy", "italy_riders").Result()
916
917 if err != nil {
918 panic(err)
919 }
920
921
922
923
924
925
926 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
927 Stream: "race:italy",
928 MaxLen: 2,
929 Values: map[string]interface{}{"rider": "Jones"},
930 },
931 ).Result()
932
933 if err != nil {
934 panic(err)
935 }
936
937 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
938 Stream: "race:italy",
939 MaxLen: 2,
940 Values: map[string]interface{}{"rider": "Wood"},
941 },
942 ).Result()
943
944 if err != nil {
945 panic(err)
946 }
947
948 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
949 Stream: "race:italy",
950 MaxLen: 2,
951 Values: map[string]interface{}{"rider": "Henshaw"},
952 },
953 ).Result()
954
955 if err != nil {
956 panic(err)
957 }
958
959 res35, err := rdb.XLen(ctx, "race:italy").Result()
960
961 if err != nil {
962 panic(err)
963 }
964
965 fmt.Println(res35)
966
967 res36, err := rdb.XRange(ctx, "race:italy", "-", "+").Result()
968
969 if err != nil {
970 panic(err)
971 }
972
973
974
975
976
977
978 res37, err := rdb.XTrimMaxLen(ctx, "race:italy", 10).Result()
979
980 if err != nil {
981 panic(err)
982 }
983
984 fmt.Println(res37)
985
986
987
988 res38, err := rdb.XTrimMaxLenApprox(ctx, "race:italy", 10, 20).Result()
989
990 if err != nil {
991 panic(err)
992 }
993
994 fmt.Println(res38)
995
996
997
998 UNUSED(res27, res34, res36)
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018 }
1019
1020 func ExampleClient_xdel() {
1021 ctx := context.Background()
1022
1023 rdb := redis.NewClient(&redis.Options{
1024 Addr: "localhost:6379",
1025 Password: "",
1026 DB: 0,
1027 })
1028
1029
1030
1031 rdb.FlushDB(ctx)
1032 rdb.Del(ctx, "race:italy")
1033
1034
1035 _, err := rdb.XAdd(ctx, &redis.XAddArgs{
1036 Stream: "race:italy",
1037 MaxLen: 2,
1038 Values: map[string]interface{}{"rider": "Wood"},
1039 ID: "1692633198206-0",
1040 },
1041 ).Result()
1042
1043 if err != nil {
1044 panic(err)
1045 }
1046
1047 _, err = rdb.XAdd(ctx, &redis.XAddArgs{
1048 Stream: "race:italy",
1049 MaxLen: 2,
1050 Values: map[string]interface{}{"rider": "Henshaw"},
1051 ID: "1692633208557-0",
1052 },
1053 ).Result()
1054
1055 if err != nil {
1056 panic(err)
1057 }
1058
1059
1060 res39, err := rdb.XRangeN(ctx, "race:italy", "-", "+", 2).Result()
1061
1062 if err != nil {
1063 panic(err)
1064 }
1065
1066 fmt.Println(res39)
1067
1068
1069 res40, err := rdb.XDel(ctx, "race:italy", "1692633208557-0").Result()
1070
1071 if err != nil {
1072 panic(err)
1073 }
1074
1075 fmt.Println(res40)
1076
1077 res41, err := rdb.XRangeN(ctx, "race:italy", "-", "+", 2).Result()
1078
1079 if err != nil {
1080 panic(err)
1081 }
1082
1083 fmt.Println(res41)
1084
1085
1086
1087
1088
1089
1090
1091 }
1092
View as plain text