using System; using System.Collections.Concurrent; using System.Collections.Generic; namespace Admin.Core.Common.Helper { public class CacheHelper { private readonly static ConcurrentDictionary _registry = new ConcurrentDictionary(); #region 注册缓存 /// /// 注册缓存 /// /// /// /// public static void RegisterCache(string definition,string json) { if (_registry.ContainsKey($"{definition}")) { throw new InvalidOperationException($"Cache {definition} is already registered"); } lock (_registry) { _registry[$"{definition}"] = json; } } #endregion #region 是否注册 /// /// 是否注册 /// /// /// /// public static bool IsRegistered(string definition) { return _registry.ContainsKey($"{definition}"); } #endregion #region 查询 /// /// 是否注册 /// /// /// /// public static string GetDefinition(string definition) { if (!_registry.ContainsKey($"{definition}")) return default; return _registry[$"{definition}"]; } #endregion #region 注销 /// /// 注销 /// /// /// public static void DeregisterCache(string definition) { try { if (!_registry.ContainsKey($"{definition}")) return; lock (_registry) { _registry.TryRemove($"{definition}", out var _); } } catch (Exception) { throw; } } #endregion #region 获取所有定义的缓存 /// /// 获取所有定义的缓存 /// /// public static IEnumerable GetAllDefinitions() { return _registry.Values; } #endregion #region 清空所有缓存 /// /// 清空所有缓存 /// /// public static bool ClearAllDefinitions() { try { _registry.Clear(); return true; } catch (Exception) { return false; } } #endregion } }