Пример #1
0
        private IRedisClient GetRedisClient(string sectionName = "RedisConfig")
        {
            PooledRedisClientManager pcm;

            if (!_pcms.TryGetValue(sectionName, out pcm))
            {
                var      redisConfigInfo = RedisConfigInfo.GetConfig(sectionName);
                string[] writeServerList = SplitString(redisConfigInfo.WriteServerList, ",");
                string[] readServerList  = SplitString(redisConfigInfo.ReadServerList, ",");
                pcm = new PooledRedisClientManager(readServerList, writeServerList,
                                                   new RedisClientManagerConfig
                {
                    MaxWritePoolSize = redisConfigInfo.MaxWritePoolSize,
                    MaxReadPoolSize  = redisConfigInfo.MaxReadPoolSize,
                    AutoStart        = redisConfigInfo.AutoStart,
                    DefaultDb        = redisConfigInfo.DefaultDb,
                });
                pcm.ConnectTimeout = 2 * 60 * 1000;
                _pcms[sectionName] = pcm;
            }

            if (pcm == null)
            {
                throw new InvalidOperationException("Redis尚未初始化或失败,无法打开客户端!");
            }


            IRedisClient client = null;

            try
            {
                client = pcm.GetClient();//获取连接;
            }
            catch (TimeoutException)
            {
                client = pcm.GetClient();
            }
            catch (Exception ex)
            {
                return(null);
            }
            return(client);//获取连接;
        }
Пример #2
0
        public static RedisConfigInfo GetConfig()
        {
            RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig");

            return(section);
        }