exec

UK[ɪgˈzek] 美[ɪɡ'zek]

abbr.execute execution;executive execution

plural: execs

redis EXEC command syntax

Function: Execute all commands in the transaction block.

Syntax: EXEC

Description: If a certain (or some) key is under the monitoring of the WATCH command, and the transaction block If there are commands related to this (or these) key, then the EXEC command will only be executed and take effect when this (or these) key has not been modified by other commands, otherwise the transaction will be aborted.

Available versions: >= 1.2.0

Time complexity: The sum of the time complexity of all commands within the transaction block.

Return: The return value of all commands within the transaction block, arranged in the order of command execution.

When the operation is interrupted, the empty value nil is returned.

redis EXEC command example

# 事务被成功执行
redis> MULTI
OK
redis> INCR user_id
QUEUED
redis> INCR user_id
QUEUED
redis> INCR user_id
QUEUED
redis> PING
QUEUED
redis> EXEC
1) (integer) 1
2) (integer) 2
3) (integer) 3
4) PONG
# 监视 key ,且事务成功执行
redis> WATCH lock lock_times
OK
redis> MULTI
OK
redis> SET lock "huangz"
QUEUED
redis> INCR lock_times
QUEUED
redis> EXEC
1) OK
2) (integer) 1
# 监视 key ,且事务被打断
redis> WATCH lock lock_times
OK
redis> MULTI
OK
redis> SET lock "joe"        # 就在这时,另一个客户端修改了 lock_times 的值
QUEUED
redis> INCR lock_times
QUEUED
redis> EXEC                  # 因为 lock_times 被修改, joe 的事务执行失败
(nil)