Redis Transaction Operations

Redis transactions cannot be rolled back, but can guarantee atomicity, and lua scripts can also achieve Redis transaction effects

The following is an example of a transactional operation, the return data is a bit special, the even number is successful, the odd number is executed key , and a result traversal method is written below

$count  = 2;
$result = Redis::transaction(function (\Redis $redis) use ($count) {
    for ($i = 0; $i < $count; $i++) {
        $key = "key:$i";
        $redis->set($key, $i);
        $redis->get($key);
    }
});

/*
$result = array(4) {
    [0]=>
    bool(true)
    [1]=>
    int(0)
    [2]=>
    bool(true)
    [3]=>
    int(1)
}*/

foreach ($result as $index => $value) {
    if ($index % 2 == 0) {
        // is exec ok ?
       if ($value) {
            // todo... 
       }
    }
}
/docs/2.x/en/redis/transaction.html
progress-bar