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.

  

  Opening the second client to subscribe this channel,and you will find something similar with the above example.

  

  publish a new message to the news-nba,it returns 2 meaning ...(you understand it)

  

  the both subscribers' client are as follows:

  

  

  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-*

  Let's publish some message to the others channel started with news- to find out the changes.
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.

  The client of Redis can not show you the unsubscription.So turning to the StackExchange.Redis.The

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");
  Debuging the code ,you will see the result as follow. 

  

  The next post of this series is the basic opreation of transaction in Redis.Thanks for your reading.

Basic Tutorials of Redis(7) -Publish and Subscribe的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. dotnet core use Redis to publish and subscribe

    安装Redis 同样我这边再次使用Docker, 方便快捷: # 拉取镜像 docker pull redis # 运行镜像 docker run -d -p 6379:6379 --name red ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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#? ...

  9. 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 ...

随机推荐

  1. C#多线程之线程池篇1

    在C#多线程之线程池篇中,我们将学习多线程访问共享资源的一些通用的技术,我们将学习到以下知识点: 在线程池中调用委托 在线程池中执行异步操作 线程池和并行度 实现取消选项 使用等待句柄和超时 使用计时 ...

  2. PHP之Memcache缓存详解

         Mem:memory缩写(内存):内存缓存 1.  断电或者重启服务器内存数据即消失,即临时数据: Memcache默认端口:11211 存入方式:key=>>value    ...

  3. 《月之猎人 (Moon Hunters)》主角设计

    原文链接 游戏开发人员,你们好! 我是 Kitfox Games 工作室的总监 Tanya,我们的工作室位于加拿大的蒙特利尔,拥有六名员工. 我们 3 月份发布了<月之猎人>游戏的桌面版, ...

  4. Lesson 19 Sold out

    Text 'The play may begin at any moment,' I said. 'It may have begun already,' Susan answered. I hurr ...

  5. C++的性能C#的产能?! - .Net Native 系列《三》:.NET Native部署测试方案及样例

    之前一文<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATIVE初窥> 获得很多朋友支持和鼓励,也更让我坚定做这项技术的推广者,希望能让更多的朋友了解这项技术,于是先从官方 ...

  6. ASP.NET MVC Model元数据(二)

    ASP.NET MVC Model元数据(二) 前言 在上篇中,给大家留个对Model元数据的印象,并没有对Model元数据有过多的讲解,而在本篇中也不会对Model元数据的本身来解释,而是针对于它的 ...

  7. 新思想、新技术、新架构——更好更快的开发现代ASP.NET应用程序

    在博客园学习很长时间了,今天终于自己也开通了博客,准备分享一些感悟和经验.首先感谢博客园园主提供了这么好的程序员学习交流平台,也非常感谢张善友.dax.net.netfocus.司徒正美 等技术大牛的 ...

  8. [转]Fiddler抓取Android真机上的HTTPS包

    此篇文章转载自:http://blog.csdn.net/roland_sun/article/details/30078353 工作中经常会需要对一些app进行抓包, 但是每次默认都是只抓http请 ...

  9. SpringMVC+MyBatis整合——事务管理

    项目一直没有做事务管理,这几天一直在想着解决这事,今天早上终于解决了.接下来直接上配置步骤. 我们项目采用的基本搭建环境:SpringMVC.MyBatis.Oracle11g.WebLogic10. ...

  10. WCF学习之旅—第三个示例之二(二十八)

    上接WCF学习之旅—第三个示例之一(二十七) 五.在项目BookMgr.Model创建实体类数据 第一步,安装Entity Framework 1)  使用NuGet下载最新版的Entity Fram ...