private static int GetDbQtyBykeyType(Tuple <int, int, int> tupleSource, CounterKeyType keyType, string deviceId) { switch (keyType) { case CounterKeyType.UserIdKey: return(tupleSource.Item1); case CounterKeyType.DeviceIdKey: //设备号不存在情况下特殊处理 if (string.IsNullOrEmpty(deviceId)) { return(tupleSource.Item1); } else { return(tupleSource.Item2); } case CounterKeyType.UserTelKey: return(tupleSource.Item3); default: return(0); } }
public static async Task <SimpleResponseModel> RedisCounterRecord(string key, LimitType type, CounterKeyType keyType, OrderItems item, Guid userId = new Guid(), string deviceId = null, string userTel = null, List <string> pids = null, int dbQuantity = 0) { Logger.Info($"计数器Key=>{key}增加{item.Num}"); using (var client = CacheHelper.CreateCounterClient(ClientName, TimeSpan.FromDays(30))) { var recordCache = await client.CountAsync(key); //这里跟hashclient一点咯要区分开,这里key不存在会报错 double retryresult = 0; IResult <long> increment; var tupleReult = new Tuple <int, int, int>(0, -1, -1); var incrementNum = 0; var quantity = dbQuantity; if (!recordCache.Success) { if (recordCache.Message == "Key不存在") //暂时写死错误信息,后面如果底层针对这种情况有固定errorcode返回修改下 { if (dbQuantity == -1) { switch (type) { case LimitType.PlaceLimit: if (item.ActivityId != null) { tupleReult = await DalFlashSale.SelectOrderActivityProductRecordNumAsync( userId, deviceId, string.IsNullOrEmpty(item.AllPlaceLimitId)?item.ActivityId.Value : new Guid(item.AllPlaceLimitId), userTel, pids); quantity = GetDbQtyBykeyType(tupleReult, keyType, deviceId); } break; case LimitType.PersonalLimit: if (item.ActivityId != null) { tupleReult = await DalFlashSale.SelectOrderActivityProductRecordNumAsync(userId, deviceId, item.ActivityId.Value, userTel, new List <string> { item.PID }); quantity = GetDbQtyBykeyType(tupleReult, keyType, deviceId); } break; case LimitType.AllPlaceLimit: if (item.ActivityId != null) { tupleReult = await DalFlashSale.SelectAllPlaceOrderActivityProductRecordNumAsync( userId, deviceId, new Guid(item.AllPlaceLimitId), userTel, pids); quantity = GetDbQtyBykeyType(tupleReult, keyType, deviceId); } break; } } increment = await client.IncrementAsync(key, quantity + item.Num); incrementNum = quantity + item.Num; } else { Logger.Error($"redis计数记录失败=>CheckFlashSaleAsync{key}"); return(new SimpleResponseModel { Code = CheckFlashSaleStatus.CreateOrderFailed, Mesage = "计数器执行失败" }); } } else { increment = await client.IncrementAsync(key, item.Num); incrementNum = item.Num; } if (!increment.Success) //失败情况后续考虑 { retryresult = await RedisCountRetry(() => client.IncrementAsync(key, incrementNum)); if (retryresult < 0) { Logger.Error($"redis计数记录失败=>CheckFlashSaleAsync{key}"); return(new SimpleResponseModel { Code = CheckFlashSaleStatus.CreateOrderFailed, Mesage = "计数器执行失败" }); } } return(new SimpleResponseModel { Code = CheckFlashSaleStatus.Succeed, Record = (int)(increment.Success ? increment.Value : retryresult), Mesage = "成功", //DbQuantity = quantity, DeviceCount = tupleReult.Item2, TelCount = tupleReult.Item3 }); } }