当前位置:网站首页>Redis design and Implementation (VII) | publish & subscribe
Redis design and Implementation (VII) | publish & subscribe
2022-06-30 08:22:00 【Just look, not like】
List of articles
Publish and subscribe
- summary
Redis The publish and subscribe function of is provided by
PUBLISH、SUBSCRIBE、PSUBSCRIBEWait for the order to make up .
Through executionSUBSCRIBEcommand , The client can subscribe to one or more channels , To become subscribers to these channels ( subscriber ): Whenever another client sends a message to the subscribed channel(message)when , All subscribers to the channel will receive this message .
- Command to use
# If all three clients execute this , All three clients are subscribers to this channel
SUBSCRIBE "news.it"
# A client sends this , Then all three subscribers will receive
PUBLISH "news.it" "hello"

- PSUBSCRIBE
This command allows the client to subscribe to one or more modes ,
Suppose as shown in the figure below A subscribe it,B subscribe et,C、D Subscribe to a ie Pattern , So it Channel sends messages ,C、D I will also receive,Wildcard ? Represents a placeholder ,* Represents any placeholder ( Include 0 individual ),?* Represents more than one placeholder
# PSUBSCRIBE You can subscribe to one or more patterns , Similar to wildcards
PSUBSCRIBE "news.?t"

1. Channel subscription and unsubscribe
- summary
Clients subscribing to the same channel will
redisServerStructure in the form of a linked listpubsub_channelsIn the dictionary , Here's the picture
1.1 Subscribed Channels
- Add a dictionary , Will traverse all input channels
- Add the corresponding key of the channel , If the channel does not exist , Then there must be no bond , So I will add the key to the dictionary first
- If there is a channel , Then join the team directly
1.2 unsubscribe
- summary
This action is the reverse order of the subscription action just now
UNSUBSCRIBE " channel 1" " channel 2"
2. Mode subscription and unsubscribe
- summary
Mode subscription and unsubscribe will be in redisServer Of pubsub_patterns Properties of the , It is also a linked list , The structure of each node is pubsubPattern
- example
2.1 A subscription model
- step
- Create a new one pubsubPattern structure , Structure pattern Property to the subscribed mode ,client Client with property set to subscription mode .
- take pubsubPattern Structure added to pubsub_patterns The end of a list .
2.2 unsubscribe
- summary
Mode unsubscribe order
PUNSUBSCRIBE yes PSUBSCRIBECounter operation of orders : When a client unsubscribes from a certain mode or modes , The server will bepubsub_patternsLook up and delete those in the listpatternProperty is unsubscribed mode , alsoclientProperty of the client executing the unsubscribe commandpubsubPatternstructure .
3. Send a message
- summary
When one Redis Client execution
PUBLISH <channel> <message>Command will messagemessageSend to channelchannelWhen , The server needs to perform the following two actions :
- The message is message Send to channel All subscribers to the channel .
- If there are one or more modes pattern And channel channel Match , Then send the message message Send to pattern Subscribers to patterns .
4. View subscription messages
- summary
PUBSUBThe order is Redis 2.8 One of the new orders , The client can view information about the channel or mode through this command , For example, how many subscribers a channel currently has , Or how many subscribers there are in a certain mode at present , And so on .
4.1 PUBSUB CHANNELS
- summary
PUBSUB CHANNELS <pattern>, The subcommand is used to return the channel that the server is currently subscribed to , among pattern It's optional
- No mode given , Then return to all subscribed channels
- Given mode , Then all channels that conform to the mode
4.2 PUBSUB NUMSUB
- summary
PUBSUB NUMSUB [channel-1 -2 ...-n]Sons command , Returns the number of subscribers of the corresponding channel , The bottom layer is to traverse the channel name corresponding to the dictionary , The length of the corresponding linked list
4.3 PUBSUB NUMPAT
- summary
PUBSUB NUMPATThe subcommand is used to return the number of currently subscribed modes of the server . This subcommand is done by returningpubsub patternsThe length of the list to achieve , Because the length of the linked list is the number of subscription modes of the server ,
边栏推荐
- 2021-02-27
- 【JUC系列】Fork/Join框架之概览
- MySQL加索引语句不加锁:ALGORITHM=INPLACE, LOCK=NONE
- Gilbert Strang's course notes on linear algebra - Lesson 1
- [tensorflow GPU] building of deep learning environment under windows11
- 【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
- Construction of energy conservation supervision system for campus buildings of ankery University
- Experiment 2 LED button PWM 2021/11/22
- 多快好省,低门槛AI部署工具FastDeploy测试版来了!
- Halcon12+vs2013 C # configuration
猜你喜欢
![[nvme2.0b 14 - 5] commande de téléchargement / commande du logiciel](/img/98/bb250030f3e31e71d48e3be3ea4698.png)
[nvme2.0b 14 - 5] commande de téléchargement / commande du logiciel

Oracle expansion table space installed in docker

【NVMe2.0b 14-7】Set Features(上篇)

【NVMe2.0b 14】NVMe Admin Command Set

1162 Postfix Expression

跳槽字节跳动很难嘛?掌握这些技巧,你也能轻松通过

Graffiti Wi Fi & ble SoC development slide strip

Gilbert Strang's course notes on linear algebra - Lesson 1

Is it difficult to jump job ByteDance? With these skills, you can easily pass

Want to change careers, but don't know what to do? This article is written for you who are confused
随机推荐
Cesium learning notes (I)
el-input 限制只能输数字
MySQL cannot connect to the intranet database
Do you know the IP protocol?
涂鸦Wi-Fi&BLE SoC开发幻彩灯带
【NVMe2.0b 14-2】Create/Delete Queue
Map,String,Json之間轉換
How to handle the expired data of redis and what are the elimination mechanisms?
多快好省,低门槛AI部署工具FastDeploy测试版来了!
Cesium learning notes (II) uploading data using rest API
Redis设计与实现(二)| 数据库(删除策略&过期淘汰策略)
自制GIF动态图-gifcam
What management improvements can CRM bring to enterprises
[nvme2.0b 14 - 5] commande de téléchargement / commande du logiciel
F12抓包用于做postman接口测试的全过程解析
Unity 基础光照模型
【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
Dlib library blink
Deploy the cow like customer network project on the ECS
从0开始构建一个瀚高数据库Docker镜像


