当前位置:网站首页>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 ,
边栏推荐
- Final review -php learning notes 2-php language foundation
- [notes] polygon mesh processing learning notes (10)
- Niuke Xiaobai month race 52
- Applet uses QR code plug-in
- [untitled]
- Cesium learning notes (V) custom geometry and appearance
- Acreems energy efficiency management platform escorts the power safety of high-rise residential areas
- Markdown支持的emoji图标
- 电流探头电路分析
- Gilbert Strang's course notes on linear algebra - Lesson 1
猜你喜欢

Cesium learning notes (III) creating instances

微信小程序使用vant weapp报错

1. Problems related to OpenGL window and environment configuration
![[tensorflow GPU] building of deep learning environment under windows11](/img/10/6d30d4c310e6677049a1012d47f773.png)
[tensorflow GPU] building of deep learning environment under windows11

2021-04-29

At the end of June, you can start to make preparations, otherwise you won't have a share in such a profitable industry
![[untitled]](/img/b8/e3f54fe5d1079663799887e62cb07c.jpg)
[untitled]

C # about Net cognition

Redis设计与实现(八)| 事务

【NVMe2.0b 14-2】Create/Delete Queue
随机推荐
牛客小白月赛52
【NVMe2.0b 14-2】Create/Delete Queue
Full stack performance testing theory - Summary
MIME类型大全
MySQL加索引语句不加锁:ALGORITHM=INPLACE, LOCK=NONE
Interference source current spectrum test of current probe
【NVMe2.0b 14-1】Abort、Asynchronous Event Request、Capacity Management command
1162 Postfix Expression
Sword finger offer II 076 The kth largest number in the array (use heap to solve TOPK problem)
2021-02-27
This point in JS
[tensorflow GPU] building of deep learning environment under windows11
Qqquickpainteditem implements graffiti program drawing board
Applet uses QR code plug-in
JS中的this指向
【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
Sword finger offer II 074 Merge interval (sort, array)
How to handle the expired data of redis and what are the elimination mechanisms?
Unit Test
[nvme2.0b 14-8] set features (Part 2)


