Laravel5.8 的 Redis 异步队列耗时比较高
环境
centos7
php7.2.21
redis3.2.10
代码
控制器
public function setLoginLog($userInfo, $header, $ip, $appId)
{
try {
LoginLog::dispatch($userInfo, $header, $ip, $appId);
} catch (\Exception $e) {
Log::error('登录日志队列异常', [$e->getLine(), $e->getFile(), $e->getMessage()]);
}
}
Jobs
class LoginLog implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $data;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($userInfo, $header, $ip, $appId)
{
$this->data = [
'app_id' => $appId,
'user_agent' => $header,
'account_id' => $userInfo['account_id'],
'workcode' => $userInfo['workcode'] ?? $userInfo['user']['workcode'],
'login_ip' => $ip,
'login_time' => date("Y-m-d H:i:s")
];
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$loginLogResponse = ir('log.login.add', $this->data);
if (!$loginLogResponse->ok()) {
Log::error('sso登录日志消费异常', [$this->data, $loginLogResponse->raw()]);
}
}
}
性能分析
基本上每个请求入队列都会消耗在大约20%左右,是不是有点太高了,本来想着用redis异步队列可以提高一些性能,这里还有优化空间吗?
跟redis服务器的通信是内网通信,网络延迟也不高。
不用把对象丢进去,另外你这个有外部网络请求