Redis Learning Note (10) Advanced message Notification

Source: Internet
Author: User
Tags redis

Task Queue

Using the Lpush and Rpop command action lists to implement the queue

Blpop key [key ...] timeout (s)

Brpop key [key ...] timeout (s)

Blpop/brpop is a blocking type that detects multiple keys at the same time and blocks if none of the keys have elements, and if one of the keys has elements, the element is ejected from the key, and the return value is the key name and the corresponding element value.

If multiple keys have elements, one of the first keys is taken from left to right in order to implement the priority queue.

Publish / Subscription Mode

The Publish/Subscribe mode contains two roles, publisher and Subscriber, respectively. Subscribers can subscribe to one or several channels (channel), and publishers can send messages to the specified channel, and all subscribers to the channel receive this message.

The publisher's command to publish the message is publish, and the usage is publish channelmessage, and the return value is the number of subscribers who received the message

Redis b> PUBLISH channel.1 hi

(integer) 0

Messages sent out will not be persisted, that is, when a client subscribes to CHANNEL.1, it can only receive subsequent messages that are posted to that channel, and the previously sent messages are not received.

The command to subscribe to the channel is SUBSCRIBE, can subscribe to multiple channels at the same time, usage is SUBSCRIBE Channel [channel ...]

Redis a> SUBSCRIBE CHANNEL.1

Readingmessages ... (Press Ctrl-c to quit) "Subscribe" "CHANNEL.1" (integer) 1

After executing the subscribe command, the client enters the subscription state, in which case the client cannot use a command other than the Subscribe/unsubscribe/psubscribe/punsubscribe 4 commands that belong to the Publish/subscribe mode. Otherwise you will get an error.

After entering the subscription status, the client may receive three types of replies. Each type of reply contains 3 values, the first value is the message type, and the second and third values have different meanings depending on the message type. Possible values for message types are:

1) Subscribe. Represents the feedback for a successful subscription. The second value is the channel name for which the subscription succeeds, and the third value is the number of current client subscription channels.

2) message. This type of reply is our top concern and it represents the received message. The second value represents the channel name that generated the message, and the third value is the message content.

3) unsubscribe. Indicates that a channel was successfully unsubscribed. The second value is the channel name, and the third value is the number of channels the current client subscribes to, and when this value is 0 o'clock, the client exits the subscription state and can then execute other commands that are not in the Publish/subscribe mode.

Subscribe by rules

The Psubscribe command subscribes to the specified rule. Rules support GLOB style wildcard format. Psubscribe pattern [pattern ...]

Redis c> psubscribe channel.? *

Readingmessages ... (Press Ctrl-c to quit) "Psubscribe" "channel.? * "(integer) 1

Reply: "Psubscribe" "channel.? * "CHANNEL.1" "hi!"

The first value indicates that the message was received by subscribing to the channel through the Psubscribe command, the second value represents the wildcard character used at the time of the subscription, the third value represents the channel name that actually received the message, and the fourth value is the message content.

punsubscribe pattern [pattern ...] Unsubscribe from the specified rule, and if there are no parameters, all rules will be retired.

The Punsubscribe command can only be used to unsubscribe from the rules subscribed by the Psubscribe command, without affecting the channels subscribed directly through the Subscribe command, as is the unsubscribe command.




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.