Example #1
0
        public async Task UpdateTradingInstrumentsCacheAsync(string id = null)
        {
            await _log.WriteInfoAsync(nameof(UpdateTradingInstrumentsCacheAsync), nameof(TradingInstrumentsManager),
                                      $"Started {nameof(UpdateTradingInstrumentsCacheAsync)}");

            var count = 0;

            if (string.IsNullOrEmpty(id))
            {
                var instruments = (await _tradingInstrumentsApi.List(string.Empty))?
                                  .Select(i => _convertService.Convert <TradingInstrumentContract, TradingInstrument>(i))
                                  .ToDictionary(x => x.GetKey());

                if (instruments != null)
                {
                    _tradingInstrumentsCacheService.InitCache(instruments.Values.Select(i => (ITradingInstrument)i)
                                                              .ToList());

                    count = instruments.Count;
                }
            }
            else
            {
                var ids = JsonConvert.DeserializeObject <TradingInstrumentContract>(id);

                var instrumentContract = await _tradingInstrumentsApi.Get(ids.TradingConditionId, ids.Instrument);

                if (instrumentContract != null)
                {
                    var newInstrument = _convertService.Convert <TradingInstrumentContract, TradingInstrument>(instrumentContract);

                    _tradingInstrumentsCacheService.UpdateCache(newInstrument);

                    count = 1;
                }
                else
                {
                    _tradingInstrumentsCacheService.RemoveFromCache(ids.TradingConditionId, ids.Instrument);
                }
            }

            await _log.WriteInfoAsync(nameof(UpdateTradingInstrumentsCacheAsync), nameof(TradingInstrumentsManager),
                                      $"Finished {nameof(UpdateTradingInstrumentsCacheAsync)} with count: {count}.");
        }