弄技术要弄通-公司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 6378 redis 127.0.0.1:6378> MONITOR OK 1321312790.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.php function 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.php get hello, world! from chan-1 #newly displayed get hello, world2! FROM chan-2 #newly displayed get hello, world! from chan-1 #newly displayed get 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 6378 redis 127.0.0.1:6378> MONITOR OK 1321313546.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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2015-01-31 apache RewriteCond RewriteRule