标签:
/// <summary>
/// CachePriceParamSetting类
/// </summary>
public class CachePriceParamSetting
{
/// <summary>
/// 缓存参数集合
/// </summary>
private static List<MPriceParamSetting> priceParamSettingList = new List<MPriceParamSetting>();
/// <summary>
/// 缓存刷新时间
/// </summary>
private static int cacheIntervalTime = 5;
/// <summary>
/// 缓存时间
/// </summary>
private static DateTime cacheTime = DateTime.MinValue;
/// <summary>
/// 控制刷新时间time
/// </summary>
private static Timer timer = new Timer();
/// <summary>
/// timer是否开始
/// </summary>
private static bool timerStart = false;
/// <summary>
/// 锁
/// </summary>
private static object lockObj = new object();
/// <summary>
/// 构造函数
/// </summary>
public CachePriceParamSetting()
{
if (!timerStart)
{
// 首次 先初始化
RefreshCache(true);
timerStart = true;
timer.Interval = cacheIntervalTime * 1000 * 60;
timer.Enabled = true;
timer.Elapsed += new ElapsedEventHandler(this.Timer_Elapsed);
}
}
/// <summary>
/// 刷新缓存
/// </summary>
/// <param name="mustRefresh">是否强制刷新缓存</param>
public static void RefreshCache(bool mustRefresh)
{
if (null == priceParamSettingList || mustRefresh)
{
try
{
JavaScriptSerializer serialize = new JavaScriptSerializer();
List<MPriceParamSetting> tempList = new List<MPriceParamSetting>();
lock (lockObj)
{
tempList = serialize.Deserialize<List<MPriceParamSetting>>(SupplyPriceBusinessBaseServiceHelper.GetAllPriceParamSettingJSON());
if (tempList != null)
{
priceParamSettingList = tempList;
}
}
}
catch (Exception ex)
{
if (TrackIdManager.CurrentTrackID != null)
{
TrackIdManager.CurrentTrackID.ExceptionId = Guid.Empty;
}
AppException app = new AppException("999999", ex.Message, ex, null);
LogManager.Log.WriteException(app);
}
}
}
/// <summary>
/// 获取规则和缓存列表
/// </summary>
/// <returns>缓存列表</returns>
public List<MPriceParamSetting> GetPriceParamSettingListCache()
{
return priceParamSettingList;
}
/// <summary>
/// 定时执行 刷新缓存
/// </summary>
/// <param name="obj">事件</param>
/// <param name="e">对象</param>
private void Timer_Elapsed(object obj, ElapsedEventArgs e)
{
RefreshCache(true);
}
}
标签:
原文地址:http://www.cnblogs.com/SmartLee/p/4709258.html