Redis Research (10)-Redis transactions and survival time, redis transactions
I. Transaction Overview
Redis transactions are a collection of commands. A transaction is the smallest Execution Unit of Redis like a command, either executed or not executed.
The principle of a transaction is to first send the commands of a transaction to Redis, and then let Redis execute these commands in sequence.
Redis does not immediately execute the sadd operation in the transaction, but returns QUEUED, indicating that the two commands have entered the transaction queue waiting for execution.
The EXEC command tells Redis to execute all the commands in the transaction queue waiting for execution in the order of sending.
The EXEC return value is a list composed of the return values of these commands. The returned values are in the same order as the commands.
Redis ensures that all commands in a transaction are either executed or not executed. If the client is disconnected before the EXEC command is sent, Redis clears the transaction queue, and all commands in the transaction are not executed. Once the client sends the EXEC command, all the commands will be executed, even if the client is disconnected afterwards, because Redis has recorded all the commands to be executed.
Redis transactions can also ensure that commands in one transaction are executed in sequence without being inserted by other commands.
Ii. Error Handling
An error occurred while executing a command in the transaction. What should I do?
(1) syntax error: the command does not exist or the number of command parameters is incorrect.
Three commands: one correct and two errors. If one command has a syntax error, Redis will directly return an error after executing the EXEC command, and the command with the correct syntax will not be executed.
(2) Running errors refer to errors that occur during command execution. For example, you can use Hash commands to operate on keys of the set type. Redis cannot be found before execution, so such commands will be accepted and executed in the transaction,
If a running error occurs for a command, other commands in the transaction will continue to be executed.
Although an error occurs in sadd key 2, set key 3 is still executed.
Cancel a transaction:
Redis transactions do not have the rollback function.
Redis does not support the rollback function, which makes Redis simple and fast in transactions. Syntax errors and running errors can be found and solved during development.
Iii. watch command
The execution result of each command in the transaction is returned in the last period, and the result of the previous command cannot be used as the parameter of the next command.
For example, get value, + 1, and then set
To solve this problem, after obtaining the key value through get, ensure that the key value is not modified by other clients until the function execution is complete. This prevents race conditions.
After the watch command is executed, set key 2 is modified before the transaction is executed. Therefore, the set key 3 command in the last transaction is not executed, and the EXEC command returns an empty result.
Because the watch command only blocks the execution of a transaction after the monitored key value is modified, it cannot be ensured that other clients do not modify this key value, therefore, we need to re-execute the entire function after EXEC fails.
After EXEC is executed, the monitoring of all keys is canceled. You can also use the unwatch command to cancel monitoring.
Iv. survival time
Use the expire command to set the survival time of a key. redis will automatically delete it after that.
If you want to delete the session: 29e3d key 15 minutes later
1 indicates success, 0 indicates that the key does not exist, or the setting fails.
How long can a key be deleted? You can use the ttl command. The returned value is the remaining time of the key (unit: s)
If the key does not exist, ttl returns-1.-1 is also returned, indicating that the ttl is not set for the key (permanent ).
If you want to cancel the life time setting, that is, restore permanently, run the persist command. 1 successful, 0 failed
In addition to the number of times, the set or getset command can be used to assign values to the key, and the survival time of the key is also cleared.
Using expire will reset the key survival time.
Other commands that only operate on key values (incr, lpush, hset, and zrem) do not affect the key survival time.
The time parameter of the expire command must be an integer (s. More precise. Use pexpire in milliseconds. You can use pttl to return the remaining time.
If the watch command monitors a key with a life time, the key is automatically deleted after the key expires and is not considered changed by the watch command.
Two other commands
Expireat and pexpireat use unix time as the time parameter. Expireat is measured in seconds and pexpireat is measured in milliseconds.