Example #1
0
        /// <summary>
        ///     初始化池,类似于构造方法
        ///     不要重复调用
        /// </summary>
        /// <param name="redisConfig"></param>
        public void Init(RedisConfig redisConfig)
        {
            if (redisConfig == null)
            {
                throw new Exception("传入的redisConfig实例不能空");
            }
            _sectionName     = redisConfig.SectionName;
            _BusyRetry       = redisConfig.BusyRetry;
            _BusyRetryWaitMS = redisConfig.BusyRetryWaitMS;
            if (string.IsNullOrWhiteSpace(_sectionName))
            {
                throw new Exception("redisConfig.SectionName不能为空");
            }

            lock (locker)
            {
                if (SERedisConnectionCache.Exists(_sectionName))
                {
                    return;
                }

                var configStr = GenerateConnectionString(redisConfig);

                if ((redisConfig.PoolSize < 1) || (redisConfig.PoolSize > 100))
                {
                    redisConfig.PoolSize = 1;
                }

                //哨兵特殊处理
                if (redisConfig.Type == RedisConnectType.Sentinel)
                {
                    var sentinel = new SESentinelClient(_sectionName, configStr, redisConfig.PoolSize, redisConfig.Password);

                    sentinel.OnRedisServerChanged += sentinel_OnRedisServerChanged;

                    var operateRedisConnecitonString = sentinel.Start();

                    _SentinelPool.AddOrUpdate(_sectionName + "_" + redisConfig.ServiceName, sentinel, (x, y) => sentinel);

                    SERedisConnectionCache.Init(_sectionName, operateRedisConnecitonString);
                }
                else
                {
                    SERedisConnectionCache.Init(_sectionName, configStr);
                }
            }
        }
Example #2
0
 /// <summary>
 /// 哨兵监测事件
 /// </summary>
 /// <param name="section"></param>
 /// <param name="newConnectionString"></param>
 private void sentinel_OnRedisServerChanged(string section, string newConnectionString)
 {
     SERedisConnectionCache.Init(_sectionName, newConnectionString);
 }