From 59007b8ef6543aae4b6b3226d167f215c3fe0e96 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Thu, 21 Nov 2024 12:52:03 +0000 Subject: [PATCH] DOC-4560 basic transaction example --- doctests/pipe_trans_example_test.go | 78 +++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 doctests/pipe_trans_example_test.go diff --git a/doctests/pipe_trans_example_test.go b/doctests/pipe_trans_example_test.go new file mode 100644 index 000000000..61f1bcbc7 --- /dev/null +++ b/doctests/pipe_trans_example_test.go @@ -0,0 +1,78 @@ +// EXAMPLE: pipe_trans_tutorial +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + "time" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_transactions() { + // STEP_START basic_trans + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "RateCounter") + // REMOVE_END + + setResult, err := rdb.Set(ctx, "RateCounter", 0, 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(setResult) // >>> OK + + trans := rdb.TxPipeline() + + // The values of `incrResult` and `expResult` are not available + // until the transaction has finished executing. + incrResult := trans.Incr(ctx, "RateCounter") + expResult := trans.Expire(ctx, "RateCounter", time.Second*10) + + cmdsExecuted, err := trans.Exec(ctx) + + if err != nil { + panic(err) + } + + // Values are now available. + fmt.Println(incrResult.Val()) // >>> 1 + fmt.Println(expResult.Val()) // >>> true + + // You can also use the array of command data returned + // by the `Exec()` call. + fmt.Println(len(cmdsExecuted)) // >>> 2 + + fmt.Printf("%v: %v\n", + cmdsExecuted[0].Name(), + cmdsExecuted[0].(*redis.IntCmd).Val(), + ) + // >>> incr: 1 + + fmt.Printf("%v: %v\n", + cmdsExecuted[1].Name(), + cmdsExecuted[1].(*redis.BoolCmd).Val(), + ) + // >>> expire: true + // STEP_END + + // Output: + // OK + // 1 + // true + // 2 + // incr: 1 + // expire: true +}