示例#1
0
        private async Task TruncateCacheAsync()
        {
            // Shall not truncate cache while reloading data.
            if (_cacheInitalizationService.InitializationState != CacheInitializationState.Idle)
            {
                return;
            }

            SlotType activeSlot = await _redisCacheService.GetActiveSlotAsync(_marketType);

            var tasks = new List <Task>();

            foreach (var assetId in _historyRepository.GetStoredAssetPairs())
            {
                foreach (var priceType in Constants.StoredPriceTypes)
                {
                    foreach (var timeInterval in Constants.InitFromDbIntervals)
                    {
                        int candlesAmount = _amountOfCandlesToStore[timeInterval];

                        var intervals = _redisCacheService.GetRedisCacheIntervals(timeInterval);

                        foreach (var interval in intervals)
                        {
                            tasks.Add(_redisCacheService.TruncateCacheAsync(assetId, priceType, interval, GetIntervalCandlesAmount(candlesAmount, interval), activeSlot));
                        }
                    }
                }
            }

            await Task.WhenAll(tasks);
        }
示例#2
0
        private async Task CacheAssetPairCandlesAsync(AssetPair assetPair, DateTime now, SlotType slotType)
        {
            try
            {
                _log.Info(nameof(InitializeCacheAsync), $"Caching {assetPair.Id} candles history...");

                foreach (CandlePriceType priceType in Constants.StoredPriceTypes)
                {
                    foreach (CandleTimeInterval timeInterval in Constants.InitFromDbIntervals)
                    {
                        DateTime alignedToDate   = now.TruncateTo(timeInterval).AddIntervalTicks(1, timeInterval);
                        DateTime alignedFromDate = alignedToDate.AddIntervalTicks(-_amountOfCandlesToStore[timeInterval] - 1, timeInterval);

                        if (alignedFromDate < _minDate)
                        {
                            alignedFromDate = _minDate.TruncateTo(timeInterval);
                        }

                        ICandle[] candles = (await _candlesHistoryRepository.GetCandlesAsync(assetPair.Id, timeInterval, priceType, alignedFromDate, alignedToDate)).ToArray();

                        if (!candles.Any())
                        {
                            continue;
                        }

                        var intervals = _candlesCacheService.GetRedisCacheIntervals(timeInterval);

                        foreach (var interval in intervals)
                        {
                            ICandle[] intervalCandles = interval != timeInterval
                                ? CandlesMerger.MergeIntoBiggerIntervals(candles, interval).ToArray()
                                : candles;

                            await _candlesCacheService.InitializeAsync(assetPair.Id, priceType, interval, intervalCandles, slotType);
                        }
                    }
                }

                _log.Info($"{assetPair.Id} candles history is cached.");
            }
            catch (Exception ex)
            {
                _log.Error(nameof(CacheAssetPairCandlesAsync), ex);
            }
        }