Redis Exec Command
The Redis Exec command is used to execute all commands within a transaction block.
Syntax
The basic syntax for the redis Exec command is as follows:
redis 127.0.0.1:6379> EXEC
Available Versions
= 1.2.0
Return Value
The return values of all commands within the transaction block, arranged in the order they were executed. If the operation is interrupted, it returns a nil value.
Examples
# 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
# Watching keys and transaction executed successfully
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
# Watching keys and transaction 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" # At this moment, another client modifies the value of lock_times
QUEUED
redis 127.0.0.1:6379> INCR lock_times
QUEUED
redis 127.0.0.1:6379> EXEC # Transaction fails because lock_times was modified
(nil)