添加耗时

This commit is contained in:
2025-07-03 13:53:29 +08:00
parent 38ddeafad5
commit 27a5bb60ed
7 changed files with 224 additions and 33 deletions

View File

@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using NLog;
using PMSWPF.Data.Entities;
namespace PMSWPF.Data.Repositories;
@@ -9,6 +11,8 @@ namespace PMSWPF.Data.Repositories;
/// </summary>
public class UserRepository
{
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
/// <summary>
/// 根据ID获取用户
/// </summary>
@@ -16,9 +20,14 @@ public class UserRepository
/// <returns></returns>
public async Task<DbUser> GetByIdAsync(int id)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
return await _db.Queryable<DbUser>().In(id).SingleAsync();
var result = await _db.Queryable<DbUser>().In(id).SingleAsync();
stopwatch.Stop();
Logger.Info($"根据ID '{id}' 获取用户耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}
@@ -28,9 +37,14 @@ public class UserRepository
/// <returns></returns>
public async Task<List<DbUser>> GetAllAsync()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
return await _db.Queryable<DbUser>().ToListAsync();
var result = await _db.Queryable<DbUser>().ToListAsync();
stopwatch.Stop();
Logger.Info($"获取所有用户耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}
@@ -41,9 +55,14 @@ public class UserRepository
/// <returns></returns>
public async Task<int> AddAsync(DbUser user)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
return await _db.Insertable(user).ExecuteReturnIdentityAsync();
var result = await _db.Insertable(user).ExecuteReturnIdentityAsync();
stopwatch.Stop();
Logger.Info($"新增用户 '{user.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}
@@ -54,9 +73,14 @@ public class UserRepository
/// <returns></returns>
public async Task<int> UpdateAsync(DbUser user)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
return await _db.Updateable(user).ExecuteCommandAsync();
var result = await _db.Updateable(user).ExecuteCommandAsync();
stopwatch.Stop();
Logger.Info($"更新用户 '{user.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}
@@ -67,9 +91,14 @@ public class UserRepository
/// <returns></returns>
public async Task<int> DeleteAsync(int id)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
return await _db.Deleteable<DbUser>().In(id).ExecuteCommandAsync();
var result = await _db.Deleteable<DbUser>().In(id).ExecuteCommandAsync();
stopwatch.Stop();
Logger.Info($"删除用户ID '{id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}
}