using System.Collections.Concurrent; using YLErp.Abstract.DataProviders; using YLErp.Modules.DataCacheModule; namespace YLErp.Modules.TradeRiskCalcModule.TaskRunner { /// /// 标的价格提供源,非线程安全, /// 此价格数据源保证计算中同一标的的价格唯一, /// 不受DataCacheManager的更新频率影响 /// class TradeRiskCalcPriceProvider : IPriceProvider { readonly ConcurrentDictionary _priceDic; public TradeRiskCalcPriceProvider() { _priceDic = new ConcurrentDictionary(); } /// /// 清除掉价格数据(用于进入下一循环的计算) /// public void Clear() { _priceDic.Clear(); } public double GetPrice(string instrumentId) { return TryGetPrice(instrumentId, out var price) ? price : 0; } public bool TryGetPrice(string underlyingCode, out double price) { if (string.IsNullOrEmpty(underlyingCode)) { price = 0; return false; } if (_priceDic.TryGetValue(underlyingCode, out price)) { return true; } var result = DataCacheManager.GetUnderlyingDataSource().TryGetPrice(underlyingCode, out price); _priceDic[underlyingCode] = price; return result; } } }