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

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

View File

@ -2180,35 +2180,39 @@ 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)
->whereType('word-chain') ->lockForUpdate()
->orderByDesc('created_at') ->whereType('word-chain')
->where('msg', 'like', "%$uuid%") ->orderByDesc('created_at')
->value('msg'); ->where('msg', 'like', "%$uuid%")
$list = array_reverse(array_merge($dialogMsg['list'] ?? [], $list)); ->value('msg');
$list = array_reduce($list, function ($result, $item) { $list = array_reverse(array_merge($dialogMsg['list'] ?? [], $list));
$fieldValue = $item['id']; // 指定字段名 $list = array_reduce($list, function ($result, $item) {
if (!isset($result[$fieldValue])) { $fieldValue = $item['id']; // 指定字段名
$result[$fieldValue] = $item; if (!isset($result[$fieldValue])) {
} $result[$fieldValue] = $item;
return $result; }
}, []); return $result;
$list = array_reverse(array_values($list)); }, []);
} $list = array_reverse(array_values($list));
// } else {
usort($list, function($a, $b) { $uuid = Base::generatePassword(36);
return $a['id'] - $b['id']; }
//
usort($list, function ($a, $b) {
return $a['id'] - $b['id'];
});
//
$msgData = [
'text' => $text,
'list' => $list,
'userid' => $user->userid,
'uuid' => $uuid,
];
return WebSocketDialogMsg::sendMsg(null, $dialog_id, 'word-chain', $msgData, $user->userid);
}); });
//
$msgData = [
'text' => $text,
'list' => $list,
'userid' => $userid,
'uuid' => $uuid ?: Base::generatePassword(36),
];
return WebSocketDialogMsg::sendMsg(null, $dialog_id, 'word-chain', $msgData, $user->userid);
} }
/** /**