Files
zszq-trs/YLErpDAL/Modules/DataCacheModule/DataSource/ExchangeListOptionDataSource.cs
T
2024-05-09 14:06:26 +08:00

84 lines
2.7 KiB
C#

namespace YLErp.Modules.DataCacheModule
{
public static partial class DataCacheManager
{
/// <summary>
/// 场内期权基础数据
/// </summary>
class ExchangeListOptionDataSource : GenericeCachedDataSource<ExchangeListOption>, Abstract.IDataSourceKey2<ExchangeListOption>
{
public override string TableName => nameof(ExchangeListOption);
readonly Dictionary<string, ExchangeListOption> _dicEx;
public ExchangeListOptionDataSource()
{
_dicEx = new Dictionary<string, ExchangeListOption>(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<ExchangeListOption>(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();
}
}
}
}