ConfigurationCache.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Dictionary<Type, Dictionary<string, string>> GetCache(ConfigurationCacheType type)
  18. {
  19. return type == ConfigurationCacheType.Local
  20. ? _localcache
  21. : type == ConfigurationCacheType.Global
  22. ? _globalcache
  23. : _usercache;
  24. }
  25. public static T Check<T>(ConfigurationCacheType type, string section)
  26. {
  27. Logger.Send(LogType.Information, "", string.Format("- Checking {0}({1},{2})", typeof(T), type, section));
  28. var _cache = GetCache(type);
  29. if (_cache.ContainsKey(typeof(T)))
  30. {
  31. var subcache = _cache[typeof(T)];
  32. if (subcache.ContainsKey(section))
  33. {
  34. var data = subcache[section];
  35. if (!string.IsNullOrWhiteSpace(data))
  36. {
  37. Logger.Send(LogType.Information, "",
  38. string.Format("- Checking: Found {0}({1},{2}) -> {3}", typeof(T), type, section, subcache[section]));
  39. var result = Serialization.Deserialize<T>(subcache[section]);
  40. return result;
  41. }
  42. }
  43. }
  44. return default(T);
  45. }
  46. public static void Add<T>(ConfigurationCacheType type, string section, T config)
  47. {
  48. Logger.Send(LogType.Information, "", string.Format("- Adding {0}({1},{2})", typeof(T), type, section));
  49. var _cache = GetCache(type);
  50. if (!_cache.ContainsKey(typeof(T)))
  51. _cache[typeof(T)] = new Dictionary<string, string>();
  52. var subcache = _cache[typeof(T)];
  53. subcache[section] = Serialization.Serialize(config);
  54. }
  55. public static void Clear<T>(ConfigurationCacheType type, string section)
  56. {
  57. Logger.Send(LogType.Information, "", string.Format("- Clearing {0}({1},{2})", typeof(T), type, section));
  58. var _cache = GetCache(type);
  59. if (_cache.ContainsKey(typeof(T)))
  60. {
  61. var subcache = _cache[typeof(T)];
  62. if (subcache.ContainsKey(section))
  63. {
  64. Logger.Send(LogType.Information, "", string.Format("- Clearing: Removing {0}({1},{2})", typeof(T), type, section));
  65. subcache[section] = "";
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// Clear the entire cache for a given cache type <see cref="ConfigurationCacheType" />. Intended for use when the
  71. /// database server has changed and therefore the entire cache needs to be reset.
  72. /// </summary>
  73. /// <param name="type">The type of cache to clear. </param>
  74. public static void ClearAll(ConfigurationCacheType type)
  75. {
  76. var _cache = GetCache(type);
  77. _cache.Clear();
  78. }
  79. }
  80. }