mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-04 07:06:36 +08:00
autoresearch iter 16: add batch user lookup and use it for log enrichment
enrichAccessLogsWithUsers preferred the UserService contract over the local repository — correct layering, but it looped GetUserByID and issued up to a page-size worth of separate SELECTs against w_users, while the single-query WHERE id IN variant was only reached in the no-contract fallback branch. Give the contract a GetUsersByIDs so callers can keep the layering and drop the N+1. The test asserts 1 query batched against 3 per-id, so the counting itself is checked.
This commit is contained in:
@@ -52,6 +52,18 @@ func GetUserByID(ctx context.Context, id uint64) (*User, error) {
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
// GetUsersByIDs 一次性批量获取多个用户,避免调用方按 ID 逐条查询。
|
||||
func GetUsersByIDs(ctx context.Context, ids []uint64) ([]User, error) {
|
||||
if len(ids) == 0 {
|
||||
return []User{}, nil
|
||||
}
|
||||
var users []User
|
||||
if err := getDB(ctx).Where("id IN ?", ids).Find(&users).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
|
||||
// GetUserByUsername 通过用户名获取用户
|
||||
func GetUserByUsername(ctx context.Context, username string) (*User, error) {
|
||||
var u User
|
||||
|
||||
Reference in New Issue
Block a user