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 func ExampleClient_set_get() {
14 ctx := context.Background()
15
16 rdb := redis.NewClient(&redis.Options{
17 Addr: "localhost:6379",
18 Password: "",
19 DB: 0,
20 })
21
22
23
24 rdb.FlushDB(ctx)
25 rdb.Del(ctx, "bike:1")
26
27
28
29 res1, err := rdb.Set(ctx, "bike:1", "Deimos", 0).Result()
30
31 if err != nil {
32 panic(err)
33 }
34
35 fmt.Println(res1)
36
37 res2, err := rdb.Get(ctx, "bike:1").Result()
38
39 if err != nil {
40 panic(err)
41 }
42
43 fmt.Println(res2)
44
45
46
47
48
49 }
50
51 func ExampleClient_setnx_xx() {
52 ctx := context.Background()
53
54 rdb := redis.NewClient(&redis.Options{
55 Addr: "localhost:6379",
56 Password: "",
57 DB: 0,
58 })
59
60
61
62 rdb.FlushDB(ctx)
63 rdb.Set(ctx, "bike:1", "Deimos", 0)
64
65
66
67 res3, err := rdb.SetNX(ctx, "bike:1", "bike", 0).Result()
68
69 if err != nil {
70 panic(err)
71 }
72
73 fmt.Println(res3)
74
75 res4, err := rdb.Get(ctx, "bike:1").Result()
76
77 if err != nil {
78 panic(err)
79 }
80
81 fmt.Println(res4)
82
83 res5, err := rdb.SetXX(ctx, "bike:1", "bike", 0).Result()
84
85 if err != nil {
86 panic(err)
87 }
88
89 fmt.Println(res5)
90
91
92
93
94
95
96 }
97
98 func ExampleClient_mset() {
99 ctx := context.Background()
100
101 rdb := redis.NewClient(&redis.Options{
102 Addr: "localhost:6379",
103 Password: "",
104 DB: 0,
105 })
106
107
108
109 rdb.FlushDB(ctx)
110 rdb.Del(ctx, "bike:1", "bike:2", "bike:3")
111
112
113
114 res6, err := rdb.MSet(ctx, "bike:1", "Deimos", "bike:2", "Ares", "bike:3", "Vanth").Result()
115
116 if err != nil {
117 panic(err)
118 }
119
120 fmt.Println(res6)
121
122 res7, err := rdb.MGet(ctx, "bike:1", "bike:2", "bike:3").Result()
123
124 if err != nil {
125 panic(err)
126 }
127
128 fmt.Println(res7)
129
130
131
132
133
134 }
135
136 func ExampleClient_incr() {
137 ctx := context.Background()
138
139 rdb := redis.NewClient(&redis.Options{
140 Addr: "localhost:6379",
141 Password: "",
142 DB: 0,
143 })
144
145
146
147 rdb.FlushDB(ctx)
148 rdb.Del(ctx, "total_crashes")
149
150
151
152 res8, err := rdb.Set(ctx, "total_crashes", "0", 0).Result()
153
154 if err != nil {
155 panic(err)
156 }
157
158 fmt.Println(res8)
159
160 res9, err := rdb.Incr(ctx, "total_crashes").Result()
161
162 if err != nil {
163 panic(err)
164 }
165
166 fmt.Println(res9)
167
168 res10, err := rdb.IncrBy(ctx, "total_crashes", 10).Result()
169
170 if err != nil {
171 panic(err)
172 }
173
174 fmt.Println(res10)
175
176
177
178
179
180
181 }
182
View as plain text