MCPcopy
hub / github.com/redis/go-redis / ExampleClient_transactions

Function ExampleClient_transactions

doctests/pipe_trans_example_test.go:14–182  ·  view source on GitHub ↗

HIDE_END

()

Source from the content-addressed store, hash-verified

12// HIDE_END
13
14func 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 // make sure we are working with fresh database
24 rdb.FlushDB(ctx)
25 for i := 0; i < 5; i++ {
26 rdb.Del(ctx, fmt.Sprintf("seat:%d", i))
27 }
28
29 rdb.Del(ctx, "counter:1", "counter:2", "counter:3", "shellpath")
30 // REMOVE_END
31
32 // STEP_START basic_pipe
33 pipe := rdb.Pipeline()
34
35 for i := 0; i < 5; i++ {
36 pipe.Set(ctx, fmt.Sprintf("seat:%v", i), fmt.Sprintf("#%v", i), 0)
37 }
38
39 cmds, err := pipe.Exec(ctx)
40
41 if err != nil {
42 panic(err)
43 }
44
45 for _, c := range cmds {
46 fmt.Printf("%v;", c.(*redis.StatusCmd).Val())
47 }
48
49 fmt.Println("")
50 // >>> OK;OK;OK;OK;OK;
51
52 pipe = rdb.Pipeline()
53
54 get0Result := pipe.Get(ctx, "seat:0")
55 get3Result := pipe.Get(ctx, "seat:3")
56 get4Result := pipe.Get(ctx, "seat:4")
57
58 cmds, err = pipe.Exec(ctx)
59
60 // The results are available only after the pipeline
61 // has finished executing.
62 fmt.Println(get0Result.Val()) // >>> #0
63 fmt.Println(get3Result.Val()) // >>> #3
64 fmt.Println(get4Result.Val()) // >>> #4
65 // STEP_END
66
67 // STEP_START basic_pipe_pipelined
68 var pd0Result *redis.StatusCmd
69 var pd3Result *redis.StatusCmd
70 var pd4Result *redis.StatusCmd
71

Callers

nothing calls this directly

Calls 15

PipelineMethod · 0.95
PipelinedMethod · 0.95
ValMethod · 0.95
TxPipelineMethod · 0.95
TxPipelinedMethod · 0.95
ValMethod · 0.95
WatchMethod · 0.95
FlushDBMethod · 0.65
DelMethod · 0.65
SetMethod · 0.65
ExecMethod · 0.65
PrintfMethod · 0.65

Tested by

no test coverage detected