简单队列实战
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
$redis->connect( '127.0.0.1' , 6379); $strQueueName = 'Test_bihu_queue' ; //进队列 $redis->rpush($strQueueName, json_encode([ 'uid' => 1, 'name' => 'Job' ])); $redis->rpush($strQueueName, json_encode([ 'uid' => 2, 'name' => 'Tom' ])); $redis->rpush($strQueueName, json_encode([ 'uid' => 3, 'name' => 'John' ])); echo "---- 进队列成功 ---- <br /><br />" ; //查看队列 $strCount = $redis->lrange($strQueueName, 0, -1); echo "当前队列数据为: <br />" ; print_r($strCount); //出队列 $redis->lpop($strQueueName); echo "<br /><br /> ---- 出队列成功 ---- <br /><br />" ; //查看队列 $strCount = $redis->lrange($strQueueName, 0, -1); echo "当前队列数据为: <br />" ; print_r($strCount); |
简单发布订阅实战
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//以下是 pub.php 文件的内容 cli下运行 ini_set( 'default_socket_timeout' , -1); $redis->connect( '127.0.0.1' , 6379); $strChannel = 'Test_bihu_channel' ; //发布 $redis->publish($strChannel, "来自{$strChannel}频道的推送" ); echo "---- {$strChannel} ---- 频道消息推送成功~ <br/>" ; $redis->close(); //以下是 sub.php 文件内容 cli下运行 ini_set( 'default_socket_timeout' , -1); $redis->connect( '127.0.0.1' , 6379); $strChannel = 'Test_bihu_channel' ; //订阅 echo "---- 订阅{$strChannel}这个频道,等待消息推送...---- <br/><br/>" ; $redis->subscribe([$strChannel], 'callBackFun' ); function callBackFun($redis, $channel, $msg) { print_r([ 'redis' => $redis, 'channel' => $channel, 'msg' => $msg ]); } |