using Rhea.Common; using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Tiger.IBusiness; using Tiger.Model; namespace Tiger.Business.DbCache { /// /// 系统参数数据库数据缓存 /// public class SysParamCacheOld : ISysParamCache { #region Variables private WhileThread AutoUpdateThread; private DateTime LastUpdateTime = DateTime.MinValue; #endregion #region Propertys public string Id { get; set; } = Guid.NewGuid().ToString("N"); public string Tag { get; set; } = "SysParamCache"; public string Name { get; set; } = "SysParamCache"; public bool IsRunning { get; set; } private List _groups = new(); public List Groups { get { Update(); return _groups; } set => _groups = value; } private List _params = new(); public List Params { get { Update(); return _params; } set => _params = value; } public SYS_PARAM this[string code, string group = null] { get { Update(); return _params.FirstOrDefault(q => q.PARAM_CODE == code && (group.IsNullOrEmpty() || q.PRMG_CODE == group)); } } #endregion #region Functions /// /// 启动自动更新缓存 /// public void Start() { try { AutoUpdateThread = new(AutoUpdate); AutoUpdateThread.Start(); Logger.Default.Info("Start SysParam Cache Auto Update Thread"); } catch (System.Exception ex) { Logger.Default.Fatal(ex, "Start SysParam Cache Auto Update Thread Exception"); } } /// /// 关闭自动更新缓存 /// public void Stop() { try { AutoUpdateThread?.Stop(); Logger.Console.Info("Stop SysParam Cache Auto Update Thread"); } catch (System.Exception ex) { Logger.Console.Fatal(ex, "Stop SysParam Cache Auto Update Thread Exception"); } } /// /// 更新数据缓存 /// public void Update() { DbClient _db = Biz.DataSource["WMS57"].Client; var lastUpdate = _db.Queryable().Max(q => q.UPDATE_TIME); if (LastUpdateTime < lastUpdate) { _groups = _db.Queryable().IncludesAllFirstLayer().ToList(); var paramList = new List(); foreach (var group in _groups) { paramList.AddRange(group.Params); } _params = paramList; LastUpdateTime = paramList.Max(q => q.UPDATE_TIME); Logger.Console.Info($"Get SysParam successful, total {Params.Count} params in {Groups.Count} groups, last update time is {LastUpdateTime:yyyy-MM-dd HH:mm:ss}"); } } /// /// 自动更新 /// private void AutoUpdate() { try { Update(); } catch (System.Exception ex) { Logger.Console.Fatal(ex, "SysParam Cache Auto Update Exception"); LastUpdateTime = DateTime.MinValue; } //休眠30分钟 Thread.Sleep(30 * 60 * 1000);// } #endregion } }