分享 / 0 / 12 / 创建于 1年前
Auth 验证机制导致项目很卡 请问一下那个大神遇到过 并且又好的解决方能 给提一下 谢谢
详细说说哪里卡?怎么做的验证?
其实也不是卡,而是你这个,会创建数据库连接,然后从中获取指定的 Users 记录。所以如果你觉得慢,可能从另外一种角度来说,慢的时间是数据库连接的时间。
@小旭 不妨把日志加起来,包括sql的日志。我们一般的排查方法就是通过日志,在程序boot的时候挂一个钟表实例注册到 app 中,每隔一段代码就去问一下他,到目前为止过了多久了,并写到日志中。以下是挂钟的实现:
class WallTime { /** * 默认标记长度 */ const MARK_LENGTH_DEFAULT = 6; /** * 慢日志默认超时时间 */ const SLOW_LOG_TIMEOUT_DEFAULT = 3; /** * @var float 挂钟时间 */ protected $wallTime; /** * @var string 标识 */ protected $mark; /** * @var array 日志数据 */ protected $log = []; /** * 构造函数 * * @param int $markLength */ public function __construct(int $markLength = self::MARK_LENGTH_DEFAULT) { $this->wallTime = microtime(true); $this->mark = Str::random($markLength); } /** * 经过时间 * * @return float */ public function delay(): float { return round(microtime(true)-$this->wallTime,2); } /** * 获取标识 * * @return string */ public function mark(): string { return $this->mark; } /** * 记录日志 * * @param array $data */ public function info(array $data = []) { info($this->message(), $data); } /** * 慢日志 * * @param array $data * @param int $timeout */ public function slow(array $data = [], int $timeout = self::SLOW_LOG_TIMEOUT_DEFAULT) { $this->log[] = [ $this->message(), $data, ]; if ($this->delay() > $timeout) { foreach ($this->log as $item) { info($item[0],$item[1]); } $this->log = []; } } /** * 消息体 * * @return string */ protected function message(): string { $delay = $this->delay(); return "$this->mark\t$delay"; } }
然后通过 AppServiceProvider 注册到app中
AppServiceProvider
$this->app->instance('wallTime', new WallTime()); // 以下是给队列任务加的挂钟,用不到可以不加 Queue::before(function (JobProcessing $event) { $this->app->instance('wallTime', new WallTime()); });
定位程序中执行缓慢的位置
resolve('wallTime')->slow([__FILE__, __LINE__]);
SQL日志则可以添加查询事件的监听器:
public function handle(QueryExecuted $event) { // 监听查询执行事件,里面 $event->time 保存着查询执行的时间 }
可以先调查一下,究竟是程序耗时,还是数据库耗时,还是连接耗时。
加缓存
是你的auth写的很烂吧,我们用passport包就进来2条语句查询
telescope看下本地测试代码,是不是一个auth调用一次数据库
我要举报该,理由是:
详细说说哪里卡?怎么做的验证?
其实也不是卡,而是你这个,会创建数据库连接,然后从中获取指定的 Users 记录。所以如果你觉得慢,可能从另外一种角度来说,慢的时间是数据库连接的时间。
@小旭 不妨把日志加起来,包括sql的日志。我们一般的排查方法就是通过日志,在程序boot的时候挂一个钟表实例注册到 app 中,每隔一段代码就去问一下他,到目前为止过了多久了,并写到日志中。以下是挂钟的实现:
然后通过
AppServiceProvider
注册到app中定位程序中执行缓慢的位置
SQL日志则可以添加查询事件的监听器:
可以先调查一下,究竟是程序耗时,还是数据库耗时,还是连接耗时。
加缓存
是你的auth写的很烂吧,我们用passport包就进来2条语句查询
telescope看下本地测试代码,是不是一个auth调用一次数据库