Redis Business
brief introduction
Redis A transaction in is a queue of command execution . The business is the same as the order Redis Minimum execution unit , All commands in a transaction are executed at one time , Either not . When performing , One time in sequence , The intermediate process cannot be disturbed
The basic operation of a transaction
Open transaction multi
Set the start of the transaction , Subsequent instructions are added to the queue , Perform transactions
exec
The end of the business , Execute all transactions and return results .
Cancel the business discard
There was a problem in the process of defining the transaction , Cancel the transaction immediately
The workflow of a transaction
The operation process
There is no transaction open , Unable to get current settings data
The other customer service terminal is set with set name yu
After opening the transaction , All steps are consistent
Transaction setup error , Cancel the current transaction
Business considerations
1. If there is a syntax error in the input instruction , All commands in the overall transaction will not be executed
2. If there is no syntax error in the input instruction , Command execution error , The right order will be executed , Running the wrong command will not be executed . If in actual coding , Data that has been executed will not be rolled back , You need to manually roll back .
lock
Application scenarios :( Control batch task execution ) It is possible for multiple clients to operate on the same set of data at the same time , And once the data is modified by the operation , Will not apply to continue operation , Lock the data to be manipulated before the operation , As soon as it changes , Terminate the current operation .
Add monitoring lock
watch
, Before the end of the execution transaction , If key The data inside has changed , Terminate transaction execution .
unwatch
Cancel for all key Surveillance
Practice
I'm doing exec Before the end of the business , In another customer service end, it has been modified aa Value , It is found that the value obtained at the end of the transaction is empty .
After the transaction is opened , Can't monitor key, After canceling the monitoring , Open transaction , After adding data , Modify the data on another client aa, And then finish the business , Found that the data has not been changed .