private async Task ClearCache(int tradePairId) { await CacheService.InvalidateAsync ( CacheKey.ExchangeTradeHistory(tradePairId), CacheKey.ExchangeOrderBook(tradePairId), //CacheKey.ExchangeStockChart(tradePairId, 30), CacheKey.ExchangeDepthChart(tradePairId), CacheKey.ApiMarketHistory(tradePairId, 24) ); }
private async Task <OrderBookModel> GetOrCacheOrderData(int tradePairId) { var cacheResult = await CacheService.GetOrSetHybridAsync(CacheKey.ExchangeOrderBook(tradePairId), TimeSpan.FromMinutes(60), async() => { using (var context = ExchangeDataContextFactory.CreateReadOnlyContext()) { var orderbookData = await context.Trade .AsNoTracking() .Where(x => x.TradePairId == tradePairId && (x.Status == TradeStatus.Partial || x.Status == TradeStatus.Pending)) .GroupBy(x => new { x.Type, x.TradePair.Currency1.Symbol, x.Rate }) .Select(x => new OrderData { Symbol = x.Key.Symbol, Rate = x.Key.Rate, Amount = x.Sum(c => c.Remaining), Type = x.Key.Type }).ToListNoLockAsync().ConfigureAwait(false); var buyResults = new List <string[]>(); foreach (var trade in orderbookData.Where(x => x.Type == TradeHistoryType.Buy).OrderByDescending(x => x.Rate)) { buyResults.Add(new[] { "", trade.Rate.ToString("F8"), trade.Amount.ToString("F8"), (trade.Amount *trade.Rate).ToString("F8"), "" }); } var sellResults = new List <string[]>(); foreach (var trade in orderbookData.Where(x => x.Type == TradeHistoryType.Sell).OrderBy(x => x.Rate)) { sellResults.Add(new[] { "", trade.Rate.ToString("F8"), trade.Amount.ToString("F8"), (trade.Amount *trade.Rate).ToString("F8"), "" }); } return(new OrderBookModel { SellData = sellResults, BuyData = buyResults }); } }).ConfigureAwait(false); return(cacheResult); }