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 func ExampleClient_queue() {
15 ctx := context.Background()
16
17 rdb := redis.NewClient(&redis.Options{
18 Addr: "localhost:6379",
19 Password: "",
20 DB: 0,
21 })
22
23
24
25 rdb.FlushDB(ctx)
26 rdb.Del(ctx, "bikes:repairs")
27
28
29
30 res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
31
32 if err != nil {
33 panic(err)
34 }
35
36 fmt.Println(res1)
37
38 res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
39
40 if err != nil {
41 panic(err)
42 }
43
44 fmt.Println(res2)
45
46 res3, err := rdb.RPop(ctx, "bikes:repairs").Result()
47
48 if err != nil {
49 panic(err)
50 }
51
52 fmt.Println(res3)
53
54 res4, err := rdb.RPop(ctx, "bikes:repairs").Result()
55
56 if err != nil {
57 panic(err)
58 }
59
60 fmt.Println(res4)
61
62
63
64
65
66
67
68 }
69
70 func ExampleClient_stack() {
71 ctx := context.Background()
72
73 rdb := redis.NewClient(&redis.Options{
74 Addr: "localhost:6379",
75 Password: "",
76 DB: 0,
77 })
78
79
80
81 rdb.FlushDB(ctx)
82 rdb.Del(ctx, "bikes:repairs")
83
84
85
86 res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
87
88 if err != nil {
89 panic(err)
90 }
91
92 fmt.Println(res5)
93
94 res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
95
96 if err != nil {
97 panic(err)
98 }
99
100 fmt.Println(res6)
101
102 res7, err := rdb.LPop(ctx, "bikes:repairs").Result()
103
104 if err != nil {
105 panic(err)
106 }
107
108 fmt.Println(res7)
109
110 res8, err := rdb.LPop(ctx, "bikes:repairs").Result()
111
112 if err != nil {
113 panic(err)
114 }
115
116 fmt.Println(res8)
117
118
119
120
121
122
123
124 }
125
126 func ExampleClient_llen() {
127 ctx := context.Background()
128
129 rdb := redis.NewClient(&redis.Options{
130 Addr: "localhost:6379",
131 Password: "",
132 DB: 0,
133 })
134
135
136
137 rdb.FlushDB(ctx)
138 rdb.Del(ctx, "bikes:repairs")
139
140
141
142 res9, err := rdb.LLen(ctx, "bikes:repairs").Result()
143
144 if err != nil {
145 panic(err)
146 }
147
148 fmt.Println(res9)
149
150
151
152
153 }
154
155 func ExampleClient_lmove_lrange() {
156 ctx := context.Background()
157
158 rdb := redis.NewClient(&redis.Options{
159 Addr: "localhost:6379",
160 Password: "",
161 DB: 0,
162 })
163
164
165
166 rdb.FlushDB(ctx)
167 rdb.Del(ctx, "bikes:repairs")
168 rdb.Del(ctx, "bikes:finished")
169
170
171
172 res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
173
174 if err != nil {
175 panic(err)
176 }
177
178 fmt.Println(res10)
179
180 res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
181
182 if err != nil {
183 panic(err)
184 }
185
186 fmt.Println(res11)
187
188 res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result()
189
190 if err != nil {
191 panic(err)
192 }
193
194 fmt.Println(res12)
195
196 res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
197
198 if err != nil {
199 panic(err)
200 }
201
202 fmt.Println(res13)
203
204 res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result()
205
206 if err != nil {
207 panic(err)
208 }
209
210 fmt.Println(res14)
211
212
213
214
215
216
217
218
219 }
220
221 func ExampleClient_lpush_rpush() {
222 ctx := context.Background()
223
224 rdb := redis.NewClient(&redis.Options{
225 Addr: "localhost:6379",
226 Password: "",
227 DB: 0,
228 })
229
230
231
232 rdb.FlushDB(ctx)
233 rdb.Del(ctx, "bikes:repairs")
234
235
236
237 res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result()
238
239 if err != nil {
240 panic(err)
241 }
242
243 fmt.Println(res15)
244
245 res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result()
246
247 if err != nil {
248 panic(err)
249 }
250
251 fmt.Println(res16)
252
253 res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result()
254
255 if err != nil {
256 panic(err)
257 }
258
259 fmt.Println(res17)
260
261 res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
262
263 if err != nil {
264 panic(err)
265 }
266
267 fmt.Println(res18)
268
269
270
271
272
273
274
275 }
276
277 func ExampleClient_variadic() {
278 ctx := context.Background()
279
280 rdb := redis.NewClient(&redis.Options{
281 Addr: "localhost:6379",
282 Password: "",
283 DB: 0,
284 })
285
286
287
288 rdb.FlushDB(ctx)
289 rdb.Del(ctx, "bikes:repairs")
290
291
292
293 res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
294
295 if err != nil {
296 panic(err)
297 }
298
299 fmt.Println(res19)
300
301 res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result()
302
303 if err != nil {
304 panic(err)
305 }
306
307 fmt.Println(res20)
308
309 res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
310
311 if err != nil {
312 panic(err)
313 }
314
315 fmt.Println(res21)
316
317
318
319
320
321
322 }
323
324 func ExampleClient_lpop_rpop() {
325 ctx := context.Background()
326
327 rdb := redis.NewClient(&redis.Options{
328 Addr: "localhost:6379",
329 Password: "",
330 DB: 0,
331 })
332
333
334
335 rdb.FlushDB(ctx)
336 rdb.Del(ctx, "bikes:repairs")
337
338
339
340 res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
341
342 if err != nil {
343 panic(err)
344 }
345
346 fmt.Println(res22)
347
348 res23, err := rdb.RPop(ctx, "bikes:repairs").Result()
349
350 if err != nil {
351 panic(err)
352 }
353
354 fmt.Println(res23)
355
356 res24, err := rdb.LPop(ctx, "bikes:repairs").Result()
357
358 if err != nil {
359 panic(err)
360 }
361
362 fmt.Println(res24)
363
364 res25, err := rdb.RPop(ctx, "bikes:repairs").Result()
365
366 if err != nil {
367 panic(err)
368 }
369
370 fmt.Println(res25)
371
372 res26, err := rdb.RPop(ctx, "bikes:repairs").Result()
373
374 if err != nil {
375 fmt.Println(err)
376 }
377
378 fmt.Println(res26)
379
380
381
382
383
384
385
386
387
388
389 }
390
391 func ExampleClient_ltrim() {
392 ctx := context.Background()
393
394 rdb := redis.NewClient(&redis.Options{
395 Addr: "localhost:6379",
396 Password: "",
397 DB: 0,
398 })
399
400
401
402 rdb.FlushDB(ctx)
403 rdb.Del(ctx, "bikes:repairs")
404
405
406
407 res27, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
408
409 if err != nil {
410 panic(err)
411 }
412
413 fmt.Println(res27)
414
415 res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
416
417 if err != nil {
418 panic(err)
419 }
420
421 fmt.Println(res28)
422
423 res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
424
425 if err != nil {
426 panic(err)
427 }
428
429 fmt.Println(res29)
430
431
432
433
434
435
436 }
437
438 func ExampleClient_ltrim_end_of_list() {
439 ctx := context.Background()
440
441 rdb := redis.NewClient(&redis.Options{
442 Addr: "localhost:6379",
443 Password: "",
444 DB: 0,
445 })
446
447
448
449 rdb.FlushDB(ctx)
450 rdb.Del(ctx, "bikes:repairs")
451
452
453
454 res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
455
456 if err != nil {
457 panic(err)
458 }
459
460 fmt.Println(res30)
461
462 res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result()
463
464 if err != nil {
465 panic(err)
466 }
467
468 fmt.Println(res31)
469
470 res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
471
472 if err != nil {
473 panic(err)
474 }
475
476 fmt.Println(res32)
477
478
479
480
481
482
483 }
484
485 func ExampleClient_brpop() {
486 ctx := context.Background()
487
488 rdb := redis.NewClient(&redis.Options{
489 Addr: "localhost:6379",
490 Password: "",
491 DB: 0,
492 })
493
494
495
496 rdb.FlushDB(ctx)
497 rdb.Del(ctx, "bikes:repairs")
498
499
500
501 res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result()
502
503 if err != nil {
504 panic(err)
505 }
506
507 fmt.Println(res33)
508
509 res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
510
511 if err != nil {
512 panic(err)
513 }
514
515 fmt.Println(res34)
516
517 res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
518
519 if err != nil {
520 panic(err)
521 }
522
523 fmt.Println(res35)
524
525 res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
526
527 if err != nil {
528 fmt.Println(err)
529 }
530
531 fmt.Println(res36)
532
533
534
535
536
537
538
539
540 }
541
542 func ExampleClient_rule1() {
543 ctx := context.Background()
544
545 rdb := redis.NewClient(&redis.Options{
546 Addr: "localhost:6379",
547 Password: "",
548 DB: 0,
549 })
550
551
552
553 rdb.FlushDB(ctx)
554 rdb.Del(ctx, "new_bikes")
555
556
557
558 res37, err := rdb.Del(ctx, "new_bikes").Result()
559
560 if err != nil {
561 panic(err)
562 }
563
564 fmt.Println(res37)
565
566 res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result()
567
568 if err != nil {
569 panic(err)
570 }
571
572 fmt.Println(res38)
573
574
575
576
577
578 }
579
580 func ExampleClient_rule11() {
581 ctx := context.Background()
582
583 rdb := redis.NewClient(&redis.Options{
584 Addr: "localhost:6379",
585 Password: "",
586 DB: 0,
587 })
588
589
590 rdb.Del(ctx, "new_bikes")
591
592
593
594 res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result()
595
596 if err != nil {
597 panic(err)
598 }
599
600 fmt.Println(res39)
601
602 res40, err := rdb.Type(ctx, "new_bikes").Result()
603
604 if err != nil {
605 panic(err)
606 }
607
608 fmt.Println(res40)
609
610 res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result()
611
612 if err != nil {
613 fmt.Println(err)
614
615 }
616
617 fmt.Println(res41)
618
619
620
621
622
623
624
625 }
626
627 func ExampleClient_rule2() {
628 ctx := context.Background()
629
630 rdb := redis.NewClient(&redis.Options{
631 Addr: "localhost:6379",
632 Password: "",
633 DB: 0,
634 })
635
636
637 rdb.Del(ctx, "bikes:repairs")
638
639
640
641 res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
642
643 if err != nil {
644 panic(err)
645 }
646
647 fmt.Println(res42)
648
649 res43, err := rdb.Exists(ctx, "bikes:repairs").Result()
650
651 if err != nil {
652 panic(err)
653 }
654
655 fmt.Println(res43)
656
657 res44, err := rdb.LPop(ctx, "bikes:repairs").Result()
658
659 if err != nil {
660 panic(err)
661 }
662
663 fmt.Println(res44)
664
665 res45, err := rdb.LPop(ctx, "bikes:repairs").Result()
666
667 if err != nil {
668 panic(err)
669 }
670
671 fmt.Println(res45)
672
673 res46, err := rdb.LPop(ctx, "bikes:repairs").Result()
674
675 if err != nil {
676 panic(err)
677 }
678
679 fmt.Println(res46)
680
681 res47, err := rdb.Exists(ctx, "bikes:repairs").Result()
682
683 if err != nil {
684 panic(err)
685 }
686
687 fmt.Println(res47)
688
689
690
691
692
693
694
695
696
697 }
698
699 func ExampleClient_rule3() {
700 ctx := context.Background()
701
702 rdb := redis.NewClient(&redis.Options{
703 Addr: "localhost:6379",
704 Password: "",
705 DB: 0,
706 })
707
708
709 rdb.Del(ctx, "bikes:repairs")
710
711
712
713 res48, err := rdb.Del(ctx, "bikes:repairs").Result()
714
715 if err != nil {
716 panic(err)
717 }
718
719 fmt.Println(res48)
720
721 res49, err := rdb.LLen(ctx, "bikes:repairs").Result()
722
723 if err != nil {
724 panic(err)
725 }
726
727 fmt.Println(res49)
728
729 res50, err := rdb.LPop(ctx, "bikes:repairs").Result()
730
731 if err != nil {
732 fmt.Println(err)
733 }
734
735 fmt.Println(res50)
736
737
738
739
740
741
742
743 }
744
745 func ExampleClient_ltrim1() {
746 ctx := context.Background()
747
748 rdb := redis.NewClient(&redis.Options{
749 Addr: "localhost:6379",
750 Password: "",
751 DB: 0,
752 })
753
754
755 rdb.Del(ctx, "bikes:repairs")
756
757
758
759 res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
760
761 if err != nil {
762 panic(err)
763 }
764
765 fmt.Println(res51)
766
767 res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
768
769 if err != nil {
770 panic(err)
771 }
772
773 fmt.Println(res52)
774
775 res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
776
777 if err != nil {
778 panic(err)
779 }
780
781 fmt.Println(res53)
782
783
784
785
786
787
788 }
789
View as plain text