In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you how to use Redis transactions, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Transaction:
127.0.0.1 purl 6379 > MULTI
OK
127.0.1 6379 > sadd "user:1:state" 1
QUEUED
127.0.0.1 6379 > SADD "user:2:state" 2
QUEUED
127.0.0.1 purl 6379 > EXEC
1) (integer) 1
2) (integer) 1
The transaction is initiated by MULTI, followed by sadd commands to implement the concerned and concerned operations. Instead of executing these commands, redis returns QUEUED to indicate that the two commands have entered the queue of transactions waiting to be executed.
When exec later tells redis that all commands in the waiting food queue will be executed in the order in which they are sent. The order of return values for exec is the same as the order of commands.
1. Syntax error:
Syntax error means that the command does not exist or the number of parameters of the command is incorrect, as follows:
127.0.0.1 purl 6379 > MULTI
OK
127.0.0.1 purl 6379 > set name Mike
QUEUED
127.0.0.1 purl 6379 > errorcommand name
(error) ERR unknown command' errorcommand'
127.0.0.1 purl 6379 > EXEC
(error) EXECABORT Transaction discarded because of previous errors.
Due to the existence of incorrect commands, redis directly returned an error to discard the entire transaction. It doesn't even perform syntactically correct ones, as follows:
127.0.0.1 purl 6379 > MGET name
1) (nil)
two。 Run error: a run error is an error that occurs when a command is executed, such as using the wrong command to manipulate the data type.
This error redis continues to execute, and redis cannot find the error.
127.0.0.1 purl 6379 > MULTI
OK
127.0.0.1 purl 6379 > set name hello
QUEUED
127.0.0.1 SADD name 6379 >
QUEUED
127.0.1 6379 > SET name "abc"
QUEUED
127.0.0.1 purl 6379 > EXEC
1) OK
2) (error) WRONGTYPE Operation against a key holding the wrong kind of value
3) OK
127.0.0.1 purl 6379 > GET name
"abc"
This kind of error can only rely on the programmer to grasp and check the error.
WATCH command: monitor one or more keys, once one of the key values is modified, subsequent transactions will not be executed. Monitoring continues until the exec command.
127.0.0.1 SET key 6379 > 1
OK
127.0.0.1 purl 6379 > WATCH key
OK
127.0.0.1 SET key 6379 >
OK
127.0.0.1 purl 6379 > MULTI
OK
127.0.0.1 SET key 6379 >
QUEUED
127.0.0.1 purl 6379 > EXEC
(nil) / / returns nil, indicating that the monitored ticket has changed and the transaction is cancelled.
127.0.0.1 purl 6379 > GET key
"2"
Publish / subscribe respectively publishers and subscribers, subscribers can subscribe to one or more channels, and publishers can publish messages to specified channels. All subscribers who subscribe to this channel will receive this message
Post the message:
127.0.1 6379 > PUBLISH newsChannel "good morning news report"
(integer) 0
For the time being, no subscribers have returned as 0
Subscription message:
127.0.0.1 purl 6379 > SUBSCRIBE newsChannel
Reading messages... (press Ctrl-C to quit)
1) "subscribe" / / subscribe indicates the feedback of successful subscription. The second value is the name of the channel successfully subscribed, and the third value is the number of channels currently subscribed by the client.
If the reply type of message is of concern to us, it indicates the received message, and the second value indicates the name of the channel that generated the message. The third value is the content of the message.
Unsubscribe unsubscribes to a channel, the second value is the corresponding channel name, and the third value is the number of channels currently subscribed by the client.
2) "newsChannel"
3) (integer) 1
Use publish to release the message again:
127.0.0.1 6379 > PUBLISH newsChannel "News 2"
(integer) 1
Subscribe to channels:
127.0.0.1 purl 6379 > SUBSCRIBE newsChannel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "newsChannel"
3) (integer) 1
^ [A1) "message"
2) "newsChannel"
3) "News 2"
You can subscribe to multiple channels according to regular expressions:
127.0.0.1 purl 6379 > PSUBSCRIBE news*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "news*"
3) (integer) 1
PSUBSCRIBE subscribes to channels that start with any character and end with news.
127.0.0.1 purl 6379 > PSUBSCRIBE news*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "news*"
3) (integer) 1
1) "pmessage"
2) "news*"
3) "newsChannel"
4) "News 3"
The above is all the contents of the article "how to use Redis transactions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.