ConfigurationCache.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using InABox.Core;
  4. namespace InABox.Configuration
  5. {
  6. public enum ConfigurationCacheType
  7. {
  8. Local,
  9. Global,
  10. User
  11. }
  12. public static class ConfigurationCache
  13. {
  14. private static readonly Dictionary<Type, Dictionary<string, string>> _localcache = new Dictionary<Type, Dictionary<string, string>>();
  15. private static readonly Dictionary<Type, Dictionary<string, string>> _globalcache = new Dictionary<Type, Dictionary<string, string>>();
  16. private static readonly Dictionary<Type, Dictionary<string, string>> _usercache = new Dictionary<Type, Dictionary<string, string>>();
  17. private static readonly bool _shouldLog = false;
  18. private static Dictionary<Type, Dictionary<string, string>> GetCache(ConfigurationCacheType type)
  19. {
  20. return type == ConfigurationCacheType.Local
  21. ? _localcache
  22. : type == ConfigurationCacheType.Global
  23. ? _globalcache
  24. : _usercache;
  25. }
  26. private static void Log(LogType type, string message)
  27. {
  28. if (_shouldLog)
  29. {
  30. Logger.Send(type, "", message);
  31. }
  32. }
  33. public static T Check<T>(ConfigurationCacheType type, string section)
  34. {
  35. Log(LogType.Information, $"- Checking {typeof(T)}({type},{section})");
  36. var _cache = GetCache(type);
  37. if (_cache.ContainsKey(typeof(T)))
  38. {
  39. var subcache = _cache[typeof(T)];
  40. if (subcache.ContainsKey(section))
  41. {
  42. var data = subcache[section];
  43. if (!string.IsNullOrWhiteSpace(data))
  44. {
  45. Log(LogType.Information, $"- Checking: Found {typeof(T)}({type},{section}) -> {subcache[section]}");
  46. var result = Serialization.Deserialize<T>(subcache[section]);
  47. if(result is BaseObject obj)
  48. {
  49. obj.CommitChanges();
  50. }
  51. return result;
  52. }
  53. }
  54. }
  55. return default;
  56. }
  57. public static void Add<T>(ConfigurationCacheType type, string section, T config)
  58. {
  59. Log(LogType.Information, $"- Adding {typeof(T)}({type},{section})");
  60. var _cache = GetCache(type);
  61. if (!_cache.ContainsKey(typeof(T)))
  62. _cache[typeof(T)] = new Dictionary<string, string>();
  63. var subcache = _cache[typeof(T)];
  64. subcache[section] = Serialization.Serialize(config);
  65. }
  66. public static void Clear<T>(ConfigurationCacheType type, string section)
  67. {
  68. Log(LogType.Information, $"- Clearing {typeof(T)}({type},{section})");
  69. var _cache = GetCache(type);
  70. if (_cache.ContainsKey(typeof(T)))
  71. {
  72. var subcache = _cache[typeof(T)];
  73. if (subcache.ContainsKey(section))
  74. {
  75. Log(LogType.Information, $"- Clearing: Removing {typeof(T)}({type},{section})");
  76. subcache[section] = "";
  77. }
  78. }
  79. }
  80. /// <summary>
  81. /// Clear the entire cache for a given cache type <see cref="ConfigurationCacheType" />. Intended for use when the
  82. /// database server has changed and therefore the entire cache needs to be reset.
  83. /// </summary>
  84. /// <param name="type">The type of cache to clear. </param>
  85. public static void ClearAll(ConfigurationCacheType type)
  86. {
  87. var _cache = GetCache(type);
  88. _cache.Clear();
  89. }
  90. }
  91. }