[][src]Function redis::transaction

pub fn transaction<C: ConnectionLike, K: ToRedisArgs, T, F: FnMut(&mut C, &mut Pipeline) -> RedisResult<Option<T>>>(
    con: &mut C,
    keys: &[K],
    func: F
) -> RedisResult<T>

This function simplifies transaction management slightly. What it does is automatically watching keys and then going into a transaction loop util it succeeds. Once it goes through the results are returned.

To use the transaction two pieces of information are needed: a list of all the keys that need to be watched for modifications and a closure with the code that should be execute in the context of the transaction. The closure is invoked with a fresh pipeline in atomic mode. To use the transaction the function needs to return the result from querying the pipeline with the connection.

The end result of the transaction is then available as the return value from the function call.


use redis::Commands;
let key = "the_key";
let (new_val,) : (isize,) = redis::transaction(&mut con, &[key], |con, pipe| {
    let old_val : isize = con.get(key)?;
        .set(key, old_val + 1).ignore()
println!("The incremented number is: {}", new_val);