public async Task <ApiUserTradeHistoryResponse> GetUserTradeHistory(ApiUserTradeHistoryRequest request) { var count = Math.Min(request.Count ?? 100, 1000); if (request.TradePairId.HasValue || !string.IsNullOrEmpty(request.Market)) { var tradepair = request.TradePairId.HasValue ? await TradePairReader.GetTradePair(request.TradePairId.Value, true).ConfigureAwait(false) : await TradePairReader.GetTradePair(request.Market.Replace('/', '_'), true).ConfigureAwait(false); if (tradepair == null) { return new ApiUserTradeHistoryResponse { Success = false, Error = "Market not found." } } ; var cacheResult = await CacheService.GetOrSetMemoryAsync(CacheKey.ApiUserTradeHistory(request.UserId.ToString(), tradepair.TradePairId), TimeSpan.FromSeconds(1), async() => { using (var context = ExchangeDataContextFactory.CreateReadOnlyContext()) { var history = context.TradeHistory .AsNoTracking() .Where(x => x.TradePairId == tradepair.TradePairId && (x.UserId == request.UserId || x.ToUserId == request.UserId)) .OrderByDescending(x => x.Id) .Take(1000) .Select(x => new ApiTradeHistory { Market = string.Concat(x.TradePair.Currency1.Symbol, "/", x.TradePair.Currency2.Symbol), TradeId = x.Id, Amount = x.Amount, Rate = x.Rate, Type = x.UserId == request.UserId ? TradeHistoryType.Buy.ToString() : TradeHistoryType.Sell.ToString(), TimeStamp = x.Timestamp, TradePairId = x.TradePairId, Fee = x.Fee }); return(await history.ToListNoLockAsync().ConfigureAwait(false)); } }).ConfigureAwait(false); return(new ApiUserTradeHistoryResponse { Success = true, Data = cacheResult.Take(count).ToList() }); } else { var cacheResult = await CacheService.GetOrSetHybridAsync(CacheKey.ApiUserTradeHistory(request.UserId.ToString()), TimeSpan.FromSeconds(20), async() => { using (var context = ExchangeDataContextFactory.CreateReadOnlyContext()) { var history = context.TradeHistory .AsNoTracking() .Where(x => x.UserId == request.UserId || x.ToUserId == request.UserId) .OrderByDescending(x => x.Id) .Take(1000) .Select(x => new ApiTradeHistory { Market = string.Concat(x.TradePair.Currency1.Symbol, "/", x.TradePair.Currency2.Symbol), TradeId = x.Id, Amount = x.Amount, Rate = x.Rate, Type = x.UserId == request.UserId ? TradeHistoryType.Buy.ToString() : TradeHistoryType.Sell.ToString(), TimeStamp = x.Timestamp, TradePairId = x.TradePairId, Fee = x.Fee }); return(await history.ToListNoLockAsync().ConfigureAwait(false)); } }).ConfigureAwait(false); return(new ApiUserTradeHistoryResponse { Success = true, Data = cacheResult.Take(count).ToList() }); } }