赖素文
authored
about a year ago
1
2
3
4
using Hh.Mes.Common.config ;
using Hh.Mes.Common.log ;
using Hh.Mes.POJO.Entity ;
using Hh.Mes.Service.Repository ;
唐召明
authored
6 months ago
5
using Microsoft.Extensions.Caching.Distributed ;
赖素文
authored
about a year ago
6
using Newtonsoft.Json.Converters ;
唐召明
authored
6 months ago
7
using Newtonsoft.Json ;
赖素文
authored
about a year ago
8
9
10
11
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Text ;
唐召明
authored
6 months ago
12
using JsonSerializer = System . Text . Json . JsonSerializer ;
赖素文
authored
about a year ago
13
14
15
16
17
18
19
20
namespace Hh.Mes.Service
{
/// < summary >
/// 公共信息缓存 只负责读取数据 缓存
/// </ summary >
public class BaseInfoCacheService : RepositorySqlSugar < sys_user >
{
唐召明
authored
6 months ago
21
private readonly IDistributedCache _cache ;
赖素文
authored
about a year ago
22
23
24
private Dictionary < string , string > baseSql { get ; set ; }
唐召明
authored
6 months ago
25
public BaseInfoCacheService ( IDistributedCache cache )
赖素文
authored
about a year ago
26
27
{
baseSql = ConfigRead . GetInstance . GetBaseInfoSql ();
唐召明
authored
6 months ago
28
_cache = cache ;
赖素文
authored
about a year ago
29
30
31
32
33
}
/// < summary >
/// 第一次默认把所有的基础信息缓存
/// </ summary >
唐召明
authored
6 months ago
34
public void SetBaseInfoCacheFirst ()
赖素文
authored
about a year ago
35
36
37
38
39
40
41
42
{
try
{
var sql = GetAllFileJosnSql ();
var ds = Context . Ado . GetDataSetAllAsync ( sql ). Result ;
var index = 0 ;
foreach ( var item in baseSql )
{
唐召明
authored
6 months ago
43
44
45
var jsonStr = JsonConvert . SerializeObject ( ds . Tables [ index ], new DataTableConverter ());
var cacheOption = new DistributedCacheEntryOptions (). SetAbsoluteExpiration ( TimeSpan . FromMinutes ( 1 ));
_cache . Set ( item . Key , Encoding . UTF8 . GetBytes ( jsonStr ), cacheOption );
赖素文
authored
about a year ago
46
47
48
49
50
51
index ++;
}
}
catch ( Exception ex )
{
Console . WriteLine ();
唐召明
authored
6 months ago
52
Console . WriteLine ( $ "WebApp Connection DataBase Error!Please Check Connection 【{ConfigRead.GetInstance.GetAppsetConnection().BaseDBContext}】" );
赖素文
authored
about a year ago
53
54
55
56
57
58
59
60
61
62
63
64
65
Console . WriteLine ();
Log4NetHelper . Instance . Error ( ex . Message );
}
}
/// < summary >
/// 获取缓存数据
/// </ summary >
/// < param name = "key" > key </ param >
/// < param name = "isRemove" > true 重新设置值 </ param >
/// < returns ></ returns >
public string GetOneBaseInfo ( string key , bool isRemove = false )
{
唐召明
authored
6 months ago
66
67
68
69
if (! baseSql . ContainsKey ( key ))
{
return string . Empty ;
}
赖素文
authored
about a year ago
70
var sql = baseSql [ key ];
唐召明
authored
6 months ago
71
72
73
74
75
76
77
if ( isRemove )
{
return getDataBySql ( sql , key );
}
var bytes = _cache . Get ( key );
if ( bytes != null )
{
唐召明
authored
6 months ago
78
79
80
return Encoding . UTF8 . GetString ( bytes );
// var dt = JsonSerializer . Deserialize < DataTable >( Encoding . UTF8 . GetString ( bytes ));
// return JsonConvert . SerializeObject ( dt , new DataTableConverter ());
唐召明
authored
6 months ago
81
82
}
return getDataBySql ( sql , key );
赖素文
authored
about a year ago
83
84
85
86
87
88
89
90
91
}
/// < summary >
/// 读取数据库数据,并存入缓存
/// </ summary >
private string getDataBySql ( string sql , string key )
{
var dt = Context . Ado . GetDataTableAsync ( sql ). Result ;
var jsonStr = JsonConvert . SerializeObject ( dt , new DataTableConverter ());
唐召明
authored
6 months ago
92
var cacheOption = new DistributedCacheEntryOptions (). SetAbsoluteExpiration ( TimeSpan . FromMinutes ( 1 ));
唐召明
authored
6 months ago
93
94
_cache . Set ( key , Encoding . UTF8 . GetBytes ( jsonStr ), cacheOption );
return JsonConvert . SerializeObject ( dt , new DataTableConverter ());
赖素文
authored
about a year ago
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
}
/// < summary >
/// 获取所有的配置文件 json sql
/// </ summary >
/// < returns ></ returns >
private string GetAllFileJosnSql ()
{
StringBuilder stringBuilder = new StringBuilder ();
foreach ( var item in baseSql )
{
stringBuilder . AppendLine ( item . Value );
}
return stringBuilder . ToString ();
}
赖素文
authored
about a year ago
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/// < summary >
/// api 启动检查数据库是否已连接上
/// </ summary >
public void CheckDatabaseConnected ()
{
var isConnection = false ;
ExceptionsHelp . Instance . ExecuteT (() =>
{
Console . WriteLine ();
Console . ForegroundColor = ConsoleColor . Green ;
Console . WriteLine ( $ "The database is being connected......" );
Console . WriteLine ();
var sql = baseSql [ "base_factory" ];
Context . Ado . GetDataTable ( sql );
return "" ;
}, actionCatCh : () =>
{
isConnection = true ;
Console . ForegroundColor = ConsoleColor . Red ;
Console . WriteLine ();
Console . WriteLine ( $ "The database connection is Error!Please Check Connection 【{ConfigRead.GetInstance.GetAppsetConnection().BaseDBContext}】" );
Console . WriteLine ();
Console . ResetColor ();
}, actionFinally : () =>
{
if (! isConnection ) Console . WriteLine ( "The database connection is successful" );
});
}
赖素文
authored
about a year ago
139
140
}
}