Skip to content

Commit

Permalink
进一步优化AutoCache,内部改进调优。
Browse files Browse the repository at this point in the history
  • Loading branch information
cyq1162 committed Jul 12, 2016
1 parent a395b31 commit 0792104
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 108 deletions.
22 changes: 11 additions & 11 deletions Action/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,17 +570,17 @@ public static string MemCacheServers
/// <summary>
/// Cache.CacheManage 调用GC.Collect()方法的间隔时间[(默认180)分钟]
/// </summary>
public static int GCCollectTime
{
get
{
return GetAppInt("GCCollectTime", 180);
}
set
{
SetApp("GCCollectTime", value.ToString());
}
}
//public static int GCCollectTime
//{
// get
// {
// return GetAppInt("GCCollectTime", 180);
// }
// set
// {
// SetApp("GCCollectTime", value.ToString());
// }
//}

/// <summary>
/// Cache.CacheManage 默认缓存项的时间[分钟(默认60)]
Expand Down
14 changes: 9 additions & 5 deletions Action/MAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ private void InitSqlHelper(MDataRow newRow, string newDbName)
{
dalHelper = DalCreate.CreateDal(newRow.Conn);
//创建错误事件。
dalHelper.OnExceptionEvent += new DbBase.OnException(_DataSqlHelper_OnExceptionEvent);
if (dalHelper.IsOnExceptionEventNull)
{
dalHelper.OnExceptionEvent += new DbBase.OnException(_DataSqlHelper_OnExceptionEvent);
}
}
else
{
Expand Down Expand Up @@ -1400,12 +1403,13 @@ public void Dispose()
hasDisposed = true;
if (dalHelper != null)
{
dalHelper.Dispose();
if (dalHelper != null)
if (!dalHelper.IsOnExceptionEventNull)
{
_debugInfo = dalHelper.debugInfo.ToString();
dalHelper = null;
dalHelper.OnExceptionEvent -= new DbBase.OnException(_DataSqlHelper_OnExceptionEvent);
}
_debugInfo = dalHelper.debugInfo.ToString();
dalHelper.Dispose();
dalHelper = null;
if (_sqlCreate != null)
{
_sqlCreate = null;
Expand Down
9 changes: 8 additions & 1 deletion Action/MProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ private void Init(object procNameOrSql, string conn, params bool[] isFixProc)
private void SetDbBase(DbBase dbBase)
{
dalHelper = dbBase;
dalHelper.OnExceptionEvent += new DbBase.OnException(helper_OnExceptionEvent);
if (dalHelper.IsOnExceptionEventNull)
{
dalHelper.OnExceptionEvent += new DbBase.OnException(helper_OnExceptionEvent);
}
switch (dalHelper.dalType)
{
case DalType.Txt:
Expand Down Expand Up @@ -631,6 +634,10 @@ public void Dispose()
hasDisposed = true;
if (dalHelper != null)
{
if (!dalHelper.IsOnExceptionEventNull)
{
dalHelper.OnExceptionEvent -= new DbBase.OnException(helper_OnExceptionEvent);
}
_debugInfo = dalHelper.debugInfo.ToString();
dalHelper.Dispose();
dalHelper = null;
Expand Down
6 changes: 3 additions & 3 deletions Aop/InterAop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace CYQ.Data.Aop
internal class InterAop
{
private CacheManage _Cache = CacheManage.LocalInstance;//Cache操作
private AutoCache cacheAop = new AutoCache();
// private AutoCache cacheAop = new AutoCache();
private static readonly object lockObj = new object();
private bool isHasCache = false;
private bool isUseAop = true;
Expand Down Expand Up @@ -69,7 +69,7 @@ public AopResult Begin(AopEnum action)
}
if (AppConfig.Cache.IsAutoCache && !IsTxtDataBase) // 只要不是直接返回
{
isHasCache = cacheAop.GetCache(action, Para); //找看有没有Cache
isHasCache = AutoCache.GetCache(action, Para); //找看有没有Cache
}
if (isHasCache) //找到Cache
{
Expand All @@ -93,7 +93,7 @@ public void End(AopEnum action)
}
if (!isHasCache && !IsTxtDataBase)
{
cacheAop.SetCache(action, Para); //找看有没有Cache
AutoCache.SetCache(action, Para); //找看有没有Cache
}
}

Expand Down
113 changes: 68 additions & 45 deletions Cache/AutoCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace CYQ.Data.Cache
/// <summary>
/// 内部智能缓存
/// </summary>
internal class AutoCache
internal static class AutoCache
{
private static CacheManage _MemCache = CacheManage.Instance;//有可能使用MemCache操作

internal bool GetCache(AopEnum action, AopInfo aopInfo)//Begin
internal static bool GetCache(AopEnum action, AopInfo aopInfo)//Begin
{
switch (action)
{
Expand Down Expand Up @@ -79,10 +79,11 @@ internal bool GetCache(AopEnum action, AopInfo aopInfo)//Begin
}
break;
}
baseKey = key = null;
return obj != null;
}

internal void SetCache(AopEnum action, AopInfo aopInfo)//End
internal static void SetCache(AopEnum action, AopInfo aopInfo)//End
{
string baseKey = GetBaseKey(aopInfo);
switch (action)
Expand All @@ -103,12 +104,16 @@ internal void SetCache(AopEnum action, AopInfo aopInfo)//End
return;
}
string key = GetKey(action, aopInfo, baseKey);
bool isKnownTable;
SetBaseKeys(aopInfo, key, out isKnownTable);//存档Key,后续缓存失效 批量删除
double cacheTime = (24 - DateTime.Now.Hour) * 60 + DateTime.Now.Second;//缓存到夜里1点
if (!isKnownTable || aopInfo.PageIndex > 3) // 后面的页数,缓存时间可以短一些
int flag;//0 正常;1:未识别;2:不允许缓存
SetBaseKeys(aopInfo, key, out flag);//存档Key,后续缓存失效 批量删除
if (flag == 2)
{
cacheTime = 3;//未知道操作何表时,只缓存3分钟(比如存储过程等语句)
return;//
}
double cacheTime = Math.Abs(12 - DateTime.Now.Hour) * 60 + DateTime.Now.Second;//缓存中午或到夜里1点
if (flag == 1 || aopInfo.PageIndex > 2) // 后面的页数,缓存时间可以短一些
{
cacheTime = 2;//未知道操作何表时,只缓存2分钟(比如存储过程等语句)
}
switch (action)
{
Expand Down Expand Up @@ -145,7 +150,7 @@ internal void SetCache(AopEnum action, AopInfo aopInfo)//End
}

#region 检测过滤
bool IsCanCache(List<MDataTable> dtList)
static bool IsCanCache(List<MDataTable> dtList)
{
foreach (MDataTable item in dtList)
{
Expand All @@ -156,7 +161,7 @@ bool IsCanCache(List<MDataTable> dtList)
}
return true;
}
bool IsCanCache(MDataTable dt)
static bool IsCanCache(MDataTable dt)
{
foreach (MCellStruct item in dt.Columns)
{
Expand All @@ -175,9 +180,9 @@ bool IsCanCache(MDataTable dt)
/// 单机存档: 表的baseKey:key1,key2,key3...
/// </summary>
private static MDictionary<string, StringBuilder> cacheKeys = new MDictionary<string, StringBuilder>(StringComparer.OrdinalIgnoreCase);
private void SetBaseKeys(AopInfo para, string key, out bool isKnownTable)
private static void SetBaseKeys(AopInfo para, string key, out int flag)
{
List<string> items = GetBaseKeys(para, out isKnownTable);
List<string> items = GetBaseKeys(para, out flag);
if (items != null && items.Count > 0)
{
foreach (string item in items)
Expand All @@ -187,7 +192,7 @@ private void SetBaseKeys(AopInfo para, string key, out bool isKnownTable)
items = null;
}
}
private void SetBaseKey(string baseKey, string key)
private static void SetBaseKey(string baseKey, string key)
{
//baseKey是表的,不包括视图和自定义语句
if (_MemCache.CacheType == CacheType.LocalCache)
Expand Down Expand Up @@ -216,26 +221,28 @@ private void SetBaseKey(string baseKey, string key)
}

}
private bool IsCanOperateCache(string baseKey)
private static bool IsCanOperateCache(string baseKey)
{
if (baseKey.Contains(".ActionV") || baseKey.Contains(".ProcS"))
{
string delKey = "DeleteAutoCache:" + baseKey;
return !_MemCache.Contains(delKey);
//if (baseKey.Contains(".ActionV") || baseKey.Contains(".ProcS"))
//{

return true;
}
TimeSpan ts = DateTime.Now - _MemCache.Get<DateTime>("Del:" + baseKey);
return ts.TotalSeconds > 6;//5秒内无缓存。
// return true;
//}
//TimeSpan ts = DateTime.Now - _MemCache.Get<DateTime>("Del:" + baseKey);
//return ts.TotalSeconds > 6;//5秒内无缓存。
}

private string GetBaseKey(AopInfo para)
private static string GetBaseKey(AopInfo para)
{
return GetBaseKey(para, null);
}
internal static string GetBaseKey(DalType dalType, string database, string tableName)
{
return "AutoCache:" + dalType + "." + database + "." + tableName;
}
private string GetBaseKey(AopInfo para, string tableName)
private static string GetBaseKey(AopInfo para, string tableName)
{
if (string.IsNullOrEmpty(tableName))
{
Expand All @@ -257,9 +264,9 @@ private string GetBaseKey(AopInfo para, string tableName)
}
return GetBaseKey(para.DalType, para.DataBase, tableName);
}
private List<string> GetBaseKeys(AopInfo para, out bool isKnownTable)
private static List<string> GetBaseKeys(AopInfo para, out int flag)
{
isKnownTable = true;
flag = 0;//0 正常;1:未识别;2:暂不允许缓存
List<string> baseKeys = new List<string>();
List<string> tables = null;
if (para.MAction != null)
Expand All @@ -281,21 +288,32 @@ private List<string> GetBaseKeys(AopInfo para, out bool isKnownTable)
{
foreach (string tableName in tables)
{
baseKeys.Add(GetBaseKey(para, tableName));
string baseKey = GetBaseKey(para, tableName);
string delKey = "DeleteAutoCache:" + baseKey;
if (_MemCache.Contains(delKey))
{
//说明此项不可缓存
flag = 2;
baseKeys.Clear();
baseKeys = null;
return null;
}
baseKeys.Add(baseKey);
}
tables.Clear();
tables = null;
}
if (baseKeys.Count == 0)
{
isKnownTable = false;
//baseKeys.Add(GetBaseKey(para, null));
flag = 1;
}
return baseKeys;
}
private string GetKey(AopEnum action, AopInfo aopInfo)
private static string GetKey(AopEnum action, AopInfo aopInfo)
{
return GetKey(action, aopInfo, GetBaseKey(aopInfo));
}
private string GetKey(AopEnum action, AopInfo aopInfo, string baseKey)
private static string GetKey(AopEnum action, AopInfo aopInfo, string baseKey)
{
StringBuilder sb = new StringBuilder();
sb.Append(baseKey);
Expand Down Expand Up @@ -363,26 +381,30 @@ private string GetKey(AopEnum action, AopInfo aopInfo, string baseKey)
private static Queue<string> removeList = new Queue<string>();
public static void ReadyForRemove(string baseKey)
{
_MemCache.Set("Del:" + baseKey, DateTime.Now);//设置好要更新的时间
if (!removeList.Contains(baseKey))
string delKey = "DeleteAutoCache:" + baseKey;
if (!_MemCache.Contains(delKey))
{
try
{
removeList.Enqueue(baseKey);
}
catch
if (!removeList.Contains(baseKey))
{
try
{
removeList.Enqueue(baseKey);
}
catch
{

}
}

}
}
_MemCache.Set(delKey, 0, 0.1);//设置6秒时间
}

public void ClearCache(object threadID)
public static void ClearCache(object threadID)
{
while (true)
{
Thread.Sleep(100);
Thread.Sleep(500);
if (removeList.Count > 0)
{
string baseKey = removeList.Dequeue();
Expand All @@ -395,7 +417,7 @@ public void ClearCache(object threadID)
}
private static readonly object lockObj = new object();
private static DateTime errTime = DateTime.MinValue;
internal void RemoveCache(string baseKey)
internal static void RemoveCache(string baseKey)
{
try
{
Expand Down Expand Up @@ -423,20 +445,20 @@ internal void RemoveCache(string baseKey)
}
}
}
catch (OutOfMemoryException)
{ }
catch (Exception err)
{
if (errTime == DateTime.MinValue || errTime.AddMinutes(10) < DateTime.Now) // 10分钟记录一次
{
errTime = DateTime.Now;
if (!(err is OutOfMemoryException))
{
Log.WriteLogToTxt(err);
}
Log.WriteLogToTxt(err);
}
}
}
#endregion

/*
#region 实例对象
/// <summary>
/// 单例
Expand All @@ -455,9 +477,10 @@ class Shell
}
internal AutoCache()
{
ThreadBreak.AddGlobalThread(new System.Threading.ParameterizedThreadStart(ClearCache));
}
#endregion
*/
}
}
Loading

0 comments on commit 0792104

Please sign in to comment.