当前位置:网站首页>redis消息订阅
redis消息订阅
2022-07-17 05:57:00 【程序三两行】
一、概述
发布/订阅模式包含两种角色,分别是发布者和订阅者。发布者可以向指定的频道发送消息;订阅者可以订阅一个或者多个频道,所有订阅此频道的订阅者都会收到此消息,图示

二、原理
底层是通过字典(图中的publish_channel)实现的,这个字典就用于保存订阅频道的信息:字典的键为正在被订阅的频道,而字典的值则是一个链表,链表中保存了所有订阅这个频道的客户端。
在下图展示的这个pubsub_channels的示例中,client2、client5、client1就订阅了channel1,而其他频道也被别的客户端所订阅

三、实现
常用命令
序号 | 命令及描述 |
1 | PSUBSCRIBE pattern [pattern ...] 订阅一个或多个符合给定模式的频道。 |
2 | PUBSUB subcommand [argument [argument ...]] 查看订阅与发布系统状态。 |
3 | 将信息发送到指定的频道。 |
4 | PUNSUBSCRIBE [pattern [pattern ...]] 退订所有给定模式的频道。 |
5 | SUBSCRIBE channel [channel ...] 订阅给定的一个或多个频道的信息。 |
6 | UNSUBSCRIBE [channel [channel ...]] 指退订给定的频道。 |
这里在cmd演示,如果是代码,原理一样
首先打开一个cmd订阅频道
redis 127.0.0.1:6379> SUBSCRIBE redisChat #创建了订阅频道名为 redisChat: Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "redisChat" 3) (integer) 1
再打开一个cmd发消息
redis 127.0.0.1:6379> PUBLISH redisChat "Hello,Redis" (integer) 1 redis 127.0.0.1:6379> PUBLISH redisChat "Hello,hahaha" (integer) 1
查看订阅者窗口收到消息
# 订阅者的客户端会显示如下消息 1) "message" 2) "redisChat" 3) "Hello,Redis" 1) "message" 2) "redisChat" 3) "Hello,hahaha"
边栏推荐
- [day01] preface, introductory program, constant variables
- Canel Introduction & use
- Spark3.x-mysql method of actual combat to achieve Kafka precise one-time consumption
- TSN security protocol (802.1qci)
- 京东购买意向预测(四)
- Pytoch notes (3)
- RISC-V技术杂谈
- INSTALL_PARSE_FAILED_MANIFEST_MALFORMED
- JS array intersection, subtraction and union
- Titanic passenger rescue prediction (Advanced)
猜你喜欢

【JVM】之堆内存、逃逸分析、栈上分配、同步省略、标量替换详解

Xiaoyi and you talk about how to realize c-v2x HIL test in v2x test series (2022 version)

修改radio样式

Wechat oauth2.0 login process and security analysis

RNN卷积神经网络

redis集群

Pytoch notes (3)

How does continuous integration manage Jenkins?

VMware cloud director 10.4 release (including download) - cloud computing provisioning and management platform

Flink introduction to practice - phase II (illustrated runtime architecture)
随机推荐
Machine learning interview questions (Reprinted)
收单外包服务商北京捷文科技以约4.8亿转让60%股份
修改滚动条样式
openvino机器学习初体验
【MySQL】 MVCC:正确理解MVCC及其实现原理
京东购买意向预测(三)
Titanic passenger rescue prediction (Advanced)
会话技术【黑马入门系列】
VMware cloud director 10.4 release (including download) - cloud computing provisioning and management platform
【MySQL】 事务:事务基础知识、MySQL事务实现原理、事务日志 redolog & undolog 详解
xgboos-hperopt
Mongodb index
Ku115 FPGA high performance 10G Optical fiber network hardware accelerator card / 2-way 10G Optical fiber data accelerator card
RNN convolutional neural network
Xilinx ultrascale+ MPSoC (zu9eg/zu15eg) high performance PCIe data preprocessing board
pytorch随记(5)
C语言一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程
Spark3.x-mysql method of actual combat to achieve Kafka precise one-time consumption
Introduction & use of Maxwell
SCI论文的Highlights怎么写(正经的教你怎么写)