1、系统需要做一个队列功能
2、目前使用php+redis实现,想了解下我当前的写法有没有逻辑问题,请各位大佬指点一二,代码如下:
<?php
# 设置永不超时
ini_set('default_socket_timeout', -1);
# 加载配置文件
$config = include('./config.php');
# 链接redis
$redis = new Redis();
$redis->connect($config['redis']['host'], $config['redis']['port']);
try{
while(true) {
if($redis->ping() == 0){
$redis->connect($config['redis']['host'], $config['redis']['port']);
//查看服务是否运行
echo "重新运行Server is running: " . $redis->ping()."\r\n";
}
$result = $redis->brpop($config['task_msg_key']);
var_dump($result);
}
}catch (Exception $e) {
echo $e->getMessage(); // 返回自定义的异常信息
}
BRPOPLPUSH 用这个命令好点, 防止程序挂了, 队列数据丢失.
如果想用redis当队列, redis版本支持的话, 推荐用 stream 系列命令. 这个redis中的消息队列.
https://redis.io/commands/?group=stream