Redis achieves efficient, non-missing event encapsulation

Source: Internet
Author: User

Although Redis has a subscription function, the subscription function is real-time, and after this point, the message is not received.

At the same time, if the subscribed client is shutdown for some special reason, it will not find a full subscription event.

But fortunately, Redis also has a message queue, through Message Queuing, we can not only make the publication submission faster (the Conference traverse all Subscribers, and notify all subscribers), and may not have to worry about the subscriber missing out the notice of the exception.

I will coordinate subscribers, post offices, and publisher roles in subsequent essays through Swoole,phpredis,yii.

The actual implementation of the PUBLISH command is done by the Pubsubpublishmessage function, which is fully defined as follows:

Send Message
int Pubsubpublishmessage (RobJ *channel, RobJ *message) {
int receivers = 0;
struct Dictentry *de;
ListNode *ln;
Listiter Li;
/* Send to clients listening for that channel */
Send a message to subscribers on all channels
de = Dictfind (Server.pubsub_channels,channel);
if (DE) {
List *list = Dictgetval (DE); Remove all Subscribers
ListNode *ln;
Listiter Li;
Traverse all Subscribers and send messages to them
Listrewind (List,&li);
while ((ln = listnext (&li)) = NULL) {
Redisclient *c = ln->value;
Addreply (C,shared.mbulkhdr[3]);
Addreply (C,shared.messagebulk);
Addreplybulk (C,channel); Print Channel name
Addreplybulk (C,message); Print messages
receivers++; Update the number of recipients
}
}
/* Send to clients listening to matching channels */
Send a message to all subscribers who are matched to the pattern
if (Listlength (server.pubsub_patterns)) {
Listrewind (Server.pubsub_patterns,&li); Remove all modes
Channel = Getdecodedobject (channel);
while ((ln = listnext (&li)) = NULL) {
Pubsubpattern *pat = ln->value; Remove mode
If the pattern matches the channel,
Send a message to subscribers in this mode
if (Stringmatchlen (char*) pat->pattern->ptr,
Sdslen (PAT->PATTERN->PTR),
(char*) Channel->ptr,
Sdslen (Channel->ptr), 0)) {
Addreply (Pat->client,shared.mbulkhdr[4]);
Addreply (Pat->client,shared.pmessagebulk);
Addreplybulk (Pat->client,pat->pattern); Print the pattern that is matched
Addreplybulk (Pat->client,channel); Print Channel name
Addreplybulk (Pat->client,message); Print messages
receivers++; Update the number of recipients
}
}
Decrrefcount (channel); Releasing The Used channel
}
return receivers; Returns the number of recipients
}

Redis achieves efficient, non-missing event encapsulation

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.