Skip to content

Commit 38f7ba7

Browse files
authored
Merge branch 'master' into redissearch
2 parents 77a3aed + efe0f65 commit 38f7ba7

File tree

4 files changed

+306
-4
lines changed

4 files changed

+306
-4
lines changed

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ services:
99
- REDIS_CLUSTER=no
1010
- PORT=6379
1111
- TLS_PORT=6666
12-
command: --loadmodule /usr/local/lib/redis/modules/redisbloom.so --loadmodule /usr/local/lib/redis/modules/redisearch.so --loadmodule /usr/local/lib/redis/modules/redistimeseries.so --loadmodule /usr/local/lib/redis/modules/rejson.so
12+
1313
ports:
1414
- 6379:6379
1515
- 6380:6379

doctests/cmds_hash_test.go

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package example_commands_test
55
import (
66
"context"
77
"fmt"
8+
"sort"
89

910
"github.com/redis/go-redis/v9"
1011
)
@@ -74,8 +75,20 @@ func ExampleClient_hset() {
7475
panic(err)
7576
}
7677

77-
fmt.Println(res6)
78-
// >>> map[field1:Hello field2:Hi field3:World]
78+
keys := make([]string, 0, len(res6))
79+
80+
for key, _ := range res6 {
81+
keys = append(keys, key)
82+
}
83+
84+
sort.Strings(keys)
85+
86+
for _, key := range keys {
87+
fmt.Printf("Key: %v, value: %v\n", key, res6[key])
88+
}
89+
// >>> Key: field1, value: Hello
90+
// >>> Key: field2, value: Hi
91+
// >>> Key: field3, value: World
7992
// STEP_END
8093

8194
// Output:
@@ -84,7 +97,9 @@ func ExampleClient_hset() {
8497
// 2
8598
// Hi
8699
// World
87-
// map[field1:Hello field2:Hi field3:World]
100+
// Key: field1, value: Hello
101+
// Key: field2, value: Hi
102+
// Key: field3, value: World
88103
}
89104

