Esempio n. 1
0
        /// <summary>
        /// 初始化链接池管理对象
        /// </summary>
        /// <param name="isSession"></param>
        public RedisManager(string sectionName)
        {
            RedisConfigElement redisConfigInfo = GetRedisConfigInfo(sectionName);
            var options = new ConfigurationOptions
            {
                EndPoints   = { { redisConfigInfo.SocketAddress, redisConfigInfo.SecuredPort } },
                Password    = redisConfigInfo.Password,
                SyncTimeout = redisConfigInfo.SyncTimeout
            };

            try
            {
                redis = ConnectionMultiplexer.Connect(options);
                if (redisConfigInfo.CurrentDatabase != 0)
                {
                    db = redisConfigInfo.CurrentDatabase;
                    var logMessage = string.Format("redis  connection ,configname:{0},SocketAddress:{1},db:{2}", sectionName, redisConfigInfo.SocketAddress, db);
                    //LogHelper.Info(logMessage);
                }
                else
                {
                    throw new Exception("redisConfigInfo CurrentDatabase is null " + sectionName);
                }
            }
            catch (Exception ex)
            {
                //LogHelper.Error(string.Format("redis not connection ,configname:{0},points:{1}", sectionName, options.EndPoints));
                throw new Exception(string.Format("redis not connection {0} ,message:{1}", sectionName, ex.Message));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 按照sectionName获取Redis配置信息
        /// </summary>
        /// <param name="sectionName">配置文件中 sectionName</param>
        /// <returns></returns>
        internal static RedisConfigElement GetConfig(string sectionName)
        {
            //XmlReader xr = null;
            //XmlDocument doc = new XmlDocument();
            RedisConfigElement configItem = null;

            try
            {
                RedisConfigInfo section = null;
                //读取项目webconfig中配置redisConfig目录
                string rootpath = AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.LastIndexOf("\\"));
                rootpath = rootpath.Substring(0, rootpath.LastIndexOf("\\"));
                rootpath = rootpath.Substring(0, rootpath.LastIndexOf("\\"));
                string filePath             = rootpath + "\\" + ConfigurationManager.AppSettings["redisConfig"];
                ExeConfigurationFileMap map = new ExeConfigurationFileMap();
                map.ExeConfigFilename = filePath;
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
                if (config.HasFile)
                {
                    //xr = XmlReader.Create(System.IO.File.Open(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read));
                    //doc.Load(xr);
                    //xr.Close();

                    //XmlNodeList listNode = doc.SelectNodes("configuration/redisConfigs/redisConfig");
                    section = config.GetSection("redisConfigs") as RedisConfigInfo;
                    if (section == null)
                    {
                        throw new SystemException("Section  redisConfigs is not found.");
                    }
                    else
                    {
                        foreach (RedisConfigElement item in section.RedisConfigs)
                        {
                            if (item.SectionName == sectionName)
                            {
                                configItem = item;
                            }
                        }
                    }
                }
                else
                {
                    throw new SystemException("redis config file is not found. path:" + filePath);
                }
            }
            catch (Exception exp)
            {
                throw new SystemException("RedisConfigInfo-->RedisConfigInfo:" + exp.StackTrace);
            }

            return(configItem);
        }