Command Example:1.The transaction is performed normally: #Execute the Redis client tool under the shell command line. /> redis-cli #starts a new transaction on the current connection. Redis 127.0.0.1:6379>Multi Ok #The First command in the execution transaction, from the return result of the command, shows that the command is not executed immediately, but instead is stored in the command queue of the transaction. Redis 127.0.0.1:6379>incr T1 QUEUED #A new command is executed, and the result shows that the command is also stored in the command queue of the transaction. Redis 127.0.0.1:6379>incr T2 QUEUED #executes all commands in the queue of the transaction command, from the result it can be seen that the result of the command in the queue is returned. Redis 127.0.0.1:6379>exec 1) (integer) 1 2) (integer) 1
2. There are failed commands in the transaction: #open a new transaction. Redis 127.0.0.1:6379>Multi Ok #set the value of key A to 3 of type string. Redis 127.0.0.1:6379>set a 3 QUEUED #the element is ejected from the head of the value associated with key A, because the value is a string type and the Lpop command can only be used with the list type, so the command fails when the EXEC command is executed. Redis 127.0.0.1:6379>Lpop a QUEUED #set the value of key a again to string 4. Redis 127.0.0.1:6379>set a 4 QUEUED #gets the value of key A to confirm that the value was successfully set by the second set command in the transaction. Redis 127.0.0.1:6379>Get a QUEUED #As can be seen from the results, the second command in the transaction failed Lpop execution, and the subsequent set and get commands performed successfully, which is the most important difference between a Redis transaction and a transaction in a relational database. Redis 127.0.0.1:6379>exec 1) OK 2) (Error) ERR operation against a key holding the wrong kind of value 3) OK 4) "4"
3. To Roll back a transaction: #sets a value for the key T2 before a transaction executes. Redis 127.0.0.1:6379>set t2 tt Ok #open a transaction. Redis 127.0.0.1:6379>Multi Ok #sets a new value for the key within a transaction. Redis 127.0.0.1:6379>set T2 ttnew QUEUED #discards a transaction. Redis 127.0.0.1:6379>Discard Ok #looking at the value of the key T2, you can see from the result that the value of the key is still the value before the start of the transaction. Redis 127.0.0.1:6379>Get T2 "TT" For more highlights, please follow: http://bbs.superwu.cnfocus on the two-dimensional code of Superman Academy: |
Redis Things Command Example