CommonConfigManager.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.Collections.Concurrent;
  2. using WingServerCommon.Config.Parameters;
  3. namespace WingServerCommon.Config
  4. {
  5. public class CommonConfigManager
  6. {
  7. private static ConcurrentDictionary<string, IEnumerable<IParameter>> _parametersDictionary = new ConcurrentDictionary<string, IEnumerable<IParameter>>();
  8. public static void AddParamters(string key, string path)
  9. {
  10. var _parameters = ParameterSerializer.Deserialize(path);
  11. _parametersDictionary.AddOrUpdate(key, _parameters, (k, oldParameters) => _parameters);
  12. }
  13. /// <summary>
  14. /// 获取缓存信息
  15. /// </summary>
  16. /// <param name="key"></param>
  17. /// <returns></returns>
  18. public static IEnumerable<IParameter> Get(string key)
  19. {
  20. if (_parametersDictionary.TryGetValue(key, out IEnumerable<IParameter> obj))
  21. {
  22. return obj;
  23. }
  24. return default;
  25. }
  26. /// <summary>
  27. /// Get parameter by specifed section name and parameter name
  28. /// </summary>
  29. /// <typeparam name="T"></typeparam>
  30. public static T GetParammeter<T>(string key, string sectionName, string paramName) where T : class
  31. {
  32. var _parameters = Get(key);
  33. var parameter = _parameters.FirstOrDefault(x => x.Section == sectionName && x.Name == paramName) as T;
  34. if (parameter != null)
  35. {
  36. return parameter;
  37. }
  38. throw new System.ArgumentException($"{sectionName} - {paramName} not set correct");
  39. }
  40. }
  41. }