Basic Tutorials of Redis(7) -Publish and Subscribe
This post is mainly about the publishment and subscription in Redis.I think you may subscribe some offiial
accounts on wechat,and when the authors published something new to their accounts,you will get them in
your wechat.The instance can make you understand this pattern easily.You will find there only 6 commands
for publishment and subscription.
This post introduces the basic usages as well.I will show you how to publish a message to a channel.publish
is the command to publish sometings.I publish a channel named news-nba with the message nba.And the client
return me a integer 0.What is the meaning of the return value?It means that there is nobody subscribe this channel.
publish news-nba nba
I open a new client for subscriber.In this client,I subscribe the above channel news-nba.It will return something
about this channel.
subscribe news-nba
OK,let's publish a new message to the news-nba to see the changes in the both clients.After publishing lakers to
the news-nba channel,this client returns 1.It means that the channel has a subscriber.
publish news-nba lakers
Let's turn to the subscriber's client.You will find the message lakers was already in the client.So amazing it is.
publish a new message to the news-nba,it returns 2 meaning ...(you understand it)
All right,let's see the other commands of this feature in Redis.Seeing the sql first:
select * from channels where channelname like 'news%' To execute this sql,you will get all of the channel whoes
name start with news.Redis can also do that to help us subscibe many channels by matching their name vaguely.
For example I want to subscribe all the channels of news.I will use psubscribe to do this job.
psubscribe news-*
publish news-tech tech
The subscriber's client will receive the message from the channel news-tech.
Again!!Publishing a message to a new channel.
As you can see,the client receives this message as well.
following code demonstrates the usage in C#.
//publisher
var sub = redis.GetSubscriber();
//subscriber
var sub2 = redis.GetSubscriber(); sub.Publish("news-nba", "nba");
sub.Publish("news-tech", "tech"); //sub
sub2.Subscribe("news-nba", (channel, value) =>
{
Console.WriteLine(string.Format("{0} has been published to {1}!",value,channel));
});
sub2.Subscribe("news-tech", (channel, value) =>
{
Console.WriteLine(string.Format("{0} has been published to {1}!", value, channel));
}); sub.Publish("news-nba", "Lakers");
sub.Publish("news-nba", "Kobe"); sub.Publish("news-tech", "lumia"); System.Threading.Thread.Sleep();
Console.WriteLine("unscribe the news-nba");
sub2.Unsubscribe("news-nba");//unsub sub.Publish("news-tech", "surface pro3"); System.Threading.Thread.Sleep();
sub.Publish("news-nba", "james"); System.Threading.Thread.Sleep();
Console.WriteLine("unscribe all channels");
sub2.UnsubscribeAll(); sub.Publish("news-nba", "paul");
sub.Publish("news-tech", "surface pro4");
Basic Tutorials of Redis(7) -Publish and Subscribe的更多相关文章
- Basic Tutorials of Redis(9) -First Edition RedisHelper
After learning the basic opreation of Redis,we should take some time to summarize the usage. And I w ...
- Basic Tutorials of Redis(6) - List
Redis's List is different from C#'s List,but similar with C#'s LinkedList.Sometimes I confuse with t ...
- Basic Tutorials of Redis(2) - String
This post is mainly about how to use the commands to handle the Strings of Redis.And I will show you ...
- dotnet core use Redis to publish and subscribe
安装Redis 同样我这边再次使用Docker, 方便快捷: # 拉取镜像 docker pull redis # 运行镜像 docker run -d -p 6379:6379 --name red ...
- Basic Tutorials of Redis(8) -Transaction
Data play an important part in our project,how can we ensure correctness of the data and prevent the ...
- Basic Tutorials of Redis(5) - Sorted Set
The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...
- Basic Tutorials of Redis(4) -Set
This post will introduce you to some usages of Set in Redis.The Set is a unordered set,it means that ...
- Basic Tutorials of Redis(3) -Hash
When you first saw the name of Hash,what do you think?HashSet,HashTable or other data structs of C#? ...
- Basic Tutorials of Redis(1) - Install And Configure Redis
Nowaday, Redis became more and more popular , many projects use it in the cache module and the store ...
随机推荐
- Hawk 5.1 数据导入和导出
除了一般的数据库导入导出,Hawk还支持从文件导入和导出,支持的文件类型包括: Excel CSV(逗号分割文本文件) TXT (制表符分割文本文件) Json xml Excel 目前来看,Exce ...
- [开发笔记]GCC 分支预测优化
#define likely(x) __builtin_expect(!!(x),1)#define unlikely(x) __builtin_expect(!!(x),0) 用于优化在做分支判断的 ...
- Div Vertical Menu ver5
这个小功能,如果是算此次,已经是第5次修改了.可以从这里看到前4次:V1, http://www.cnblogs.com/insus/archive/2011/10/17/2215637.html V ...
- 纯javaScript、jQuery实现个性化图片轮播
纯javaScript实现个性化图片轮播 轮播原理说明<如上图所示>: 1. 画布部分(可视区域)属性说明:overflow:hidden使得超出画布部分隐藏或说不可见.position: ...
- 【干货分享】流程DEMO-加班与调休
流程名: 加班.调休 业务描述: 加班: 工作日加班可以申请调休,也可以申请支付加班费.原则上都应申请调休:周末加班原则上申请调休:法定节假日加班支付加班费. 加班申请以半小时为单位. 当月加班不能 ...
- SSH免手动输入密码和设置代理
通过使用sshpass将密码写入命令里,直接执行,免去手动密码输入的步骤命令如下: sshpass -p password_abc ssh user_abc@ssh_host -p ssh_port ...
- Photoshop、Illustrator思维导图笔记
半年前学习Photoshop时记得的思维导图笔记,可能不是很全,常用的基本都记下了.
- mono3.2和monodevelop4.0在ubuntu12.04上两天的苦战
首先第一步是设置ubuntu server 12.04版更新源,推荐中科大的比较快:deb http://debian.ustc.edu.cn/ubuntu/ precise main multive ...
- MonoDevelop 4.2.2/Mono 3.4.0 in CentOS 6.5 安装笔记
MonoDevelop 4.2.2/Mono 3.4.0 in CentOS 6.5 安装笔记 说明 以root账户登录Linux操作系统,注意:本文中的所有命令行前面的 #> 表示命令行提示符 ...
- 认识ASP.NET 5项目结构和项目文件xproj
ASP.NET 5 在项目结构上做了很大的改变,我们以前熟悉的目录结构与项目文件内容都不太一样了,本篇文章带大家了解 ASP.NET 5 到底跟以前有哪些不一样的地方. 我们先用 Visual Stu ...