public async Task <int?> GetCurrentActivityPurchaseCount() { int?result = null; string key = GetActivityKey(); var dataInCache = await _counter.GetAsync <int>(new List <string>() { key }); if (dataInCache.Success) { if (dataInCache.Value != null && dataInCache.Value.Any()) { result = dataInCache.Value.First().Value; } else { using (var zlock = new ZooKeeperLock(key)) { if (await zlock.WaitAsync(1500)) { var retryGet = await _counter.GetAsync <int>(new List <string>() { key }); if (retryGet.Success && retryGet.Value != null && retryGet.Value.Any()) { result = retryGet.Value.First().Value; } else { var orderIds = await DalFlashSale.SelectFlashSaleOrderIdsByActivityAsync(_activityId, _round.StartTime, _round.EndTime); var count = 0; if (orderIds != null && orderIds.Any()) { var orderStatus = await DalFlashSale.SelectOrderStatusByOrderIdsAsync(orderIds); count = orderStatus.Where(o => !string.Equals(o.Value, "7Canceled")).Count(); } var increResult = await _counter.IncrementAsync(key, count); if (increResult.Success) { if (increResult.Value > count) { bool decResult = await RetryDecrement(key, count); } else { result = count; } } else { _logger.Error($"计数器设置失败, activity: {_activityId}, {dataInCache.Message}", dataInCache.Exception); } } } } } } else { _logger.Error($"计数器获取失败, {dataInCache.Message}", dataInCache.Exception); } return(result); }
public async Task <int?> GetCurrentActivityPurchaseCount() { int?result = null; var sw = new Stopwatch(); sw.Start(); long getCountCache = 0; long beforeLockTime = 0; long inLockTime = 0; long selectFlashSaleOrderIdsByActivityTime = 0; long selectOrderStatusByOrderIdsTime = 0; string key = GetActivityKey(); var dataInCache = await _counter.GetAsync <int>(new List <string>() { key }); getCountCache = sw.ElapsedMilliseconds; if (dataInCache.Success) { if (dataInCache.Value != null && dataInCache.Value.Any()) { result = dataInCache.Value.First().Value; } else { beforeLockTime = sw.ElapsedMilliseconds - getCountCache; using (var zlock = new ZooKeeperLock(key)) { if (await zlock.WaitAsync(1500)) { inLockTime = sw.ElapsedMilliseconds - beforeLockTime; var retryGet = await _counter.GetAsync <int>(new List <string>() { key }); if (retryGet.Success && retryGet.Value != null && retryGet.Value.Any()) { result = retryGet.Value.First().Value; } else { var orderIds = await DalFlashSale.SelectFlashSaleOrderIdsByActivityAsync(_activityId, _round.StartTime, _round.EndTime); selectFlashSaleOrderIdsByActivityTime = sw.ElapsedMilliseconds - inLockTime; var count = 0; if (orderIds != null && orderIds.Any()) { var orderStatus = await DalFlashSale.SelectOrderStatusByOrderIdsAsync(orderIds); selectOrderStatusByOrderIdsTime = sw.ElapsedMilliseconds - selectFlashSaleOrderIdsByActivityTime; count = orderStatus.Where(o => !string.Equals(o.Value, "7Canceled")).Count(); } var increResult = await _counter.IncrementAsync(key, count); if (increResult.Success) { if (increResult.Value > count) { bool decResult = await RetryDecrement(key, count); } else { result = count; } } else { _logger.Error($"计数器设置失败, activity: {_activityId}, {dataInCache.Message}", dataInCache.Exception); } } } } } } else { _logger.Error($"计数器获取失败, {dataInCache.Message}", dataInCache.Exception); } _logger.Info($"GetCurrentActivityPurchaseCount=>getCountCache:{getCountCache}," + $"beforeLockTime:{beforeLockTime},inLockTime:{inLockTime}," + $"selectFlashSaleOrderIdsByActivityTime:{selectFlashSaleOrderIdsByActivityTime}," + $"selectOrderStatusByOrderIdsTime:{selectOrderStatusByOrderIdsTime}," + $"total:{sw.ElapsedMilliseconds}"); sw.Stop(); return(result); }