当前位置:网站首页>Redis: redis transactions

Redis: redis transactions

2022-07-04 23:06:00 dengfengling999

 

 

 

 

Redis The transaction allows a set of commands to be executed in a single step , And it can ensure that all commands in a transaction are serialized , Then execute in order ; In a Redis Transaction ,Redis Or execute all the commands in it , Or nothing . namely Redis Transactions should be able to ensure serialization and atomicity .

Redis Transactions of can only maintain partial atomicity , Cannot remain completely atomic , Data may be inconsistent  

Redis Simplify things , Why simplify ? It should improve its access efficiency , It does not consider the portability of so many transactions  

  1. Redis Common commands for transactions :
    1. multi

         grammar :multi

function : Used to mark the beginning of a transaction block .Redis The subsequent commands will be put into the queue one by one , And then you can use it EXEC The command executes the command sequence atomically .

Return value : Open successfully and return OK

  for example :

 

2.exec

grammar :exec

function : Execute all previously queued commands in a transaction , Then restore the normal connection state .

If an error is reported in the process of pushing the command into the queue , Then the commands in the whole queue will not be executed , Error in execution result ;

If it's normal in the process of pressing the queue , An error is reported for a command in the execution queue , It will only affect the execution result of this command , Other commands operate normally ;

When using WATCH On command , Only if the monitored key has not been modified ,EXEC Command will execute the command in the transaction ; And once implemented exec command , All the previously added watch All monitoring is cancelled .

Return value : The return value of this command is an array , Each of these elements is the return value of each command in the atomized transaction . When using WATCH On command , If the transaction execution aborts , that EXEC The command will return a Null value .

inc k1 : For numerical type k1 Add one , If it is not numerical, report an error , It will not really execute when pressing the opposite column , So I don't know k1 Is it numerical

 

  for example : Press in the right column to report errors , Do not carry out , Keep atomicity

Press the right column without reporting an error : 

 

 

3.discard

grammar :discard

function : Clear all previously queued commands in a transaction , And end the transaction .

If used WATCH command , that DISCARD The command will cancel the monitoring of all keys currently connected to the monitoring .

Return value : Clear successfully , return OK.

for example :

 

 

4.watch

 

grammar :watch key [key …]

function : When a transaction needs to be executed conditionally , Use this command to set the given key to be monitored . If monitored key When the value is modified outside this transaction , Then the instructions of the firm will not be executed .Watch The command is equivalent to an optimistic lock in a relational database .

Return value : Monitoring successful , return OK.

 decrby: Minus the specified value    incrby: Add the specified value

  for example :

Establish another connection to make changes vcersion 

 

 

 

 

5.unwatch

grammar :unwatch

function : Clear all previously monitored keys for a transaction .

           If in watch After the command, you call EXEC or DISCARD command , So you don't have to call it manually UNWATCH command .

Return value : Clear successfully , return OK.

  for example : Clear all monitored

  change version:

 

Redis Business summary :

1、 Separate isolation operation : All commands in the transaction are serialized 、 Execute in sequence . The transaction is in progress , Will not be interrupted by command requests from other clients , Unless used watch Commands monitor certain keys .

2、 The atomicity of transactions is not guaranteed :redis If a command fails in the same transaction , Subsequent orders may still be executed ,redis The transaction was not rolled back .Redis Function simplification has been carried out inside the system , This ensures faster running speed , because Redis The ability to roll back transactions is not required .

原网站

版权声明
本文为[dengfengling999]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042243410896.html