当前位置:网站首页>17_ Redis_ Redis publish subscription
17_ Redis_ Redis publish subscription
2022-07-02 15:20:00 【Listen to the rain】
Redis Publish subscribe (pub/sub) It's a kind of Message communication mode ∶ sender (pub) Send a message , subscriber (sub) receive messages .
Redis Clients can subscribe to any number of channels . for example : A user can pay attention to as many as he wants up Lord
subscribe / Release message map ∶
first : Message sender
the second : channel
Third : Message subscribers
The picture below shows the channel channel1, And three clients that subscribe to this channel ―—client2、 client5 and client1 The relationship between ∶
When new news passes PUBLISH Command to channel channel1 when , This message will be sent to the three clients who subscribe to it :
command : These commands are widely used to build instant messaging applications , Such as Internet chat room (chatroom) And real-time broadcasting 、 Real time reminder, etc .
test
Subscribers :
127.0.0.1:6379> SUBSCRIBE xsy666 // Subscribe to a channel :xsy666
Reading messages… (press Ctrl-C to quit)
1 ) “subscribe”
2 ) “xsy666”
3 ) (integer) 1
// Waiting to read the push message
1 ) “message” // news
2 ) “xsy666” // Which channel
3 ) “hello,xsy” // The details of the message
1 ) “message”
2 ) “xsy666”
3 ) “hello,zhangsan”
1 ) “message”
2 ) “xsy666”
3 ) “byebye”
The sender :
127.0.0.1:6379> PUBLISH xsy666 hello,xsy // Publishers post messages to channels !
(integer) 1
127.0.0.1:6379> PUBLISH xsy666 hello,zhangsan
(integer) 1
127.0.0.1:6379> PUBLISH xsy666 byebye
(integer) 1
principle
Redis It's using C Realized , Through analysis Redis Source code pubsub.c file , Understand the underlying implementation of publish and subscribe mechanisms , In order to deepen the understanding of Redis The understanding of the .
Redis adopt PUBLISH、SUBSCRIBE and PSUBSCRIBE And so on to realize the function of publish and subscribe .
adopt SUBSCRIBE After subscribing to a channel ,redis-server It maintains a dictionary , The key of the dictionary is one by one channel, The value of a dictionary is a linked list , The linked list holds all the subscriptions channel The client of .SUBSCRIBE The key to command , Is to add the client to the given channel In the subscription list of .
adopt PUBLISH Command to send messages to subscribers ,redis-server The given channel is used as the key , In what it maintains channel Look up the list of all clients who subscribe to this channel in the dictionary , Traverse the list , Publish the message to all subscribers .
Pub/Sub Literally, it's publishing ( Publish ) With subscription ( Subscribe ), stay Redis in , You can set to a certain key Value for message publishing and message subscription , When one key After the news is released on the value , All clients that subscribe to it will receive the corresponding message . The most obvious use of this feature is as a real-time messaging system , For example, ordinary instant chat , Group chat and other functions .
Use scenarios :
1. Real time message system
2. Live chat
3. subscribe , Focus on
边栏推荐
- 12_Redis_Bitmap_命令
- [solution] educational codeforces round 82
- TiDB混合部署拓扑
- 记一次面试
- Huawei interview question: no palindrome string
- Mfc a dialog calls B dialog function and passes parameters
- C语言中的算术运算及相关练习题
- MFC console printing, pop-up dialog box
- Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
- N皇后问题的解决
猜你喜欢
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)
[C language] explain the initial and advanced levels of the pointer and points for attention (1)
Tidb data migration tool overview
Base64 coding can be understood this way
14_Redis_乐观锁
XML配置文件
15_Redis_Redis.conf详解
21_ Redis_ Analysis of redis cache penetration and avalanche
Jenkins Pipeline 应用与实践
工程师评测 | RK3568开发板上手测试
随机推荐
PHP method to get the index value of the array item with the largest key value in the array
LeetCode 2310. The number of digits is the sum of integers of K
[c voice] explain the advanced pointer and points for attention (2)
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
Solve the problem that El radio group cannot be edited after echo
N皇后问题的解决
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
学习使用php实现公历农历转换的方法代码
Map介绍
About text selection in web pages and counting the length of selected text
C# 线程传参
03_ Linear table_ Linked list
C# richTextBox控制显示最大行数
14_Redis_乐观锁
Tidb hybrid deployment topology
20_Redis_哨兵模式
MFC 定时器使用
How to solve the problem of database content output
19_Redis_宕机后手动配置主机