Redis knowledge point record

affairs

Command Prototype time complexity command description return value
MULTI   It is used to mark the beginning of a transaction, and the commands executed thereafter will be stored in the command queue, and these commands will not be executed atomically until EXEC is executed. always returns OK
EXEC   Executes all commands in the command queue within a transaction, while restoring the state of the current connection to the normal state, that is, the non-transactional state. If the WATCH command is executed in a transaction, the EXEC command can execute all commands in the transaction queue only if the Keys monitored by WATCH have not been modified, otherwise EXEC will abandon all commands in the current transaction. Atomically returns the results of each command in the transaction. If WATCH is used in the transaction, EXEC will return a NULL-multi-bulk reply once the transaction is abandoned.
DISCARD   All commands in the transaction queue are rolled back, and the state of the current connection is restored to the normal state, that is, the non-transactional state. If the WATCH command is used, the command will UNWATCH all Keys. Always returns OK.
WATCHkey [key ...] O(1) Before the execution of the MULTI command, you can specify the keys to be monitored. However, before executing EXEC, if the monitored keys are modified, EXEC will give up executing all commands in the transaction queue. Always returns OK.
UNWATCH O(1) Cancel the specified monitored Keys in the current transaction. If the EXEC or DISCARD command is executed, there is no need to manually execute the command, because after this, all monitored Keys in the transaction will be automatically canceled. Always returns OK.


multi

127.0.0.1:6379>  multi
OK
127.0.0.1:6379> set a 3
QUEUED
127.0.0.1:6379>  lpop a
QUEUED
127.0.0.1:6379>  set a 4
QUEUED
127.0.0.1:6379> get a
QUEUED
127.0.0.1:6379> exec
1) OK
2) (error) WRONGTYPE Operation against a key holding the wrong kind of value
3) OK
4) "4"
127.0.0.1:6379> get a
"4"

rollback discard

127.0.0.1:6379> get a
"4"
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set a 5
QUEUED
127.0.0.1:6379> discard     #
OK
127.0.0.1:6379> get a     
"4"

watch

Client 1

127.0.0.1:6379> get a
"4"
127.0.0.1:6379> multi
OK
127.0.0.1:6379> incr a # At this point, modify the value on another client 2
QUEUED
127.0.0.1:6379> exec
(nil)
127.0.0.1:6379> get a # invalid
"3"

Client 2

127.0.0.1:6379> decr a
(integer) 3

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325570606&siteId=291194637