perf: 优化接龙接口,加上事务锁

This commit is contained in:
wfs 2024-03-12 23:49:05 +08:00
parent f6b006b000
commit b254fd5ce2

View File

@ -2180,9 +2180,10 @@ class DialogController extends AbstractController
return Base::retError('内容最大不能超过200000字'); return Base::retError('内容最大不能超过200000字');
} }
// //
$userid = $user->userid; return AbstractModel::transaction(function () use ($user, $uuid, $dialog_id, $list, $text) {
if ($uuid) { if ($uuid) {
$dialogMsg = WebSocketDialogMsg::whereDialogId($dialog_id) $dialogMsg = WebSocketDialogMsg::whereDialogId($dialog_id)
->lockForUpdate()
->whereType('word-chain') ->whereType('word-chain')
->orderByDesc('created_at') ->orderByDesc('created_at')
->where('msg', 'like', "%$uuid%") ->where('msg', 'like', "%$uuid%")
@ -2196,6 +2197,8 @@ class DialogController extends AbstractController
return $result; return $result;
}, []); }, []);
$list = array_reverse(array_values($list)); $list = array_reverse(array_values($list));
} else {
$uuid = Base::generatePassword(36);
} }
// //
usort($list, function ($a, $b) { usort($list, function ($a, $b) {
@ -2205,10 +2208,11 @@ class DialogController extends AbstractController
$msgData = [ $msgData = [
'text' => $text, 'text' => $text,
'list' => $list, 'list' => $list,
'userid' => $userid, 'userid' => $user->userid,
'uuid' => $uuid ?: Base::generatePassword(36), 'uuid' => $uuid,
]; ];
return WebSocketDialogMsg::sendMsg(null, $dialog_id, 'word-chain', $msgData, $user->userid); return WebSocketDialogMsg::sendMsg(null, $dialog_id, 'word-chain', $msgData, $user->userid);
});
} }
/** /**