Redis Exec command
The Redis Exec command is used to execute all commands in the transaction block.
Syntax
The basic syntax of the redis Exec command is as follows:
redis 127.0.0.1:6379> Exec
Available Version
= 1.2.0
Return Value
The return values of all commands in the transaction block, ordered by execution sequence. Returns the null value nil when the operation is interrupted.
Example
# Transaction executed successfully
redis 127.0.0.1:6379> MULTI
OK
redis 127.0.0.1:6379> INCR user_id
QUEUED
redis 127.0.0.1:6379> INCR user_id
QUEUED
redis 127.0.0.1:6379> INCR user_id
QUEUED
redis 127.0.0.1:6379> PING
QUEUED
redis 127.0.0.1:6379> EXEC
1) (integer) 1
2) (integer) 2
3) (integer) 3
4) PONG
# Monitor key, and transaction successfully executed
redis 127.0.0.1:6379> WATCH lock lock_times
OK
redis 127.0.0.1:6379> MULTI
OK
redis 127.0.0.1:6379> SET lock "huangz"
QUEUED
redis 127.0.0.1:6379> INCR lock_times
QUEUED
redis 127.0.0.1:6379> EXEC
1) OK
2) (integer) 1
# Monitor key, and transaction is interrupted
redis 127.0.0.1:6379> WATCH lock lock_times
OK
redis 127.0.0.1:6379> MULTI
OK
redis 127.0.0.1:6379> SET lock "joe" # 就在这时,另一个客户端修改了 lock_times 的值
QUEUED
redis 127.0.0.1:6379> INCR lock_times
QUEUED
redis 127.0.0.1:6379> EXEC # 因为 lock_times 被修改, joe 的事务执行失败
(nil)