How to create a Redis Transaction in Go using the go-redis / redis package?

I want to execute several redis commmand with a transaction using MULTIand EXEC, so I can DISCARD, if something is wrong.

I am looking for an example of how to do redis transactions using go-redis / redis and have not found anything.

And I will also review the documentation here , and I have not received anything related to how to make a redis transaction like this, for example , using this package. Or maybe I missed something from the documentation, because yes, you know that godoc explains each function in the package, mainly using one liner.

Despite the fact that I found some example for performing a redis transaction using another Go Redis library, I will not change my programs to use another library, since the efforts will be much more for transferring the entire application using another library.

Can someone help me do this using go-redis / redis package?

early.

+4
source share
1 answer

You get the value Txfor the transaction when usingClient.Watch

err := client.Watch(func(tx *redis.Tx) error {
    n, err := tx.Get(key).Int64()
    if err != nil && err != redis.Nil {
        return err
    }

    _, err = tx.Pipelined(func(pipe *redis.Pipeline) error {
        pipe.Set(key, strconv.FormatInt(n+1, 10), 0)
        return nil
    })
    return err
}, key)
+2
source

Source: https://habr.com/ru/post/1663003/


All Articles