How to use transactions in the Redis basic tutorial

  • 2020-05-17 06:55:24
  • OfStack

How to use transactions in the Redis basic tutorial

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

A transaction is a single isolated operation: all commands in the transaction are serialized and executed sequentially. During the execution of a transaction, it will not be interrupted by command requests from other clients. A transaction is an atomic operation: either all the commands in the transaction are executed or none is executed.

A transaction goes through the following three phases from inception to execution:

Start the transaction. Order in. Execute the transaction.

The instance

The following is an example of a transaction that starts with MULTI, queues multiple commands into the transaction, and finally triggers the transaction by EXEC, 1 and executes all the commands in the transaction:


redis 127.0.0.1:6379> MULTI
OK

redis 127.0.0.1:6379> SET book-name "Mastering C++ in 21 days"
QUEUED

redis 127.0.0.1:6379> GET book-name
QUEUED

redis 127.0.0.1:6379> SADD tag "C++" "Programming" "Mastering Series"
QUEUED

redis 127.0.0.1:6379> SMEMBERS tag
QUEUED

redis 127.0.0.1:6379> EXEC
1) OK
2) "Mastering C++ in 21 days"
3) (integer) 3
4) 1) "Mastering Series"
  2) "C++"
  3) "Programming"

Redis transaction command

The following table lists the relevant commands for redis transactions:


1  DISCARD 
   Cancels a transaction, aborting the execution of all commands within the transaction block. 

2  EXEC 
   Execute all commands within the transaction block. 

3  MULTI 
   tag 1 The start of a transaction block. 

4  UNWATCH 
   cancel  WATCH  Command to all  key  The monitoring. 

5  WATCH key [key ...] 
   monitoring 1 a ( Or more ) key  If this is before the transaction is executed ( Or these ) key  If it is changed by another command, the transaction will be interrupted. 

Above is the detailed explanation of the business command, if you have any questions, please leave a message or to this site community exchange discussion, thank you for reading, hope to help you, thank you for your support to this site!


Related articles: