Ejemplo n.º 1
0
 /// <summary>
 /// Redis 配置
 /// </summary>
 /// <param name="doc"></param>
 /// <param name="redis"></param>
 /// <returns></returns>
 public static XmlDocument SetRedisConfig(XmlDocument doc, RedisConfig redis)
 {
     doc = GetXmlDocument(doc, "configuration");
     if (redis == null) return doc;
     var serverElement = GetRedisElement(doc, redis);
     var redisElement = doc.CreateElement("redis");
     redisElement.AppendChild(serverElement);
     doc.AppendChild(redisElement);
     return doc;
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 获取Redis在配置文件中的节点
 /// </summary>
 /// <param name="doc"></param>
 /// <param name="redis"></param>
 /// <returns></returns>
 private static XmlElement GetRedisElement(XmlDocument doc, RedisConfig redis)
 {
     if (redis.MaxReadPoolSize == default(int)) redis.MaxReadPoolSize = 5;
     if (redis.MaxWritePoolSize == default(int)) redis.MaxWritePoolSize = 5;
     var serverElement = doc.CreateElement("server");
     serverElement.SetAttribute("name", redis.Name);
     if (redis.ReadUris != null && redis.ReadUris.Count > 0)
     {
         var readElement = doc.CreateElement("read");
         foreach (var uri in redis.ReadUris)
         {
             var addElement = doc.CreateElement("add");
             addElement.SetAttribute("uri", uri.ToString());
             readElement.AppendChild(addElement);
         }
         serverElement.AppendChild(readElement);
     }
     if (redis.WriteUris != null && redis.WriteUris.Count > 0)
     {
         var writeElement = doc.CreateElement("write");
         foreach (var uri in redis.WriteUris)
         {
             var addElement = doc.CreateElement("add");
             addElement.SetAttribute("uri", uri.ToString());
             writeElement.AppendChild(addElement);
         }
         serverElement.AppendChild(writeElement);
     }
     var socketPoolElement = doc.CreateElement("socketPool");
     socketPoolElement.SetAttribute("maxReadPoolSize", redis.MaxReadPoolSize.ToString());
     socketPoolElement.SetAttribute("maxWritePoolSize", redis.MaxWritePoolSize.ToString());
     socketPoolElement.SetAttribute("autoStart", redis.AutoStart.ToString());
     serverElement.AppendChild(socketPoolElement);
     return serverElement;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// 读取Redis配置
 /// </summary>
 /// <param name="zone"></param>
 /// <returns></returns>
 public static List<RedisConfig> GetRedisConfig(int zone)
 {
     var result = new List<RedisConfig>();
     var path = GetEndpointPath(zone);
     var doc = XmlHelper.GetXmlDocCache(path);
     var nodes = doc.SelectNodes("/configuration/redis/server");
     if (nodes == null || nodes.Count <= 0) return result;
     foreach (XmlNode node in nodes)
     {
         var redis = new RedisConfig
         {
             Name = XmlHelper.GetXmlAttributeValue(node, "name", string.Empty),
             ReadUris = new List<Uri>(),
             WriteUris = new List<Uri>()
         };
         foreach (XmlNode item in node.ChildNodes)
         {
             switch (item.Name)
             {
                 case "read":
                     redis.ReadUris.AddRange(from XmlNode rcnode in item.ChildNodes select new Uri(XmlHelper.GetXmlAttributeValue(rcnode, "uri", string.Empty)));
                     break;
                 case "write":
                     redis.WriteUris.AddRange(from XmlNode rcnode in item.ChildNodes select new Uri(XmlHelper.GetXmlAttributeValue(rcnode, "uri", string.Empty)));
                     break;
                 case "socketPool":
                     redis.AutoStart = XmlHelper.GetXmlAttributeValue(item, "autoStart", true);
                     redis.MaxReadPoolSize = XmlHelper.GetXmlAttributeValue(item, "maxReadPoolSize", 5);
                     redis.MaxWritePoolSize = XmlHelper.GetXmlAttributeValue(item, "maxWritePoolSize", 5);
                     break;
             }
         }
         result.Add(redis);
     }
     return result;
 }