REDIS Message Notification

Source: Internet
Author: User
Message Notification Task Queues Benefits of using task queuesLoose coupling. Producers and consumers do not need to know each other's implementation details, just agree to a good description of the task format. This allows producers and consumers to be easily extensible by different teams using different programming languages. Consumers can have multiple, and can be distributed in different servers, which can easily reduce the load on a single server

Using the Lpush and Rpop commands to implement queues, here is a new command Brpop and Blpop brpop key [key ...] timeout (s) Brpop commands are similar to the Rpop commands, The only difference is that when there are no elements in the list, the Brpop command blocks the connection until a new element is added to the priority queue

Use the Brpop command to detect multiple key functions. If multiple keys have elements, the first key is taken from left to right in one element. Therefore, to achieve the priority queue, the high priority key to the front is OK. Publish/Subscribe mode The Publish/Subscribe mode contains two roles, the publisher and the Subscriber, respectively. Subscribers can subscribe to one or more channels (channel) publishers can send messages to the specified channel, and all subscribers subscribing to this channel will receive this message.

The
PUBLISH Channel msg publishes a message. The number of subscribers receiving this message is not persisted, which means that when a client subscribes to the channel, it receives only subsequent messages that are posted to the channel, and the previously sent message is not received
SUBSCRIBE Channel [channel ...] Subscribe to channels, you can subscribe to multiple channels at once
unsubscribe [channel ...] unsubscribe from the specified channel, and if no channel is specified, all channels will be canceled
psubscribe pattern [pattern ...] subscribe to one or more channels that match a given pattern
punsubscribe [pattern [...]] unsubscribe from the specified rule and unsubscribe from all rules if there are no parameters
PUBSUB subcommand [argument [argument ...]] View subscriptions and publish system status

Note The client enters the subscription state when the Subscribe command is executed, where the client cannot use a command other than the 4 commands that are part of the Publish/subscribe mode except Subscribe/unsubscribe/psubscribe/punsubscribe. Otherwise, you'll get an error. Using the Punsubscribe command, you can only unsubscribe from the rules that are subscribed through the psubscribe command, without affecting the channels that are subscribed directly through the subscribe command; the same unsubscribe command does not affect rules that are subscribed through the Psubscribe command. Another error prone is that using the Punsubscribe command to unsubscribe from a rule will not expand the wildcard character, but the strict string matching, so punsubscribe cannot unsubscribe channel. Rules, but must use Punsubscribe Channel.* to unsubscribe.

Clients may receive three types of replies after entering the subscription state. Each type of reply contains 3 values,

Message type (first value) Second Value Third Value
Subscribe: Feedback for successful subscriptions Subscribe to a successful channel name Number of channels subscribed by the current client
Message: Indicates received messages The name of the channel that generated the message The contents of the message
Unsubscribe: Indicates successful unsubscribe of a channel The corresponding channel name Number of channels subscribed by the current client

The first value is unsubscribe, and when the third value is 0 o'clock the client exits the subscription status, and can then perform other commands other than publish/subscribe mode.

Example:

Redis a>subscribe channel.1
Reading messages ... (Press Ctrl-c to quit)
1) "Subscribe"
2) "CHANNEL.1"
3) (integer) 1
# Psubscribe Subscribe to
Redis c>psubscribe channel by rule.? *
Reading messages ... (Press Ctrl-c to quit)
1) "Psubscribe" (
2) "channel". * "
3) (integer) 1

redis b>publish channel.1 hi!
(integer) 2

#此时, C received reply
1) "Pmessage 2"
channel.? * "# Note that there is more than one value returned, the second value is the wildcard character used when subscribing to
3)" CHANNEL.1 "4" "
hi!"

Rule channel.? * Can match CHANNEL.1 and channel.10, but will not match channel.


If found wrong, please tap, welcome message exchange, thank you

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.