1.项目代码中程序
<?php
namespace Api\Controller;
use Think\Controller;
class TestController extends Controller {
/**
* redis发布消息
*/
public function redis_publish(){
$redis = new \Redis();
$redis->connect('127.0.0.1',6379);
$key = 'answer_train_test_id_1418';
$redis->publish('answer_list',$key);
return true;
}
}
2.写个redis_subscribe.php脚本文件并运行
<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
$redis->subscribe(['answer_list'], 'callback');
//回调函数,这里写处理逻辑
function callback($instance, $channelName, $message) {
echo $channelName, "==>", $message,PHP_EOL;
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
$get_data = $redis->get($message);
$link = mysqli_connect('127.0.0.1','root','root','yichen');
$str_sql = "INSERT INTO `pg_action_log` (`uid`, `model`, `controller`, `action`, `content`, `client_ip`, `add_time`) VALUES ('0', NULL, NULL, NULL, '".$get_data."', NULL, NULL);";
mysqli_query($link, $str_sql);
mysqli_close($link);
}
?>