90105
func ExampleClient_hget() {
@@ -131,3 +146,96 @@ func ExampleClient_hget() {
131146
// foo
132147
// redis: nil
133148
}
149+
150+
func ExampleClient_hgetall() {
151+
ctx := context.Background()
152+
153+
rdb := redis.NewClient(&redis.Options{
154+
Addr: "localhost:6379",
155+
Password: "", // no password
156+
DB: 0, // use default DB
157+
})
158+
159+
// REMOVE_START
160+
rdb.Del(ctx, "myhash")
161+
// REMOVE_END
162+
163+
// STEP_START hgetall
164+
hGetAllResult1, err := rdb.HSet(ctx, "myhash",
165+
"field1", "Hello",
166+
"field2", "World",
167+
).Result()
168+
169+
if err != nil {
170+
panic(err)
171+
}
172+
173+
fmt.Println(hGetAllResult1) // >>> 2
174+
175+
hGetAllResult2, err := rdb.HGetAll(ctx, "myhash").Result()
176+
177+
if err != nil {
178+
panic(err)
179+
}
180+
181+
keys := make([]string, 0, len(hGetAllResult2))
182+
183+
for key, _ := range hGetAllResult2 {
184+
keys = append(keys, key)
185+
}
186+
187+
sort.Strings(keys)
188+
189+
for _, key := range keys {
190+
fmt.Printf("Key: %v, value: %v\n", key, hGetAllResult2[key])
191+
}
192+
// >>> Key: field1, value: Hello
193+
// >>> Key: field2, value: World
194+
// STEP_END
195+
196+
// Output:
197+
// 2
198+
// Key: field1, value: Hello
199+
// Key: field2, value: World
200+
}
201+
202+
func ExampleClient_hvals() {
203+
ctx := context.Background()
204+
205+
rdb := redis.NewClient(&redis.Options{
206+
Addr: "localhost:6379",
207+
Password: "", // no password docs
208+
DB: 0, // use default DB
209+
})
210+
211+
// REMOVE_START
212+
rdb.Del(ctx, "myhash")
213+
// REMOVE_END
214+
215+
// STEP_START hvals
216+
hValsResult1, err := rdb.HSet(ctx, "myhash",
217+
"field1", "Hello",
218+
"field2", "World",
219+
).Result()
220+
221+
if err != nil {
222+
panic(err)
223+
}
224+
225+
fmt.Println(hValsResult1) // >>> 2
226+
227+
hValsResult2, err := rdb.HVals(ctx, "myhash").Result()
228+
229+
if err != nil {
230+
panic(err)
231+
}
232+
233+
sort.Strings(hValsResult2)
234+
235+
fmt.Println(hValsResult2) // >>> [Hello World]
236+
// STEP_END
237+
238+
// Output:
239+
// 2
240+
// [Hello World]
241+
}

doctests/pipe_trans_example_test.go

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
// EXAMPLE: pipe_trans_tutorial
2+
// HIDE_START
3+
package example_commands_test
4+
5+
import (
6+
"context"
7+
"fmt"
8+
9+
"github.com/redis/go-redis/v9"
10+
)
11+
12+
// HIDE_END
13+
14+
func ExampleClient_transactions() {
15+
ctx := context.Background()
16+
17+
rdb := redis.NewClient(&redis.Options{
18+
Addr: "localhost:6379",
19+
Password: "", // no password docs
20+
DB: 0, // use default DB
21+
})
22+
// REMOVE_START
23+
for i := 0; i < 5; i++ {
24+
rdb.Del(ctx, fmt.Sprintf("seat:%d", i))
25+
}
26+
27+
rdb.Del(ctx, "counter:1", "counter:2", "counter:3", "shellpath")
28+
// REMOVE_END
29+
30+
// STEP_START basic_pipe
31+
pipe := rdb.Pipeline()
32+
33+
for i := 0; i < 5; i++ {
34+
pipe.Set(ctx, fmt.Sprintf("seat:%v", i), fmt.Sprintf("#%v", i), 0)
35+
}
36+
37+
cmds, err := pipe.Exec(ctx)
38+
39+
if err != nil {
40+
panic(err)
41+
}
42+
43+
for _, c := range cmds {
44+
fmt.Printf("%v;", c.(*redis.StatusCmd).Val())
45+
}
46+
47+
fmt.Println("")
48+
// >>> OK;OK;OK;OK;OK;
49+
50+
pipe = rdb.Pipeline()
51+
52+
get0Result := pipe.Get(ctx, "seat:0")
53+
get3Result := pipe.Get(ctx, "seat:3")
54+
get4Result := pipe.Get(ctx, "seat:4")
55+
56+
cmds, err = pipe.Exec(ctx)
57+
58+
// The results are available only after the pipeline
59+
// has finished executing.
60+
fmt.Println(get0Result.Val()) // >>> #0
61+
fmt.Println(get3Result.Val()) // >>> #3
62+
fmt.Println(get4Result.Val()) // >>> #4
63+
// STEP_END
64+
65+
// STEP_START basic_pipe_pipelined
66+
var pd0Result *redis.StatusCmd
67+
var pd3Result *redis.StatusCmd
68+
var pd4Result *redis.StatusCmd
69+
70+
cmds, err = rdb.Pipelined(ctx, func(pipe redis.Pipeliner) error {
71+
pd0Result = (*redis.StatusCmd)(pipe.Get(ctx, "seat:0"))
72+
pd3Result = (*redis.StatusCmd)(pipe.Get(ctx, "seat:3"))
73+
pd4Result = (*redis.StatusCmd)(pipe.Get(ctx, "seat:4"))
74+
return nil
75+
})
76+
77+
if err != nil {
78+
panic(err)
79+
}
80+
81+
// The results are available only after the pipeline
82+
// has finished executing.
83+
fmt.Println(pd0Result.Val()) // >>> #0
84+
fmt.Println(pd3Result.Val()) // >>> #3
85+
fmt.Println(pd4Result.Val()) // >>> #4
86+
// STEP_END
87+
88+
// STEP_START basic_trans
89+
trans := rdb.TxPipeline()
90+
91+
trans.IncrBy(ctx, "counter:1", 1)
92+
trans.IncrBy(ctx, "counter:2", 2)
93+
trans.IncrBy(ctx, "counter:3", 3)
94+
95+
cmds, err = trans.Exec(ctx)
96+
97+
for _, c := range cmds {
98+
fmt.Println(c.(*redis.IntCmd).Val())
99+
}
100+
// >>> 1
101+
// >>> 2
102+
// >>> 3
103+
// STEP_END
104+
105+
// STEP_START basic_trans_txpipelined
106+
var tx1Result *redis.IntCmd
107+
var tx2Result *redis.IntCmd
108+
var tx3Result *redis.IntCmd
109+
110+
cmds, err = rdb.TxPipelined(ctx, func(trans redis.Pipeliner) error {
111+
tx1Result = trans.IncrBy(ctx, "counter:1", 1)
112+
tx2Result = trans.IncrBy(ctx, "counter:2", 2)
113+
tx3Result = trans.IncrBy(ctx, "counter:3", 3)
114+
return nil
115+
})
116+
117+
if err != nil {
118+
panic(err)
119+
}
120+
121+
fmt.Println(tx1Result.Val()) // >>> 2
122+
fmt.Println(tx2Result.Val()) // >>> 4
123+
fmt.Println(tx3Result.Val()) // >>> 6
124+
// STEP_END
125+
126+
// STEP_START trans_watch
127+
// Set initial value of `shellpath`.
128+
rdb.Set(ctx, "shellpath", "/usr/syscmds/", 0)
129+
130+
const maxRetries = 1000
131+
132+
// Retry if the key has been changed.
133+
for i := 0; i < maxRetries; i++ {
134+
err := rdb.Watch(ctx,
135+
func(tx *redis.Tx) error {
136+
currentPath, err := rdb.Get(ctx, "shellpath").Result()
137+
newPath := currentPath + ":/usr/mycmds/"
138+
139+
_, err = tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
140+
pipe.Set(ctx, "shellpath", newPath, 0)
141+
return nil
142+
})
143+
144+
return err
145+
},
146+
"shellpath",
147+
)
148+
149+
if err == nil {
150+
// Success.
151+
break
152+
} else if err == redis.TxFailedErr {
153+
// Optimistic lock lost. Retry the transaction.
154+
continue
155+
} else {
156+
// Panic for any other error.
157+
panic(err)
158+
}
159+
}
160+
161+
fmt.Println(rdb.Get(ctx, "shellpath").Val())
162+
// >>> /usr/syscmds/:/usr/mycmds/
163+
// STEP_END
164+
165+
// Output:
166+
// OK;OK;OK;OK;OK;
167+
// #0
168+
// #3
169+
// #4
170+
// #0
171+
// #3
172+
// #4
173+
// 1
174+
// 2
175+
// 3
176+
// 2
177+
// 4
178+
// 6
179+
// /usr/syscmds/:/usr/mycmds/
180+
}

