[redis]Redis Transaction

时间:2020-12-17 16:16:12

https://github.com/phpredis/phpredis#transactions

Transactions

  1. multi, exec, discard - Enter and exit transactional mode
  2. watch, unwatch - Watches a key for modifications by another client.

multi, exec, discard.


Description: Enter and exit transactional mode.

Parameters

(optional) Redis::MULTI or Redis::PIPELINE. Defaults to Redis::MULTI. A Redis::MULTI block of commands runs as a single transaction; a Redis::PIPELINE block is simply transmitted faster to the server, but without any guarantee of atomicity. discard cancels a transaction.

Return value

multi() returns the Redis instance and enters multi-mode. Once in multi-mode, all subsequent method calls return the same object until exec() is called.

Example
$ret = $redis->multi()
->set('key1', 'val1')
->get('key1')
->set('key2', 'val2')
->get('key2')
->exec(); /*
$ret == array(
0 => TRUE,
1 => 'val1',
2 => TRUE,
3 => 'val2');
*/