异步Redis
建议先阅读官方文档异步Redis客户端章节
1.编译安装hiredis库
wget https://github.com/redis/hiredis/archive/v0.13.3.tar.gz
tar -zxf v0.13.3.tar.gz
cd hiredis-0.13.3
make -j
make install
2.重新编译swoole扩展,编译swoole时,在configure
指令中加入--enable-async-redis
cd swoole-2.2.0
./configure --enable-async-redis
make clean
make -j
make install
查看是否编译成功
php --ri swoole
出现此条表示编译正确async redis client => enabled
注意:
执行php-m发现swoole消失或者是通过php --ri swoole没有显示async redis client,或者报错,做一下操作
vi ~/.bash_profile 在最后一行添加 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib source ~/.bash_profile
示例异步redis发布订阅模式
<?php
$options['timeout'] = 1.5;
$redis=new swoole\redis($options);
$redis->on('close',function ($redis){
});
//redis发布订阅
$redis->on('message',function ($redis,$message){
//var_dump($message);
//订单处理
//入库操作
//异步的守护进程
});
$redis->connect('127.0.0.1',6379,function ($redis,$res){
if($res==false){
echo $redis->errMsg;
}
$redis->subscribe('test'); //频道订阅不阻塞
echo '异步';
});
资料
Redis 发布订阅 (subscribe/psubscribe的区别在于是否可以模糊匹配)
redis 订阅(subscribe/psubscribe)和发布 (publish)
附录:安装redis服务器
1 EPEL源安装 这个软件包会自动配置yum的软件仓库, 参见:EPEL源-是什么?为什么安装?
2 yum 安装redis
$ yum install redis
3 配置密码
Redis的配置文件默认在/etc/redis.conf
,找到如下行:
#requirepass myPassword
去掉前面的注释,并修改为所需要的密码:
requirepass myPassword
(其中myPassword就是要设置的密码)
Last updated
Was this helpful?