Redis Things Command Example

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.