CacheMaintenance.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using WingServerCommon.Log;
  3. using WingServerCommon.Interfaces.OpLog;
  4. namespace WingServerCommon.Interfaces.Cache
  5. {
  6. public class CacheMaintenance
  7. {
  8. private static CacheMaintenance _instance;
  9. private readonly Dictionary<Type, ICacheManager> _instances;
  10. public static CacheMaintenance Instance
  11. {
  12. get { return _instance ?? (_instance = new CacheMaintenance()); }
  13. }
  14. private CacheMaintenance()
  15. {
  16. _instances = new Dictionary<Type, ICacheManager>();
  17. Register(typeof(IDeviceInfosManager), new DeviceInfosManager());
  18. Register(typeof(IOrganizationsManager), new OrganizationsManager());
  19. Register(typeof(IDiagnosisModulesManager), new DiagnosisModulesManager());
  20. Register(typeof(IReportPostersManager), new ReportPostersManager());
  21. Register(typeof(IDistributedServerInfosManager), new DistributedServerInfosManager());
  22. Register(typeof(ITokensManager), new TokensManager());
  23. Register(typeof(IIPManager), new IPManager());
  24. Register(typeof(IConsultationRecordManager), new ConsultationRecordManager());
  25. Register(typeof(ICourseInfosManager), new CourseInfosManager());
  26. Register(typeof(IUserInfoManager), new UserInfoManager());
  27. Register(typeof(IGradingProtectionConfigManager), new GradingProtectionConfigManager());
  28. Register(typeof(IControllingParametersManager), new ControllingParametersManager());
  29. DatabaseCollection.CollectionDocumentChanged += OnCollectionDocumentChanged;
  30. }
  31. private void OnCollectionDocumentChanged(Object sender, CollectionDocumentChangedArgs args)
  32. {
  33. var typeName = $"I{args.CollectionName}Manager";
  34. //var type = Type.GetType(typeName);
  35. var type = _instances.FirstOrDefault(i => i.Key.Name == typeName).Key;
  36. if (type == null)
  37. {
  38. // Logger.WriteLineWarn($"CacheMaintenance manager never existed, typeName:{typeName}");
  39. return;
  40. }
  41. ICacheManager manager;
  42. if (_instances.TryGetValue(type, out manager))
  43. {
  44. manager.RemoveMany(args);
  45. }
  46. }
  47. /// <summary>
  48. /// Register a cache manager to cache maintenance
  49. /// </summary>
  50. /// <param name="type"></param>
  51. /// <param name="manager"></param>
  52. private void Register(Type type, ICacheManager manager)
  53. {
  54. if (_instances.ContainsKey(typeof(Type)))
  55. {
  56. Logger.WriteLineWarn("Register duplicate type");
  57. }
  58. else
  59. {
  60. _instances.Add(type, manager);
  61. }
  62. }
  63. public T Get<T>()
  64. {
  65. ICacheManager manager;
  66. if (_instances.TryGetValue(typeof(T), out manager))
  67. {
  68. return (T)manager;
  69. }
  70. throw new Exception("Invalid type");
  71. }
  72. }
  73. }