7. Redis transactions and locks

1. Affairs

1.1 Start a transaction

multi

Set the opening position of the transaction. After this command is executed, all subsequent commands will be added to the transaction

1.2 Executing Transactions

exec

Set the end position of the transaction and execute the transaction at the same time. Appears in pairs with multi and is used in pairs.
Note: The command to join the transaction temporarily enters the task queue and is not executed immediately. Only the exec command is executed to start execution

1.3 Cancel transaction

discard

Terminate the definition of the current transaction, which occurs after multi and before exec

2. lock

2.1 lock

Add a monitoring lock to the key. If the key changes before exec is executed, the transaction execution will be terminated

watch key1 [key2……]

Unwatch all keys

unwatch

2.2 Distributed lock

Use setnx to set a public lock

setnx lock-key value

If number 1 wants to go to the toilet and locks the toilet, then number 2 cannot go to the toilet

Use expire to add a time limit to the lock key, do not release it when the time comes, and give up the lock

expire lock-key second
pexpire lock-key milliseconds

Guess you like

Origin blog.csdn.net/qq_44954571/article/details/122969774