fix: synchronize module data cache cleanup

This commit is contained in:
2026-08-15 18:39:26 +08:00
parent d098dde830
commit a65dfab897
11 changed files with 126 additions and 71 deletions
+74 -27
View File
@@ -197,26 +197,31 @@ namespace Lskj.Util
value = default;
return false;
}
if (dic.ContainsKey(control))
Task<T> task;
lock (dic)
{
if (dic[control].ContainsKey(key))
{
Task<T> task = dic[control][key] as Task<T>;
value = task.Result;
task.Dispose();
return true;
}
else
Hashtable cache;
if (!dic.TryGetValue(control, out cache) ||
cache == null ||
!cache.ContainsKey(key))
{
value = default;
return false;
}
task = cache[key] as Task<T>;
}
else
if (task == null)
{
value = default;
return false;
}
value = task.Result;
task.Dispose();
return true;
}
catch (Exception ex)
{
@@ -237,14 +242,23 @@ namespace Lskj.Util
{
try
{
if (cachesDic.ContainsKey(control) && cachesDic[control].ContainsKey(key))
{
return (Task<T>)cachesDic[control][key];
}
else
if (cachesDic == null)
{
return default;
}
lock (cachesDic)
{
Hashtable cache;
if (cachesDic.TryGetValue(control, out cache) &&
cache != null &&
cache.ContainsKey(key))
{
return cache[key] as Task<T>;
}
}
return default;
}
catch (Exception ex)
{
@@ -253,6 +267,36 @@ namespace Lskj.Util
}
}
/// <summary>
/// 在线程安全的同步区间内移除控件缓存。
/// </summary>
public static bool RemoveCache(this Dictionary<object, Hashtable> cachesDic, object control)
{
if (cachesDic == null)
{
return false;
}
lock (cachesDic)
{
return cachesDic.Remove(control);
}
}
/// <summary>
/// 在线程安全的同步区间内清空任务缓存。
/// </summary>
public static void ClearCaches(this Dictionary<object, Hashtable> cachesDic)
{
if (cachesDic == null)
{
return;
}
lock (cachesDic)
{
cachesDic.Clear();
}
}
/// <summary>
/// 添加任务
/// </summary>
/// <typeparam name="T"></typeparam>
@@ -262,20 +306,23 @@ namespace Lskj.Util
/// <param name="task"></param>
public static Task<T> AddTask<T>(this Dictionary<object, Hashtable> cachesDic, object control, string key, Task<T> task)
{
Hashtable cachesHash = new Hashtable();
if (!cachesDic.ContainsKey(control))
lock (cachesDic)
{
cachesDic.Add(control, cachesHash);
}
else
{
cachesHash = cachesDic[control];
}
cachesHash.Add(key, task);
if (task.Status == TaskStatus.Created)
{
task.Start(SchedulerUtil);
Hashtable cachesHash;
if (!cachesDic.TryGetValue(control, out cachesHash) ||
cachesHash == null)
{
cachesHash = new Hashtable();
cachesDic[control] = cachesHash;
}
cachesHash.Add(key, task);
if (task.Status == TaskStatus.Created)
{
task.Start(SchedulerUtil);
}
}
return task;
}