弄技术要弄通-公司reis的pub/sub怎么使用的呢?
Pub/Sub in Redis using PHP
I would like to put an example together about the pub/sub using php in Redis; there is only API documentation available in phpredis, the PHP client I am using (http://redis.io/clients).
0. Setup
First setup a Redis Server. I set up a Redis server in my local box using port 6378 (myredisserver.test.com:6378).
1. Publish: push a message to a channel.
This part is relatively easy. The following is a php script “publish.php“.
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php //publish.php $redis = new Redis(); $redis->pconnect('myredisserver.test.com',6378); $redis->publish('chan-1', 'hello, world!'); // send message to channel 1. $redis->publish('chan-2', 'hello, world2!'); // send message to channel 2. print "\n"; $redis->close();?> |
1.5 Checkpoint: Monitor in Redis server.
Let’s take a break now and see what will happen if we run the script “publish.php” from a client side.
Because I use a non default port, “-p” option is used with “redis-cli” command.
Open a new terminal at Redis Server, and issue “MONITOR” command in redis “console”:
|
1
2
3
4
5
6
7
|
%redis-cli -p 6378redis 127.0.0.1:6378> MONITOROK1321312790.866271 "MONITOR"1321312792.221599 "PING"1321312796.330376 "PUBLISH" "chan-1" "hello, world!" # after run "publish.php"1321312796.330482 "PUBLISH" "chan-2" "hello, world2!" # after run "publish.php" |
2. Subscribe: Listen to a channel (or some channels).
Here is the complete php script “subscribe.php” after I did some debugging. Thanks to the info here: https://github.com/nicolasff/phpredis/issues/36.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<?php//subscribe.phpfunction f($redis, $chan, $msg) { switch($chan) { case 'chan-1': print "get $msg from $chan\n"; break; case 'chan-2': print "get $msg FROM $chan\n"; break; case 'chan-3': break; }}ini_set('default_socket_timeout', -1);$redis = new Redis();$redis->pconnect('myredisserver.test.com',6378);$redis->subscribe(array('chan-1', 'chan-2', 'chan-3'), 'f');print "\n";?> |
2.5
1) run script “subscribe.php“:
|
1
|
%php subscribe.php |
of course, nothing happens.
2) In another terminal window, run script “publish.php” twice, and come back to have a look!
|
1
2
3
4
5
|
%php subscribe.phpget hello, world! from chan-1 #newly displayedget hello, world2! FROM chan-2 #newly displayedget hello, world! from chan-1 #newly displayedget hello, world2! FROM chan-2 #newly displayed |
3. Breakpoint: Revisit the MINTOR window in Redis Server:
|
1
2
3
4
5
6
7
8
9
10
11
|
%redis-cli -p 6378redis 127.0.0.1:6378> MONITOROK1321313546.882528 "MONITOR"1321313552.848569 "PING"1321313553.458541 "SUBSCRIBE" "chan-1" "chan-2" "chan-3"1321313556.223800 "PUBLISH" "chan-1" "hello, world!"1321313556.223862 "PUBLISH" "chan-2" "hello, world2!"1321313557.597914 "PUBLISH" "chan-1" "hello, world!"1321313557.598061 "PUBLISH" "chan-2" "hello, world2!"1321313562.851878 "PING" |
4. Test with multiple publishers and multiple subscribers!
Reference: http://robots.thoughtbot.com/post/6325247416/redis-pub-sub-how-does-it-work
弄技术要弄通-公司reis的pub/sub怎么使用的呢?的更多相关文章
- 打工心态废掉了很多人,包括你吗?(你把现在这家公司的业务都弄清楚、弄懂了吗?君子报仇十年不晚!不离不弃!)good
我只拿这点钱,凭什么去做那么多工作,我傻呀. 我为公司干活,公司付我一份报酬,等价交换而已,我不欠谁的. 我只要对得起这份薪水就行了,多一点我都不干,做了也白做. 工作嘛,又不是为自己干,说得过去就行 ...
- 互联网技术笔试总通不过?leetcode刷对了么
https://36kr.com/p/5084645 Leetcode,绕都绕不过去的程序员刷题神器 编者按:本文来自逆行求职(ID:nixingjihua). 对所有求职技术岗位的童鞋来说,有这么一 ...
- 泛泰A820L (高通公司MSM8660 cpu) 3.4内核CM10.1(Android 4.2.2) 测试版第二版
欢迎关注泛泰非盈利专业第三方开发团队 VegaDevTeam (本team 由 syhost suky zhaochengw(z大) xuefy(大星星) tenfar(R大师) loogeo cr ...
- Nubia Z5S(高通公司MSM8974) QHSUSB_BULK砖的方法节省模式(随着win7在恢复recovery分区案例)
Nubia Z5S在某些异常情况或按组合键进入QHSUSB_BULK状态, 这种模式的现象, 猜想windows(实例win7)即使在数据线, 它会出现在计算机n载,甚至会提示要格式化某些分区(这里要 ...
- 泛泰A860(高通公司8064 cpu 1080p) 拂4.4中国民营recovery TWRP2.7.1.2文本(通过刷第三版)
专业第三方开发团队 VegaDevTeam (本team 由 syhost suky zhaochengw(z大) xuefy(大星星) tenfar(R大师) loogeo crazyi(天下无雪 ...
- 高通公司 MSM8K GPT异常原因分析无法开机的问题
问题分析过程如下面: 一. MSM8916台gpt概率问题:采用QPST emmc software download下载软件工具后,无法开机.例如下面的附图: log分析是userdata分区未成功 ...
- pytorch加载数据的方法-没弄,打算弄
参考:https://www.jianshu.com/p/aee6a3d72014 # 网络,netg为生成器,netd为判别器 netg, netd = NetG(opt), NetD(opt) # ...
- 入坑IT十年(二)技术以外
上一篇博客里提到:技术越来越简单,发布后不久,就看到<技术并不是越来越简单>,这显然是打擂台来了. 技术究竟是不是越来越简单?其实这个问题,要看你究竟是以什么角度来思考这个问题.我们可以举 ...
- 手机CPU
说起手机CPU的历史,笔者给大家提一个问题:"世界上第一款智能手机是什么呢?"相信很多人的答案是爱立信的R380或诺基亚的7650,但都不对,真正的首款智能手机是由摩托罗拉在200 ...
随机推荐
- Mathematics-基础:斐波那契数列
f(1)=1 f(2)=1 f(n)=f(n-1)+f(n-2) n>2
- NodeJs运行服务器-day01
//读取内置模块http,这个模块开发服务器用的var http =require('http'); var server=http.createServer(function(req,res){ r ...
- python 05 关于对python中引用的理解
数据的在内存中的地址就是数据的引用. 如果两个变量为同一个引用,那么这两个变量对应的数据一定相同: 如果两个变量对应的数据相同,引用不一定相同. 通过id(数据)可以查看数据对应的地址,修改变量的值, ...
- 为什么JavaScript里面0.1+0.2 === 0.3是false
以下这一篇说明的很详细:彻底理解0.1 + 0.2 === 0.30000000000000004的背后 0.1+0.2 === 0.3 //返回是false, 这是为什么呢?? 我们知道浮点数计算是 ...
- fshc模块fsch2mcu_if理解
fshc2mcu_if中包括ahb2reg/ahb2fifo两个文件,都是协议转换文件.ahb2reg下游文件是reg files,ahb2fifo下游文件是fifo控制器.所有的配置和flag都是要 ...
- Python数据分析 Pandas模块 基础数据结构与简介(二)
重点方法 分组:groupby('列名') groupby(['列1'],['列2'........]) 分组步骤: (spiltting)拆分 按照一些规则将数据分为不同的组 (Applying)申 ...
- python_正则_re模块
正则表达式元字符: 字符匹配 : . :除换行符以外的任意单个字符 [] :指定范围内字符 [^] :指定范围外字符 次数匹配: * :任意次,0,1,多次 .* :任意字符 任意次 ? :至多1次或 ...
- 1. node.js环境搭建 第一行代码
一.NodeJs简介 NodeJS官网上的介绍: Node.js is a platform built on Chrome's JavaScript runtime for easily bui ...
- 用python做分布式定时器
分布式任务系统 (Python) github地址 https://github.com/thomashuang/Lilac/blob/master/README.rst 这里将介绍Liac的设计架构 ...
- 大数据学习——Linux上常用软件安装
4.1 Linux系统软件安装方式 Linux上的软件安装有以下几种常见方式: 1.二进制发布包 软件已经针对具体平台编译打包发布,只要解压,修改配置即可 2.RPM发布包 软件已经按照redhat的 ...