public static void UpdateConfig(ConnectionConfig config) { List <ConnectionConfig> configs = JsonConfigUtil.GetConfigObject <List <ConnectionConfig> >(PathConnections); //检查旧名称 if (configs == null || !configs.Exists(p => p.Id.Equals(config.Id))) { throw new KeyNotFoundException($"配置文件中已丢失原配置信息。"); } //检查新名称 List <ConnectionConfig> tmps = configs.Where(p => p.Id.Equals(config.Id) == false).ToList(); if (tmps != null && tmps.Exists(p => p.Name.Equals(config.Name))) { throw new DuplicateMemberException($"已存在名为 {config.Name} 的配置信息。"); } ConnectionConfig tmp = configs.Find(p => p.Id.Equals(config.Id)); tmp.IP = config.IP; tmp.Name = config.Name; tmp.Password = config.Password; tmp.Port = config.Port; JsonConfigUtil.SetConfigObject <List <ConnectionConfig> >(PathConnections, configs); DictConnectionConfig[config.Id] = config; string host = (string.IsNullOrEmpty(config.Password) ? "" : $"{config.Password}@") + $"{config.IP}:{config.Port}"; string[] hosts = new string[] { host }; RedisCaches[config.Id] = new RedisCache(hosts, hosts); }
/// <summary> /// 移除配置信息。 /// </summary> /// <param name="id"></param> public static void RemoveConfig(string id) { List <ConnectionConfig> configs = JsonConfigUtil.GetConfigObject <List <ConnectionConfig> >(PathConnections); int index = configs.FindIndex(p => p.Id.Equals(id)); configs.RemoveAt(index); JsonConfigUtil.SetConfigObject <List <ConnectionConfig> >(PathConnections, configs); DictConnectionConfig.Remove(id); RedisCaches.Remove(id); }
/// <summary> /// 保存配置信息。 /// </summary> /// <param name="config"></param> /// <exception cref="DuplicateMemberException">成员重复异常</exception> public static void SaveConfig(ConnectionConfig config) { List <ConnectionConfig> configs = JsonConfigUtil.GetConfigObject <List <ConnectionConfig> >(PathConnections); if (configs == null) { configs = new List <ConnectionConfig>(); } if (configs.Exists(p => p.Name.Equals(config.Name))) { throw new DuplicateMemberException($"已存在名为 {config.Name} 的配置信息。"); } configs.Add(config); JsonConfigUtil.SetConfigObject <List <ConnectionConfig> >(PathConnections, configs); DictConnectionConfig.Add(config.Id, config); string host = (string.IsNullOrEmpty(config.Password) ? "" : $"{config.Password}@") + $"{config.IP}:{config.Port}"; string[] hosts = new string[] { host }; RedisCaches.Add(config.Id, new RedisCache(hosts, hosts)); }