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. 将 instance 部署到 OVS Local Network - 每天5分钟玩转 OpenStack(130)

    上一节创建了 OVS 本地网络 first_local_net,今天我们会部署一个 instance 到该网络并分析网络结构.launch 一个 instance,选择 first_local_net ...

  2. ubuntu系统下如何修改host

    Ubuntu系统的Hosts只需修改/etc/hosts文件,在目录中还有一个hosts.conf文件,刚开始还以为只需要修改这个就可以了,结果发现是需要修改hosts.修改完之后要重启网络.具体过程 ...

  3. 从史上八大MySQL事故中学到的经验

    本文列举了史上八大MySQL宕机事件原因.影响以及人们从中学到的经验,文中用地震级数来类比宕机事件的严重性和后果,排在最严重层级前两位的是由于亚马逊AWS宕机故障(相当于地震十级和九级). 一.Per ...

  4. Tomcat之APR错误

    在发布Apache Tomcat的时候,突然出现如下错误: An incompatible version 1.1.31 of the APR based Apache Tomcat Native l ...

  5. [学习笔记]JavaScript之函数式编程

    欢迎指导与讨论:) 前言 函数式编程能使我们的代码结构变得简洁,让代码更接近于自然语言,易于理解. 一.减少不必要的函数嵌套代码 (1)当存在函数嵌套时,若内层函数的参数与外层函数的参数一致时,可以这 ...

  6. 【腾讯Bugly干货分享】Android性能优化典范——第6季

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/580d91208d80e49771f0a07c 导语 这里是Android性能优 ...

  7. 使用ENode框架前您需要了解的东西(初稿)

    选择ENode意味着什么可能很多人还不太清楚.我简单整理了一下: 意味着你选择了:你需要做DDD领域建模.选择了事件驱动的架构.选择了CQRS架构.选择了最终一致性.选择了事件溯源.选择了分布式.这些 ...

  8. MVVM大比拼之AngularJS源码精析

    MVVM大比拼之AngularJS源码精析 简介 AngularJS的学习资源已经非常非常多了,AngularJS基础请直接看官网文档.这里推荐几个深度学习的资料: AngularJS学习笔记 作者: ...

  9. Web API 入门指南 - 闲话安全

    Web API入门指南有些朋友回复问了些安全方面的问题,安全方面可以写的东西实在太多了,这里尽量围绕着Web API的安全性来展开,介绍一些安全的基本概念,常见安全隐患.相关的防御技巧以及Web AP ...

  10. 在rem布局下使用背景图片以及sprite

    现在移动端页面用rem布局已经是一大流派了,成熟的框架如淘宝的flexiable.js,以及我的好友@墨尘写的更轻量级的hotcss.用rem作单位使得元素能够自适应后,还有一块需要关注的,那就是背景 ...