using System; using System.Collections.Concurrent; namespace YLErp.Modules.RiskEngine { /// /// 规则编译结果的进程内缓存。 /// 当前第一版按规则 Id 缓存 Roslyn 编译后的可执行委托。 /// public static class RuleCompiledCache { /// /// 规则编译结果的进程内缓存。ConcurrentDictionary为线程安全的字典,用于缓存编译后的规则委托,性能略低。 /// private static readonly ConcurrentDictionary> Cache = new ConcurrentDictionary>(StringComparer.OrdinalIgnoreCase); /// /// 写入或更新规则编译结果。 /// public static void Set(string ruleCode, Func compiledScript) { if (string.IsNullOrWhiteSpace(ruleCode) || compiledScript == null) { return; } Cache[ruleCode] = compiledScript; } /// /// 尝试获取规则编译结果。 /// public static bool TryGet(string ruleCode, out Func compiledScript) { compiledScript = null; if (string.IsNullOrWhiteSpace(ruleCode)) { return false; } return Cache.TryGetValue(ruleCode, out compiledScript); } /// /// 删除指定规则的编译结果。 /// public static void Remove(string ruleCode) { if (string.IsNullOrWhiteSpace(ruleCode)) { return; } Cache.TryRemove(ruleCode, out _); } /// /// 清空全部规则编译结果。 /// public static void Clear() { Cache.Clear(); } } }