Skip to content

Commit

Permalink
perf: 优化接龙接口,加上事务锁
Browse files Browse the repository at this point in the history
  • Loading branch information
wfs committed Mar 12, 2024
1 parent f6b006b commit b254fd5
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions app/Http/Controllers/Api/DialogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2180,35 +2180,39 @@ public function msg__wordchain()
return Base::retError('内容最大不能超过200000字');
}
//
$userid = $user->userid;
if ($uuid) {
$dialogMsg = WebSocketDialogMsg::whereDialogId($dialog_id)
->whereType('word-chain')
->orderByDesc('created_at')
->where('msg', 'like', "%$uuid%")
->value('msg');
$list = array_reverse(array_merge($dialogMsg['list'] ?? [], $list));
$list = array_reduce($list, function ($result, $item) {
$fieldValue = $item['id']; // 指定字段名
if (!isset($result[$fieldValue])) {
$result[$fieldValue] = $item;
}
return $result;
}, []);
$list = array_reverse(array_values($list));
}
//
usort($list, function($a, $b) {
return $a['id'] - $b['id'];
return AbstractModel::transaction(function () use ($user, $uuid, $dialog_id, $list, $text) {
if ($uuid) {
$dialogMsg = WebSocketDialogMsg::whereDialogId($dialog_id)
->lockForUpdate()
->whereType('word-chain')
->orderByDesc('created_at')
->where('msg', 'like', "%$uuid%")
->value('msg');
$list = array_reverse(array_merge($dialogMsg['list'] ?? [], $list));
$list = array_reduce($list, function ($result, $item) {
$fieldValue = $item['id']; // 指定字段名
if (!isset($result[$fieldValue])) {
$result[$fieldValue] = $item;
}
return $result;
}, []);
$list = array_reverse(array_values($list));
} else {
$uuid = Base::generatePassword(36);
}
//
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);
}

/**
Expand Down

0 comments on commit b254fd5

Please sign in to comment.