doctests/sets_example_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package example_commands_test
55
import (
66
"context"
77
"fmt"
8+
"sort"
89

910
"github.com/redis/go-redis/v9"
1011
)
@@ -215,6 +216,9 @@ func ExampleClient_saddsmembers() {
215216
panic(err)
216217
}
217218

219+
// Sort the strings in the slice to make sure the output is lexicographical
220+
sort.Strings(res10)
221+
218222
fmt.Println(res10) // >>> [bike:1 bike:2 bike:3]
219223
// STEP_END
220224

@@ -294,6 +298,10 @@ func ExampleClient_sdiff() {
294298
panic(err)
295299
}
296300

301+
302+
// Sort the strings in the slice to make sure the output is lexicographical
303+
sort.Strings(res13)
304+
297305
fmt.Println(res13) // >>> [bike:2 bike:3]
298306
// STEP_END
299307

@@ -349,6 +357,9 @@ func ExampleClient_multisets() {
349357
panic(err)
350358
}
351359

360+
// Sort the strings in the slice to make sure the output is lexicographical
361+
sort.Strings(res15)
362+
352363
fmt.Println(res15) // >>> [bike:1 bike:2 bike:3 bike:4]
353364

354365
res16, err := rdb.SDiff(ctx, "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy").Result()
@@ -373,6 +384,9 @@ func ExampleClient_multisets() {
373384
panic(err)
374385
}
375386

387+
// Sort the strings in the slice to make sure the output is lexicographical
388+
sort.Strings(res18)
389+
376390
fmt.Println(res18) // >>> [bike:2 bike:3]
377391
// STEP_END
378392

0 commit comments

Comments
 (0)