using System; using WingServerCommon.Log; using WingServerCommon.Interfaces.OpLog; namespace WingServerCommon.Interfaces.Cache { public class CacheMaintenance { private static CacheMaintenance _instance; private readonly Dictionary _instances; public static CacheMaintenance Instance { get { return _instance ?? (_instance = new CacheMaintenance()); } } private CacheMaintenance() { _instances = new Dictionary(); Register(typeof(IDeviceInfosManager), new DeviceInfosManager()); Register(typeof(IOrganizationsManager), new OrganizationsManager()); Register(typeof(IDiagnosisModulesManager), new DiagnosisModulesManager()); Register(typeof(IReportPostersManager), new ReportPostersManager()); Register(typeof(IDistributedServerInfosManager), new DistributedServerInfosManager()); Register(typeof(ITokensManager), new TokensManager()); Register(typeof(IIPManager), new IPManager()); Register(typeof(IConsultationRecordManager), new ConsultationRecordManager()); Register(typeof(ICourseInfosManager), new CourseInfosManager()); Register(typeof(IUserInfoManager), new UserInfoManager()); Register(typeof(IGradingProtectionConfigManager), new GradingProtectionConfigManager()); Register(typeof(IControllingParametersManager), new ControllingParametersManager()); DatabaseCollection.CollectionDocumentChanged += OnCollectionDocumentChanged; } private void OnCollectionDocumentChanged(Object sender, CollectionDocumentChangedArgs args) { var typeName = $"I{args.CollectionName}Manager"; //var type = Type.GetType(typeName); var type = _instances.FirstOrDefault(i => i.Key.Name == typeName).Key; if (type == null) { // Logger.WriteLineWarn($"CacheMaintenance manager never existed, typeName:{typeName}"); return; } ICacheManager manager; if (_instances.TryGetValue(type, out manager)) { manager.RemoveMany(args); } } /// /// Register a cache manager to cache maintenance /// /// /// private void Register(Type type, ICacheManager manager) { if (_instances.ContainsKey(typeof(Type))) { Logger.WriteLineWarn("Register duplicate type"); } else { _instances.Add(type, manager); } } public T Get() { ICacheManager manager; if (_instances.TryGetValue(typeof(T), out manager)) { return (T)manager; } throw new Exception("Invalid type"); } } }