123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using System;
- using WingServerCommon.Log;
- using WingServerCommon.Interfaces.OpLog;
- namespace WingServerCommon.Interfaces.Cache
- {
- public class CacheMaintenance
- {
- private static CacheMaintenance _instance;
- private readonly Dictionary<Type, ICacheManager> _instances;
- public static CacheMaintenance Instance
- {
- get { return _instance ?? (_instance = new CacheMaintenance()); }
- }
- private CacheMaintenance()
- {
- _instances = new Dictionary<Type, ICacheManager>();
- 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);
- }
- }
- /// <summary>
- /// Register a cache manager to cache maintenance
- /// </summary>
- /// <param name="type"></param>
- /// <param name="manager"></param>
- 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<T>()
- {
- ICacheManager manager;
- if (_instances.TryGetValue(typeof(T), out manager))
- {
- return (T)manager;
- }
- throw new Exception("Invalid type");
- }
- }
- }
|