using System; using Newtonsoft.Json; using System.Globalization; using WingInterfaceLibrary.Enum; using System.Collections.Concurrent; namespace WingDeviceService.Common { public class CacheManager : IDisposable where TObject : class { private ConcurrentDictionary _source = new(); /// /// 添加/更新缓存 /// /// /// public TObject AddOrUpdate(string key, TObject obj) { return _source.AddOrUpdate(key, obj, (k, v) => obj); } /// /// 删除缓存 /// /// /// public bool Remove(string key) { return _source.TryRemove(key, out _); } /// /// 获取缓存信息 /// /// /// public TObject Get(string key) { if (_source.TryGetValue(key, out TObject obj)) { return obj; } return default; } /// /// 清空缓存 /// public void Dispose() { _source.Clear(); } } public class CacheManagerSingle where TObject : class { private CacheManagerSingle() { } private readonly static object _locker = new(); private static CacheManager _instance; /// /// 单例对象 /// /// public static CacheManager Instance { get { if (_instance == null) { lock (_locker) { if (_instance == null) { _instance = new CacheManager(); } } } return _instance; } } } }