namespace YLErp.Modules.DataCacheModule { public static partial class DataCacheManager { /// /// 场内期权基础数据 /// class ExchangeListOptionDataSource : GenericeCachedDataSource, Abstract.IDataSourceKey2 { public override string TableName => nameof(ExchangeListOption); readonly Dictionary _dicEx; public ExchangeListOptionDataSource() { _dicEx = new Dictionary(StringComparer.OrdinalIgnoreCase); AfterUpdate = data => { if (!string.IsNullOrWhiteSpace(data?.ContractCode)) { _dicEx[data.ContractCode] = data; } }; AfterRemove = data => { if (!string.IsNullOrWhiteSpace(data?.ContractCode)) { _dicEx.Remove(data.ContractCode); } }; //使用近1个月的数据作为缓存 var date = DateTime.Today.AddDays(-30); Filter = PredicateBuilder.Create(n => n.MaturityDate > date); } public ExchangeListOption GetData(string keyId) { if (string.IsNullOrWhiteSpace(keyId)) { return null; } lock (this) { if (_dicEx.TryGetValue(keyId, out ExchangeListOption data)) { return data; } using (var db = DbContextFactory.GetYLDbContext()) { var exopt = db.exchange_list_option.AsNoTracking().FirstOrDefault(n => n.ContractCode == keyId); if (exopt != null) { _dic[exopt.id] = exopt; _dicEx[exopt.ContractCode] = exopt; } return exopt?.Clone(); } } } public override void ResetDataSource() { base.ResetDataSource(); _dicEx.Clear(); } //--------------------------------------------- public static readonly ExchangeListOptionDataSource Default; static ExchangeListOptionDataSource() { Default = new ExchangeListOptionDataSource(); } } } }