【调整】优化redis仓储,实现LockTake/LockRelease锁处理并发问题。

This commit is contained in:
大灰灰
2022-09-15 01:28:34 +08:00
parent a3630fecc0
commit 5494608f7a
7 changed files with 478 additions and 90 deletions

View File

@@ -13,6 +13,7 @@ using System;
using System.Linq;
using System.Threading.Tasks;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.Caching.AutoMate.RedisCache;
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
@@ -47,6 +48,7 @@ namespace CoreCms.Net.Web.WebApi.Controllers
private readonly ICoreCmsGoodsServices _goodsServices;
private readonly ICoreCmsStoreServices _storeServices;
private readonly ICoreCmsOrderDistributionModelServices _orderDistributionModelServices;
private readonly IRedisOperationRepository _redisOperationRepository;
@@ -59,7 +61,7 @@ namespace CoreCms.Net.Web.WebApi.Controllers
, ICoreCmsSettingServices settingServices
, ICoreCmsAreaServices areaServices
, ICoreCmsBillReshipServices reshipServices, ICoreCmsShipServices shipServices
, ICoreCmsBillDeliveryServices billDeliveryServices, ICoreCmsLogisticsServices logisticsServices, ICoreCmsGoodsServices goodsServices, ICoreCmsStoreServices storeServices, ICoreCmsOrderDistributionModelServices orderDistributionModelServices)
, ICoreCmsBillDeliveryServices billDeliveryServices, ICoreCmsLogisticsServices logisticsServices, ICoreCmsGoodsServices goodsServices, ICoreCmsStoreServices storeServices, ICoreCmsOrderDistributionModelServices orderDistributionModelServices, IRedisOperationRepository redisOperationRepository)
{
_user = user;
_orderServices = orderServices;
@@ -73,6 +75,7 @@ namespace CoreCms.Net.Web.WebApi.Controllers
_goodsServices = goodsServices;
_storeServices = storeServices;
_orderDistributionModelServices = orderDistributionModelServices;
_redisOperationRepository = redisOperationRepository;
}
@@ -155,53 +158,81 @@ namespace CoreCms.Net.Web.WebApi.Controllers
{
var jm = new WebApiCallBack();
var type = entity.receiptType;
if (type == (int)GlobalEnumVars.OrderReceiptType.Logistics || type == (int)GlobalEnumVars.OrderReceiptType.IntraCityService)
var lockKey = "LOCK_CreateOrder:user_" + _user.ID;
var redisUserLock = await _redisOperationRepository.LockTakeAsync(lockKey, _user.ID.ToString(), TimeSpan.FromSeconds(10));
if (redisUserLock)
{
//收货地址id
if (entity.ushipId == 0)
try
{
jm.data = 13001;
jm.msg = GlobalErrorCodeVars.Code13001;
var type = entity.receiptType;
switch (type)
{
case (int)GlobalEnumVars.OrderReceiptType.Logistics:
case (int)GlobalEnumVars.OrderReceiptType.IntraCityService:
{
//收货地址id
if (entity.ushipId == 0)
{
jm.data = 13001;
jm.msg = GlobalErrorCodeVars.Code13001;
}
break;
}
case (int)GlobalEnumVars.OrderReceiptType.SelfDelivery:
{
//提货门店
if (entity.storeId == 0)
{
jm.data = 13001;
jm.msg = GlobalErrorCodeVars.Code13001;
}
//提货人姓名 提货人电话
if (string.IsNullOrEmpty(entity.ladingName))
{
jm.data = 13001;
jm.msg = "请输入姓名";
}
if (string.IsNullOrEmpty(entity.ladingMobile))
{
jm.data = 13001;
jm.msg = "请输入电话";
}
break;
}
default:
jm.data = 13001;
jm.msg = "未查询到配送方式";
break;
}
if (string.IsNullOrEmpty(entity.cartIds))
{
jm.data = 10000;
jm.msg = GlobalErrorCodeVars.Code10000;
}
jm = await _orderServices.ToAdd(_user.ID, entity.orderType, entity.cartIds, entity.receiptType,
entity.ushipId, entity.storeId, entity.ladingName, entity.ladingMobile, entity.memo,
entity.point, entity.couponCode, entity.source, entity.scene, entity.taxType, entity.taxName,
entity.taxCode, entity.objectId, entity.teamId, entity.requireOrder, entity.requiredFundType,
entity.traceId);
}
}
else if (type == (int)GlobalEnumVars.OrderReceiptType.SelfDelivery)
{
//提货门店
if (entity.storeId == 0)
catch (Exception e)
{
jm.data = 13001;
jm.msg = GlobalErrorCodeVars.Code13001;
jm.msg = "数据处理异常";
jm.otherData = e;
}
//提货人姓名 提货人电话
if (string.IsNullOrEmpty(entity.ladingName))
finally
{
jm.data = 13001;
jm.msg = "请输入姓名";
}
if (string.IsNullOrEmpty(entity.ladingMobile))
{
jm.data = 13001;
jm.msg = "请输入电话";
await _redisOperationRepository.LockReleaseAsync(lockKey, _user.ID.ToString());
}
}
else
{
jm.data = 13001;
jm.msg = "未查询到配送方式";
jm.msg = "当前请求太频繁_请稍后再试";
}
if (string.IsNullOrEmpty(entity.cartIds))
{
jm.data = 10000;
jm.msg = GlobalErrorCodeVars.Code10000;
}
jm = await _orderServices.ToAdd(_user.ID, entity.orderType, entity.cartIds, entity.receiptType,
entity.ushipId, entity.storeId, entity.ladingName, entity.ladingMobile, entity.memo,
entity.point, entity.couponCode, entity.source, entity.scene, entity.taxType, entity.taxName,
entity.taxCode, entity.objectId, entity.teamId, entity.requireOrder, entity.requiredFundType, entity.traceId);
//jm.otherData = entity;
return jm;
}
#endregion