forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheManage.cs
287 lines (276 loc) · 9.1 KB
/
CacheManage.cs
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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System.Collections.Generic;
using CYQ.Data.Table;
using CYQ.Data.Tool;
using System;
using CYQ.Data.SQL;
namespace CYQ.Data.Cache
{
/// <summary>
/// 全局缓存类
/// </summary>
/// <example><code>
/// 使用示例:
/// 实例化: CacheManage cache=CacheManage.Instance;
/// 添加: Cache.Set("路过秋天",new MDataTable);
/// 判断: if(cache.Contains("路过秋天"))
/// {
/// 获取: MDataTable table=cache.Get("路过秋天") as MDataTable;
/// }
/// </code></example>
public abstract partial class CacheManage
{
#region 对外实例
/// <summary>
/// 返回唯一实例(根据配置(AppConfig.Cache.XXXCacheServers)决定启用顺序:Redis、MemCache、本地缓存)
/// </summary>
public static CacheManage Instance
{
get
{
if (!string.IsNullOrEmpty(AppConfig.Cache.RedisServers))
{
return RedisInstance;
}
else if (!string.IsNullOrEmpty(AppConfig.Cache.MemCacheServers))
{
return MemCacheInstance;
}
else
{
return LocalShell.instance;
}
}
}
/// <summary>
/// 单机本地缓存
/// </summary>
public static CacheManage LocalInstance
{
get
{
return LocalShell.instance;
}
}
private static CacheManage _MemCacheInstance;
private static readonly object lockMemCache = new object();
/// <summary>
/// MemCache缓存(需要配置AppConfig.Cache.MemCacheServers)
/// </summary>
public static CacheManage MemCacheInstance
{
get
{
if (_MemCacheInstance == null)
{
lock (lockMemCache)
{
if (_MemCacheInstance == null)
{
_MemCacheInstance = new MemCache();
}
}
}
return _MemCacheInstance;
}
}
private static CacheManage _RedisInstance;
private static readonly object lockRedisCache = new object();
/// <summary>
/// Redis缓存(需要配置AppConfig.Cache.RedisServers)
/// </summary>
public static CacheManage RedisInstance
{
get
{
if (_RedisInstance == null)
{
lock (lockRedisCache)
{
if (_RedisInstance == null)
{
_RedisInstance = new RedisCache();
}
}
}
return _RedisInstance;
}
}
class LocalShell
{
internal static readonly LocalCache instance = new LocalCache();
}
//此种方式,会提前处理,导致异常。
//class MemShell
//{
// internal static readonly MemCache instance = new MemCache();
//}
//class RedisShell
//{
// internal static readonly RedisCache instance = new RedisCache();
//}
#endregion
/// <summary>
/// 缓存的实例类型
/// </summary>
public abstract CacheType CacheType { get; }
/// <summary>
/// 缓存的信息
/// </summary>
public abstract MDataTable CacheInfo { get; }
/// <summary>
/// 设置一个Cache对象
/// </summary>
public abstract void Set(string key, object value);
public abstract void Set(string key, object value, double cacheMinutes);
public abstract void Set(string key, object value, double cacheMinutes, string fileName);
/// <summary>
/// 清除所有缓存
/// </summary>
public abstract void Clear();
public abstract bool Contains(string key);
/// <summary>
/// 获和缓存总数
/// </summary>
public abstract int Count { get; }
/// <summary>
/// 获得一个Cache对象
/// </summary>
public abstract object Get(string key);
/// <summary>
/// 获得一个Cache对象
/// </summary>
public T Get<T>(string key)
{
object o = Get(key);
if (o != null)
{
Type t = typeof(T);
try
{
return (T)StaticTool.ChangeType(o, t);
}
catch (Exception err)
{
Log.WriteLogToTxt(err);
return default(T);
}
}
return default(T);
}
/// <summary>
/// 删除一个Cache对象
/// </summary>
public abstract void Remove(string key);
public abstract string WorkInfo { get; }
}
public abstract partial class CacheManage
{
/// <summary>
/// 获取系统内部缓存Key
/// </summary>
public static string GetKey(CacheKeyType ckt, string tableName)
{
return GetKey(ckt, tableName, AppConfig.DB.DefaultDataBase, AppConfig.DB.DefaultDalType);
}
/// <summary>
/// 获取系统内部缓存Key
/// </summary>
public static string GetKey(CacheKeyType ckt, string tableName, string dbName, DalType dalType)
{
switch (ckt)
{
case CacheKeyType.Schema:
return TableSchema.GetSchemaKey(tableName, dbName, dalType);
case CacheKeyType.AutoCache:
return AutoCache.GetBaseKey(dalType, dbName, tableName);
}
return string.Empty;
}
}
public abstract partial class CacheManage
{
/// <summary>
/// 通过该方法可以预先加载整个数据库的表结构缓存
/// </summary>
public static void PreLoadDBSchemaToCache()
{
PreLoadDBSchemaToCache(AppConfig.DB.DefaultConn, true);
}
private static readonly object obj = new object();
/// <summary>
/// 通过该方法可以预先加载整个数据库的表结构缓存(异常会抛,外层Try Catch)
/// </summary>
/// <param name="conn">指定数据链接</param>
/// <param name="isUseThread">是否开启线程</param>
public static void PreLoadDBSchemaToCache(string conn, bool isUseThread)
{
if (TableSchema.tableCache == null || TableSchema.tableCache.Count == 0)
{
lock (obj)
{
if (TableSchema.tableCache == null || TableSchema.tableCache.Count == 0)
{
if (isUseThread)
{
ThreadBreak.AddGlobalThread(new System.Threading.ParameterizedThreadStart(LoadDBSchemaCache), conn);
}
else
{
LoadDBSchemaCache(conn);
}
}
}
}
}
private static void LoadDBSchemaCache(object connObj)
{
string conn = Convert.ToString(connObj);
Dictionary<string, string> dic = DBTool.GetTables(Convert.ToString(conn));
if (dic != null && dic.Count > 0)
{
DbBase helper = DalCreate.CreateDal(conn);
if (helper.dalType != DalType.Txt && helper.dalType != DalType.Xml)
{
foreach (string key in dic.Keys)
{
TableSchema.GetColumns(key, ref helper);
}
}
helper.Dispose();
}
}
}
/// <summary>
/// 支持的Cache类型
/// </summary>
public enum CacheType
{
/// <summary>
/// 本地缓存
/// </summary>
LocalCache,
/// <summary>
/// MemCached分布式缓存
/// </summary>
MemCache,
/// <summary>
/// Redis分布式缓存
/// </summary>
Redis
}
/// <summary>
/// Cache的Key类型
/// </summary>
public enum CacheKeyType
{
/// <summary>
/// 表架构的Key
/// </summary>
Schema,
/// <summary>
/// 智能缓存Key
/// </summary>
AutoCache
}
}