12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using System.Collections.Concurrent;
- using WingServerCommon.Config.Parameters;
- namespace WingServerCommon.Config
- {
- public class CommonConfigManager
- {
- private static ConcurrentDictionary<string, IEnumerable<IParameter>> _parametersDictionary = new ConcurrentDictionary<string, IEnumerable<IParameter>>();
- public static void AddParamters(string key, string path)
- {
- var _parameters = ParameterSerializer.Deserialize(path);
- _parametersDictionary.AddOrUpdate(key, _parameters, (k, oldParameters) => _parameters);
- }
- /// <summary>
- /// 获取缓存信息
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static IEnumerable<IParameter> Get(string key)
- {
- if (_parametersDictionary.TryGetValue(key, out IEnumerable<IParameter> obj))
- {
- return obj;
- }
- return default;
- }
- /// <summary>
- /// Get parameter by specifed section name and parameter name
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public static T GetParammeter<T>(string key, string sectionName, string paramName) where T : class
- {
- var _parameters = Get(key);
- var parameter = _parameters.FirstOrDefault(x => x.Section == sectionName && x.Name == paramName) as T;
- if (parameter != null)
- {
- return parameter;
- }
- throw new System.ArgumentException($"{sectionName} - {paramName} not set correct");
- }
- }
- }
|