Cache database-redis (subscribe to publish)

Source: Internet
Author: User

One: Redis Publish Subscription

A Redis Publish Subscription (PUB/SUB) is a message communication pattern: the Sender (pub) sends a message and the Subscriber (sub) receives the message.

Redis clients can subscribe to any number of channels.

Shows the relationship between channel Channel1 and the three client--client2, CLIENT5, and client1 subscribing to this channel:

When a new message is sent to channel Channel1 via the PUBLISH command, the message is sent to the three clients subscribed to it:

Two: Example
#-*-coding:utf-8-*-__author__='Shisanjun'ImportRedisclassRedishelper (object):def __init__(self): self.__conn=redis. Redis (host="192.168.0.121") Self.chan_sub="fm104.5"self.chan_pub="fm104.5"    defPublic (self,msg): Self.__conn. Publish (SELF.CHAN_PUB,MSG)returnTruedefSubscribe (self): Pub=self.__conn. PubSub ()#equivalent to turning on the radioPub.subscribe (Self.chan_sub)#Tune ChannelPub.parse_response ()#ready to accept, the next call will begin to accept        returnPub

Subscription

# -*-coding:utf-8-*- __author__ ' Shisanjun '  from Import redishelperobj=redishelper () redis_sub=obj.subscribe () while True:    msg=redis_sub.parse_response ()    print(msg)

Release

# -*-coding:utf-8-*- __author__ ' Shisanjun '  from Import redishelperobj=redishelper () obj.public ("hello")

Cache database-redis (subscribe to publish)

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.