Detailed explanation of the use of Redis transactions

Redis transactions can execute multiple commands at once, with the following two important guarantees:

The transaction is a separate isolated operation: all commands in the transaction are serialized and executed sequentially. During the execution of the transaction, it will not be interrupted by command requests sent by other clients.
A transaction is an atomic operation: either all commands in the transaction are executed, or none of them are executed.
A transaction will go through the following three stages from inception to execution:

  • Start the business.
  • Order to join the team.
  • Perform affairs.
    Example The
    following is an example of a transaction. It first starts a transaction with MULTI, then enqueues multiple commands into the transaction, and finally triggers the transaction by the EXEC command, and executes all the commands in the transaction together:
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set username lihaitao
QUEUED
127.0.0.1:6379> get username
QUEUED
127.0.0.1:6379> sadd fruit "apple" "banana" "orange"
QUEUED
127.0.0.1:6379> smembers fruit
QUEUED
127.0.0.1:6379> exec
1) OK
2) "lihaitao"
3) (integer) 3
4) 1) "apple"
   2) "banana"
   3) "orange"
127.0.0.1:6379>

Redis transaction commands The
following table lists the redis transaction related commands:
1 DISCARD cancels the transaction and abandons the execution of all commands in the transaction block.

127.0.0.1:6379> multi
OK
127.0.0.1:6379> set username lihaitao
QUEUED
127.0.0.1:6379> discard
OK
127.0.0.1:6379> exec
(error) ERR EXEC without MULTI
127.0.0.1:6379>

2 EXEC executes all commands in the transaction block.
3 MULTI marks the beginning of a transaction block.
4 UNWATCH cancels the monitoring of all keys by the WATCH command.
5 WATCH key [key …] Monitor one (or more) key. If this (or these) keys are changed by other commands before the transaction is executed, the transaction will be interrupted.
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_38019299/article/details/103457710