1.Transaction command
command | Effect | Return value |
---|---|---|
watch | Monitor one or more key s | Always OK |
multi | Declare the start of the transaction, and subsequent commands will be queued for exec execution in order | Always OK |
exec | Execute the command after multi in sequence. If the value of the key monitored by the watch command before multi changes, the execution will fail | When the execution succeeds, the returned array contains the execution results of each command. When the execution fails, the native command returns null, and the PHP redis extension method returns false |
discard | Cancel transaction | Always OK |
unwatch | Cancel the watch monitoring. If exec or discard is executed after the watch monitoring, the monitoring will be cancelled automatically, and there is no need to wait again | Always OK |
2. Execution
<?php /** * Created by PhpStorm. * User: jmsite.cn * Date: 2019/1/24 * Time: 21:03 */ try { $redis = new Redis(); $redis->connect('192.168.75.132', 6379); //Open transaction $redis->multi(); $redis->setex('keyTest', 60, 1); $redis->get('keyTest'); $redis->incr('keyTest'); $redis->get('keyTest'); //Executive affairs $ret = $redis->exec(); print_r($ret); } catch (Exception $e){ echo $e->getMessage(); }
Execution result: the execution result of each command is returned as an array
Array ( [0] => 1 [1] => 1 [2] => 2 [3] => 2 )
3. Cancellation
<?php /** * Created by PhpStorm. * User: jmsite.cn * Date: 2019/1/24 * Time: 21:03 */ try { $redis = new Redis(); $redis->connect('192.168.75.132', 6379); //First set the cache keyTest to 1 $redis->setex('keyTest', 60, 1); //Open transaction $redis->multi(); $redis->setex('keyTest', 60, 10); $redis->get('keyTest'); $redis->incr('keyTest'); $redis->get('keyTest'); //Cancel transaction $redis->discard(); $ret = $redis->get('keyTest'); var_dump($ret); //View keyTest } catch (Exception $e){ echo $e->getMessage(); }
Execution result: keyTest is still the original value and has not changed
string(1) "1"
4. Monitor key and execute transaction
<?php /** * Created by PhpStorm. * User: jmsite.cn * Date: 2019/1/24 * Time: 21:03 */ try { $redis = new Redis(); $redis->connect('192.168.75.132', 6379); //First set the cache keyTest to 1 $redis->setex('keyTest', 60, 1); //Monitor keyTest $redis->watch(array('keyTest')); //Suppose that after monitoring and before transaction execution, the keyTest is modified by other users who operate redis concurrently $redis->setex('keyTest', 60, 10); //Open transaction $redis->multi(); $redis->incr('keyTest'); //Executive affairs $ret = $redis->exec(); var_dump($ret); $ret = $redis->get('keyTest'); var_dump($ret); //View keyTest } catch (Exception $e){ echo $e->getMessage(); }
Execution result: exec returns false
bool(false) string(2) "10"
The key of transaction command is that while redis provides us with high performance, although the command is atomic, it can't generate context relation with our php. At this time, transaction command comes into use
Original address: https://www.jmsite.cn/blog-601.html