forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalCache.cs
458 lines (432 loc) · 17.2 KB
/
LocalCache.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using CYQ.Data.Tool;
using CYQ.Data.Table;
using CYQ.Data.SQL;
using System.IO;
namespace CYQ.Data.Cache
{
/// <summary>
/// 单机缓存类
/// 为兼容.NET Core 去掉Web.Caching,重写
/// </summary>
internal class LocalCache : CacheManage
{
private MDictionary<string, object> theCache = new MDictionary<string, object>(2048, StringComparer.OrdinalIgnoreCase);//key,cache
private MDictionary<string, DateTime> theKeyTime = new MDictionary<string, DateTime>(2048, StringComparer.OrdinalIgnoreCase);//key,time
private MDictionary<string, string> theFileName = new MDictionary<string, string>();//key,filename
private SortedDictionary<int, MList<string>> theTime = new SortedDictionary<int, MList<string>>();//worktime,keylist
private MDictionary<string, FileSystemWatcher> theFolderWatcher = new MDictionary<string, FileSystemWatcher>();//folderPath,watch
private MDictionary<string, MList<string>> theFolderKeys = new MDictionary<string, MList<string>>();//folderPath,keylist
private static object lockObj = new object();
private DateTime workTime, startTime;
DateTime allowCacheTableTime = DateTime.Now;
private MDataTable cacheInfoTable;
/// <summary>
/// 获取缓存信息对象列表
/// </summary>
public override MDataTable CacheInfo
{
get
{
if (cacheInfoTable == null || cacheInfoTable.Columns.Count == 0 || DateTime.Now > allowCacheTableTime)
{
#region Create Table
MDataTable cacheTable = new MDataTable("LocalCache");
cacheTable.Columns.Add("Key,Value");
cacheTable.NewRow(true).Set(0, "CacheCount").Set(1, theCache.Count);
cacheTable.NewRow(true).Set(0, "TimeCount").Set(1, theTime.Count);
cacheTable.NewRow(true).Set(0, "FileCount").Set(1, theFileName.Count);
cacheTable.NewRow(true).Set(0, "KeyTimeCount").Set(1, theKeyTime.Count);
cacheTable.NewRow(true).Set(0, "FolderWatcherCount").Set(1, theFolderWatcher.Count);
cacheTable.NewRow(true).Set(0, "TaskStartTime").Set(1, startTime);
cacheTable.NewRow(true).Set(0, "TaskWorkCount").Set(1, taskCount);
cacheTable.NewRow(true).Set(0, "ErrorCount").Set(1, errorCount);
#endregion
allowCacheTableTime = DateTime.Now.AddSeconds(5);
cacheInfoTable = cacheTable;
}
return cacheInfoTable;
}
}
internal LocalCache()
{
try
{
ThreadBreak.AddGlobalThread(new ParameterizedThreadStart(ClearState));
ThreadBreak.AddGlobalThread(new ParameterizedThreadStart(ConnObject.CheckConnIsOk));//主从链接的检测机制。
//if (AppConfig.Cache.IsAutoCache) // 机制变更为Aop也可控制,所以这个参数还决定不了
//{
ThreadBreak.AddGlobalThread(new ParameterizedThreadStart(AutoCache.ClearCache));
//}
}
catch (Exception err)
{
errorCount++;
Log.Write(err, LogType.Cache);
}
}
int taskCount = 0, taskInterval = 5;//5分钟清一次缓存。
private static DateTime errTime = DateTime.MinValue;
private void ClearState(object threadid)
{
startTime = DateTime.Now;
while (true)
{
try
{
workTime = startTime.AddMinutes((taskCount + 1) * taskInterval);
TimeSpan ts = workTime - DateTime.Now;
if (ts.TotalSeconds > 0)
{
Thread.Sleep(ts);//taskInterval * 60 * 1000);//10分钟休眠时间
}
#region 新的机制
if (theTime.ContainsKey(taskCount))
{
RemoveList(theTime[taskCount].GetList());
theTime.Remove(taskCount);
}
#endregion
}
catch (ThreadAbortException e)
{
}
catch (OutOfMemoryException)
{ errorCount++; }
catch (Exception err)
{
errorCount++;
if (errTime == DateTime.MinValue || errTime.AddMinutes(10) < DateTime.Now) // 10分钟记录一次
{
errTime = DateTime.Now;
Log.Write("LocalCache.ClearState : " + Log.GetExceptionMessage(err), LogType.Cache);
}
}
finally
{
taskCount++;
if (taskCount % 10 == 9)
{
try
{
if (theCache.Count > 100000)// theKey.Count > 100000)
{
NoSqlAction.ResetStaticVar();
GC.Collect();
}
}
catch
{
errorCount++;
}
}
CheckIsSafe();
}
}
}
private bool isFirstCheck = false;
private void CheckIsSafe()
{
if (!isFirstCheck)
{
isFirstCheck = true;
if (!EncryptHelper.HashKeyIsValid())
{
AppConfig.SetApp("Conn" + AppConst.Result, "false");
}
}
}
private int errorCount = 0;//缓存捕异常次数
/// <summary>
/// 内存工作信息
/// </summary>
public override string WorkInfo
{
get
{
JsonHelper js = new JsonHelper(false, false);
js.Add("TaskCount", taskCount.ToString(), true);
js.Add("ErrorCount", errorCount.ToString(), true);
js.Add("NextTaskTime", workTime.ToString());
js.AddBr();
return js.ToString();
// return string.Format("try catch error count:{0}--clear count:{1}--next clear work time at:{2}", errorCount, taskCount, workTime);
}
}
/// <summary>
/// 获和缓存总数
/// </summary>
public override int Count
{
get
{
return theCache.Count;
// return theKey.Count;
}
}
/// <summary>
/// 获得一个Cache对象
/// </summary>
/// <param name="key">标识</param>
public override object Get(string key)
{
if (Contains(key))
{
return theCache[key];// && theCache.ContainsKey(key) 内部已有判断和Lock
}
return null;
}
/// <summary>
/// 是否存在缓存
/// </summary>
/// <param name="key">标识</param>
/// <returns></returns>
public override bool Contains(string key)
{
if (string.IsNullOrEmpty(key)) { return false; }
return theCache.ContainsKey(key) && theKeyTime.ContainsKey(key) && theKeyTime[key] > DateTime.Now;
}
/// <summary>
/// 添加一个Cache对象
/// </summary>
/// <param name="key">标识</param>
/// <param name="value">对象值</param>
public override bool Set(string key, object value)
{
return Set(key, value, AppConfig.Cache.DefaultCacheTime);
}
/// <param name="cacheMinutes">缓存时间(单位分钟)</param>
public override bool Set(string key, object value, double cacheMinutes)
{
return Set(key, value, cacheMinutes, null);
}
/// <param name="fileName">文件依赖路径</param>
public override bool Set(string key, object value, double cacheMinutes, string fileName)
{
try
{
if (string.IsNullOrEmpty(key)) { return false; }
if (value == null) { return Remove(key); }
lock (lockObj)
{
if (theCache.ContainsKey(key))
{
theCache[key] = value;
}
else
{
theCache.Add(key, value);//2:设置value
}
double cacheTime = cacheMinutes;
if (cacheMinutes <= 0)
{
cacheTime = AppConfig.Cache.DefaultCacheTime;
}
DateTime cTime = DateTime.Now.AddMinutes(cacheTime);
int workCount = GetWorkCount(cTime);
if (theKeyTime.ContainsKey(key))
{
int wc = GetWorkCount(theKeyTime[key]);
if (wc != workCount && theTime.ContainsKey(wc))
{
if (theTime[wc].Contains(key))
{
theTime[wc].Remove(key); //移除旧值
}
}
theKeyTime[key] = cTime;
}
else
{
theKeyTime.Add(key, cTime);
}
if (theTime.ContainsKey(workCount))//3:设置time
{
if (!theTime[workCount].Contains(key))
{
theTime[workCount].Add(key);
}
}
else
{
MList<string> list = new MList<string>();
list.Add(key);
theTime.Add(workCount, list);
}
if (!string.IsNullOrEmpty(fileName))//3:设置file
{
if (fileName.IndexOf("\\\\") > -1)
{
fileName = fileName.Replace("\\\\", "\\");
}
if (!theFileName.ContainsKey(key))
{
theFileName.Add(key, fileName);
}
string folder = Path.GetDirectoryName(fileName);
if (!theFolderWatcher.ContainsKey(folder) && Directory.Exists(folder))
{
theFolderWatcher.Add(folder, CreateFileSystemWatcher(folder));
}
if (theFolderKeys.ContainsKey(folder))
{
if (!theFolderKeys[folder].Contains(key))
{
theFolderKeys[folder].Add(key);
}
}
else
{
MList<string> list = new MList<string>();
list.Add(key);
theFolderKeys.Add(folder, list);
}
}
}
return true;
}
catch (Exception err)
{
Log.Write(err, LogType.Cache);
errorCount++;
return false;
}
}
private int GetWorkCount(DateTime cTime)
{
TimeSpan ts = cTime - startTime;
return (int)ts.TotalMinutes / taskInterval;//计算出离开始有多少个间隔时间。
}
/// <summary>
/// 删除一个Cache对象
/// </summary>
/// <param name="key">标识</param>
public override bool Remove(string key)
{
if (string.IsNullOrEmpty(key)) { return false; }
return theCache.Remove(key);//清除Cache,其它数据在定义线程中移除
}
/// <summary>
/// 移除Key和Value
/// </summary>
/// <param name="removeKeys"></param>
private void RemoveList(List<string> removeKeys)
{
if (removeKeys != null && removeKeys.Count > 0)
{
lock (lockObj)
{
foreach (string key in removeKeys)
{
try
{
if (theCache.ContainsKey(key))
{
theCache.Remove(key);
}
if (theKeyTime.ContainsKey(key))
{
theKeyTime.Remove(key);
}
if (theFileName.ContainsKey(key))
{
string folder = Path.GetDirectoryName(theFileName[key]);
MList<string> keys = theFolderKeys[folder];
keys.Remove(key);
if (keys.Count == 0)
{
theFolderWatcher[folder].Changed -= new FileSystemEventHandler(fsy_Changed);//取消事件
theFolderWatcher.Remove(folder);//文件夹下没有要监视的文件,取消事件和对象。
theFolderKeys.Remove(folder);//移除对象。
}
theFileName.Remove(key);//file
}
//file
}
catch
{
errorCount++;
break;
}
}
}
}
}
/// <summary>
/// 清除所有缓存
/// </summary>
public override void Clear()
{
try
{
lock (lockObj)
{
DBSchema.Clear();
TableSchema.Clear();
theCache.Clear();
theTime.Clear();
theFileName.Clear();
theKeyTime.Clear();
for (int i = 0; i < theFolderWatcher.Count; i++)
{
theFolderWatcher[i].Changed -= new FileSystemEventHandler(fsy_Changed);
theFolderWatcher[i] = null;
}
theFolderWatcher.Clear();
theFolderKeys.Clear();
}
}
catch
{
errorCount++;
}
}
public override CacheType CacheType
{
get { return CacheType.LocalCache; }
}
#region 处理文件依赖
private FileSystemWatcher CreateFileSystemWatcher(string folderName)
{
FileSystemWatcher fsy = new FileSystemWatcher(folderName, "*.*");
fsy.EnableRaisingEvents = true;
fsy.IncludeSubdirectories = false;
fsy.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.FileName | NotifyFilters.DirectoryName;
fsy.Changed += new FileSystemEventHandler(fsy_Changed);
return fsy;
}
private static readonly object obj2 = new object();
void fsy_Changed(object sender, FileSystemEventArgs e)
{
lock (obj2)
{
string fileName = e.FullPath;
string folder = Path.GetDirectoryName(fileName);
if (theFolderKeys.ContainsKey(folder))
{
MList<string> keys = theFolderKeys[folder];//.GetList();
int count = keys.Count;
//统一路径方式
fileName = fileName.Replace("/", "\\");
for (int i = 0; i < count; i++)
{
if (i < keys.Count)
{
string path = theFileName[keys[i]].Replace("/", "\\");
if (string.Compare(path, fileName, StringComparison.OrdinalIgnoreCase) == 0)
{
Remove(keys[i]);
keys.Remove(keys[i]);
i--;
}
}
}
}
}
}
#endregion
}
}