ConfigurationCache.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. return result;
  48. }
  49. }
  50. }
  51. return default;
  52. }
  53. public static void Add<T>(ConfigurationCacheType type, string section, T config)
  54. {
  55. Log(LogType.Information, $"- Adding {typeof(T)}({type},{section})");
  56. var _cache = GetCache(type);
  57. if (!_cache.ContainsKey(typeof(T)))
  58. _cache[typeof(T)] = new Dictionary<string, string>();
  59. var subcache = _cache[typeof(T)];
  60. subcache[section] = Serialization.Serialize(config);
  61. }
  62. public static void Clear<T>(ConfigurationCacheType type, string section)
  63. {
  64. Log(LogType.Information, $"- Clearing {typeof(T)}({type},{section})");
  65. var _cache = GetCache(type);
  66. if (_cache.ContainsKey(typeof(T)))
  67. {
  68. var subcache = _cache[typeof(T)];
  69. if (subcache.ContainsKey(section))
  70. {
  71. Log(LogType.Information, $"- Clearing: Removing {typeof(T)}({type},{section})");
  72. subcache[section] = "";
  73. }
  74. }
  75. }
  76. /// <summary>
  77. /// Clear the entire cache for a given cache type <see cref="ConfigurationCacheType" />. Intended for use when the
  78. /// database server has changed and therefore the entire cache needs to be reset.
  79. /// </summary>
  80. /// <param name="type">The type of cache to clear. </param>
  81. public static void ClearAll(ConfigurationCacheType type)
  82. {
  83. var _cache = GetCache(type);
  84. _cache.Clear();
  85. }
  86. }
  87. }