9951 explained code solutions for 126 technologies


php-redisRedis transactions using multi and exec


$redis->multi()
->set('a', 1)
->get('a')
->set('b', 2)
->get('b')
->exec();ctrl + c
$redis

Redis object after connection

multi

execute set of commands within single transaction

exec

commit transaction and return list of results of each executed command


Usage example

<?php

$r = new Redis(); 
$r->connect('127.0.0.1', 6379);

$res = $r->multi()
       ->set('a', 1)
       ->get('a')
       ->set('b', 2)
       ->get('b')
       ->exec();

var_dump($res);
output
array(4) {
  [0]=>
  bool(true)
  [1]=>
  string(1) "1"
  [2]=>
  bool(true)
  [3]=>
  string(1) "2"
}