优化接口的并发

This commit is contained in:
tongfei
2024-04-20 11:48:58 +08:00
parent 57acf1b699
commit 4f668a209b

View File

@@ -118,6 +118,15 @@ namespace WMS.Web.Domain.Services
/// <param name="loginInfo"></param>
/// <returns></returns>
public async Task<Result> Receive(UpdateInStockTaskRequest dto, LoginInDto loginInfo)
{
// 构建 Redis 锁的键名
string lockKey = $"lock:taskidreceive{dto.Id}";
// 构建 Redis 锁的值,用于标识锁的持有者
string lockValue = Guid.NewGuid().ToString();
// 获取 Redis 锁,设置超时时间为 10 秒
if (await _redisDb.LockTakeAsync(lockKey, lockValue, TimeSpan.FromSeconds(10)))
{
try
{
IDbContextTransaction _transaction = _basicsRepositories.GetTransaction();
bool isRollback = false;
@@ -132,6 +141,18 @@ namespace WMS.Web.Domain.Services
return Result.ReSuccess();
}
finally
{
// 释放 Redis 锁
await _redisDb.LockReleaseAsync(lockKey, lockValue);
}
}
else
{
return Result.ReFailure(ResultCodes.Concurrent_Instock);
}
}
/// <summary>
/// 上架-采购订单
@@ -144,7 +165,7 @@ namespace WMS.Web.Domain.Services
var taskId = instock.Details.Select(x => x.TaskId).FirstOrDefault();
// 构建 Redis 锁的键名
string lockKey = $"lock:taskid{taskId}";
string lockKey = $"lock:taskidshelf{taskId}";
// 构建 Redis 锁的值,用于标识锁的持有者
string lockValue = Guid.NewGuid().ToString();
// 获取 Redis 锁,设置超时时间为 10 秒
@@ -197,6 +218,15 @@ namespace WMS.Web.Domain.Services
/// <param name="loginInDto"></param>
/// <returns></returns>
public async Task<Result> ShelfNoPurchase(NoPurchaseShelfRequest dto, LoginInDto loginInDto)
{
// 构建 Redis 锁的键名
string lockKey = $"lock:taskidshelfnopurchase{dto.TaskId}";
// 构建 Redis 锁的值,用于标识锁的持有者
string lockValue = Guid.NewGuid().ToString();
// 获取 Redis 锁,设置超时时间为 10 秒
if (await _redisDb.LockTakeAsync(lockKey, lockValue, TimeSpan.FromSeconds(10)))
{
try
{
_logger.LogInformation($"非采购入库:{JsonConvert.SerializeObject(dto)} 操作人:{loginInDto.UserInfo.StaffId + loginInDto.UserInfo.Nickname}");
IDbContextTransaction _transaction = _basicsRepositories.GetTransaction();
@@ -211,6 +241,17 @@ namespace WMS.Web.Domain.Services
return shelfSave_result;
return Result.ReSuccess();
}
finally
{
// 释放 Redis 锁
await _redisDb.LockReleaseAsync(lockKey, lockValue);
}
}
else
{
return Result.ReFailure(ResultCodes.Concurrent_Instock);
}
}
/// <summary>
/// 获取任务单:根据箱号信息