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)