服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-09-11 ba0b68c347de4c9214d128d0b51d3af75688d6b3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
{
    /// <summary>
    /// 系统参数数据库数据缓存
    /// </summary>
    public class SysParamCache : 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<SYS_PARAMGROUP> _groups = new();
        public List<SYS_PARAMGROUP> Groups { get { Update(); return _groups; } set => _groups = value; }
        private List<SYS_PARAM> _params = new();
        public List<SYS_PARAM> 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
        /// <summary>
        /// 启动自动更新缓存
        /// </summary>
        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");
            }
        }
 
        /// <summary>
        /// 关闭自动更新缓存
        /// </summary>
        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");
            }
        }
 
        /// <summary>
        /// 更新数据缓存
        /// </summary>
        public void Update()
        {
            var lastUpdate = Biz.Db.Queryable<SYS_PARAM>().Max(q => q.UPDATE_TIME);
            if (LastUpdateTime < lastUpdate)
            {
                _groups = Biz.Db.Queryable<SYS_PARAMGROUP>().IncludesAllFirstLayer().ToList();
                var paramList = new List<SYS_PARAM>();
                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}");
            }
        }
 
        /// <summary>
        /// 自动更新
        /// </summary>
        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
    }
}