Esempio n. 1
0
        /// <summary>
        /// 建立redis cluster 本地映射
        /// </summary>
        private void GetClusterMap()
        {
            var clusterNodes = ClusterNodes;

            if (clusterNodes != null && clusterNodes.Any())
            {
                foreach (var item in clusterNodes)
                {
                    if (!RedisConnectionManager.Exsits(item.IPPort))
                    {
                        var cnn = new RedisConnection(item.IPPort);
                        cnn.OnRedirect     += _redisConnection_OnRedirect;
                        cnn.OnDisconnected += _cnn_OnDisconnected;
                        cnn.Connect();
                        if (!string.IsNullOrEmpty(RedisConfig.Passwords))
                        {
                            cnn.Auth(RedisConfig.Passwords);
                        }
                        var isMaster = item.IsMaster;
                        cnn.RedisServerType = isMaster ? RedisServerType.ClusterMaster : RedisServerType.ClusterSlave;
                        RedisConnectionManager.Set(item.IPPort, isMaster, cnn);
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// redis cluster中重置连接事件
        /// </summary>
        /// <param name="ipPort"></param>
        /// <returns></returns>
        private RedisConnection _redisDataBase_OnRedirect(string ipPort)
        {
            var cnn = RedisConnectionManager.Get(ipPort);

            if (cnn != null)
            {
                return((RedisConnection)cnn);
            }
            else
            {
                this.IsConnected = false;
                this.RedisConfig = new RedisConfig(ipPort, this.RedisConfig.Passwords, this.RedisConfig.ActionTimeOut);

                if (_debugModel)
                {
                    _cnn = new RedisConnectionDebug(RedisConfig.GetIPPort(), this.RedisConfig.ActionTimeOut);
                }
                else
                {
                    _cnn = new RedisConnection(RedisConfig.GetIPPort(), this.RedisConfig.ActionTimeOut);
                }

                _cnn.OnRedirect     += _redisConnection_OnRedirect;
                _cnn.OnDisconnected += _cnn_OnDisconnected;
                this.Connect();
                RedisConnectionManager.Set(ipPort, _cnn);
                return(_cnn);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 使用密码连接到RedisServer
        /// </summary>
        /// <returns></returns>
        public string Connect()
        {
            lock (_syncLocker)
            {
                if (!IsConnected)
                {
                    _cnn.Connect();

                    IsConnected = _cnn.IsConnected;

                    var infoMsg = Info();

                    if (infoMsg.Contains(RedisConst.NOAuth))
                    {
                        if (string.IsNullOrEmpty(RedisConfig.Passwords))
                        {
                            _cnn.Quit();
                            return(infoMsg);
                        }

                        var authMsg = Auth(RedisConfig.Passwords);

                        if (string.Compare(authMsg, OK, true) != 0)
                        {
                            _cnn.Quit();
                            return(authMsg);
                        }
                    }
                }
                _cnn.OnRedirect += _redisConnection_OnRedirect;
                _cnn.KeepAlived(() => this.KeepAlive());
                var ipPort = RedisConfig.GetIPPort();

                var isMaster  = this.IsMaster;
                var isCluster = this.IsCluster;

                if (isCluster)
                {
                    _cnn.RedisServerType = isMaster ? RedisServerType.ClusterMaster : RedisServerType.ClusterSlave;
                    GetClusterMap(ipPort);
                }
                else
                {
                    _cnn.RedisServerType = isMaster ? RedisServerType.Master : RedisServerType.Slave;
                    RedisConnectionManager.Set(ipPort, _cnn);
                }
                return(OK);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 建立redis cluster 本地映射
        /// </summary>
        /// <param name="ipPort"></param>
        private void GetClusterMap(string ipPort)
        {
            var clusterNodes = ClusterNodes;

            RedisConnectionManager.SetClusterNodes(clusterNodes);

            foreach (var item in clusterNodes)
            {
                if (!RedisConnectionManager.Exsits(item.IPPort))
                {
                    var cnn = new RedisConnection(item.IPPort);
                    cnn.Connect();
                    cnn.RedisServerType = item.IsMaster ? RedisServerType.ClusterMaster : RedisServerType.ClusterSlave;
                    RedisConnectionManager.Set(item.IPPort, cnn);
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 建立redis cluster 本地映射
        /// </summary>
        /// <param name="ipPort"></param>
        private void GetClusterMap(string ipPort)
        {
            var clusterNodes = ClusterNodes;

            RedisConnectionManager.SetClusterNodes(clusterNodes);

            foreach (var item in clusterNodes)
            {
                if (!RedisConnectionManager.Exsits(item.IPPort))
                {
                    TaskHelper.Start(() =>
                    {
                        var cnn             = new RedisConnection(item.IPPort);
                        cnn.OnRedirect     += _redisConnection_OnRedirect;
                        cnn.OnDisconnected += _cnn_OnDisconnected;
                        cnn.Connect();
                        cnn.RedisServerType = item.IsMaster ? RedisServerType.ClusterMaster : RedisServerType.ClusterSlave;
                        RedisConnectionManager.Set(item.IPPort, cnn);
                    });
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// redis cluster中重置连接事件
        /// </summary>
        /// <param name="ipPort"></param>
        /// <param name="operationType"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private Interface.IResult _redisConnection_OnRedirect(string ipPort, OperationType operationType, params object[] args)
        {
            var cnn = RedisConnectionManager.Get(ipPort);

            if (cnn == null)
            {
                this.IsConnected = false;
                this.RedisConfig = new RedisConfig(ipPort, this.RedisConfig.Passwords, this.RedisConfig.ActionTimeOut);
                if (_debugModel)
                {
                    cnn = new RedisConnectionDebug(RedisConfig.GetIPPort(), this.RedisConfig.ActionTimeOut);
                }
                else
                {
                    cnn = new RedisConnection(RedisConfig.GetIPPort(), this.RedisConfig.ActionTimeOut);
                }
                cnn.OnRedirect     += _redisConnection_OnRedirect;
                cnn.OnDisconnected += _cnn_OnDisconnected;
                this.Connect();
                RedisConnectionManager.Set(ipPort, cnn);
            }

            _cnn = cnn;


            switch (operationType)
            {
            case OperationType.Do:
                return(_cnn.Do((RequestType)args[0]));

            case OperationType.DoBatchWithDic:
                return(_cnn.DoBatchWithDic((RequestType)args[0], (Dictionary <string, string>)args[1]));

            case OperationType.DoBatchWithIDDic:
                return(_cnn.DoBatchWithIDDic((RequestType)args[0], (string)args[1], (Dictionary <double, string>)args[2]));

            case OperationType.DoBatchWithIDKeys:
                return(_cnn.DoBatchWithIDKeys((RequestType)args[0], (string)args[1], (string[])args[3]));

            case OperationType.DoBatchWithParams:
                return(_cnn.DoBatchWithParams((RequestType)args[0], (string[])args[1]));

            case OperationType.DoCluster:
                return(_cnn.DoCluster((RequestType)args[0], (object[])args[1]));

            case OperationType.DoClusterSetSlot:
                return(_cnn.DoClusterSetSlot((RequestType)args[0], (string)args[1], (int)args[2], (string)args[3]));

            case OperationType.DoExpire:
                _cnn.DoExpire((string)args[0], (int)args[1]);
                break;

            case OperationType.DoExpireInsert:
                _cnn.DoExpireInsert((RequestType)args[0], (string)args[1], (string)args[2], (int)args[3]);
                break;

            case OperationType.DoHash:
                return(_cnn.DoHash((RequestType)args[0], (string)args[1], (string)args[2], (string)args[3]));

            case OperationType.DoInOne:
                return(_cnn.DoInOne((RequestType)args[0], (string)args[1]));

            case OperationType.DoRang:
                return(_cnn.DoRang((RequestType)args[0], (string)args[1], (double)args[2], (double)args[3]));

            case OperationType.DoScan:
                return(_cnn.DoScan((RequestType)args[0], (int)args[1], (string)args[2], (int)args[3]));

            case OperationType.DoScanKey:
                return(_cnn.DoScanKey((RequestType)args[0], (string)args[1], (int)args[2], (string)args[3], (int)args[4]));

            case OperationType.DoSub:
                _cnn.DoSub((string[])args[0], (Action <string, string>)args[1]);
                break;

            case OperationType.DoWithKey:
                return(_cnn.DoWithKey((RequestType)args[0], (string)args[1]));

            case OperationType.DoWithKeyValue:
                return(_cnn.DoWithKeyValue((RequestType)args[0], (string)args[1], (string)args[2]));

            default:
                return(null);
            }
            return(null);
        }
Esempio n. 7
0
        /// <summary>
        /// redis cluster中重置连接事件
        /// </summary>
        /// <param name="ipPort"></param>
        /// <param name="operationType"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private IResult _redisConnection_OnRedirect(string ipPort, OperationType operationType, params object[] args)
        {
            var cnn = RedisConnectionManager.Get(ipPort);

            var requestType = (RequestType)args[0];

            switch (requestType)
            {
            case RequestType.GET:
            case RequestType.EXISTS:
            case RequestType.SCAN:
            case RequestType.TTL:
            case RequestType.PTTL:
            case RequestType.TYPE:
            case RequestType.HGET:
            case RequestType.HEXISTS:
            case RequestType.HLEN:
            case RequestType.SMEMBERS:
            case RequestType.SISMEMBER:
            case RequestType.SSCAN:
            case RequestType.HSCAN:
            case RequestType.ZSCAN:
            case RequestType.ZSCORE:
            case RequestType.RANDOMKEY:
            case RequestType.ZLEXCOUNT:
            case RequestType.ZCARD:
            case RequestType.ZCOUNT:
            case RequestType.ZRANGE:
            case RequestType.ZRANGEBYLEX:
            case RequestType.ZRANGEBYSCORE:
            case RequestType.ZRANK:
            case RequestType.ZREVRANGE:
            case RequestType.ZREVRANGEBYSCORE:
            case RequestType.ZREVRANK:
            case RequestType.LINDEX:
            case RequestType.LLEN:

                var slave = RedisConnectionManager.Get(ipPort, false);

                if (slave != null)
                {
                    cnn = slave;
                }
                break;
            }

            if (cnn == null || !cnn.IsConnected)
            {
                this.IsConnected = false;
                this.RedisConfig = new RedisConfig(ipPort, this.RedisConfig.Passwords, this.RedisConfig.ActionTimeOut);
                if (_debugModel)
                {
                    cnn = new RedisConnectionDebug(RedisConfig.GetIPPort(), this.RedisConfig.ActionTimeOut);
                }
                else
                {
                    cnn = new RedisConnection(RedisConfig.GetIPPort(), this.RedisConfig.ActionTimeOut);
                }
                cnn.OnRedirect     += _redisConnection_OnRedirect;
                cnn.OnDisconnected += _cnn_OnDisconnected;
                if (cnn.Connect())
                {
                    if (!string.IsNullOrEmpty(this.RedisConfig.Passwords))
                    {
                        cnn.Auth(this.RedisConfig.Passwords);
                    }
                    _cnn             = cnn;
                    this.IsConnected = true;
                    RedisConnectionManager.Set(ipPort, IsMaster, cnn);
                }
                else
                {
                    throw new Exception($"当前节点不可达ipport:{ipPort}");
                }
            }
            else
            {
                _cnn = cnn;
            }

            switch (operationType)
            {
            case OperationType.Do:
                return(_cnn.Do((RequestType)args[0]));

            case OperationType.DoBatchWithList:
                return(_cnn.DoMultiLineWithList((RequestType)args[0], (string)args[1], (List <string>)args[2]));

            case OperationType.DoBatchWithDic:
                return(_cnn.DoMultiLineWithDic((RequestType)args[0], (Dictionary <string, string>)args[1]));

            case OperationType.DoBatchWithIDDic:
                return(_cnn.DoBatchWithIDDic((RequestType)args[0], (string)args[1], (Dictionary <string, string>)args[2]));

            case OperationType.DoBatchZaddWithIDDic:
                return(_cnn.DoBatchZaddWithIDDic((RequestType)args[0], (string)args[1], (Dictionary <double, string>)args[2]));

            case OperationType.DoBatchWithIDKeys:
                return(_cnn.DoBatchWithIDKeys((RequestType)args[0], (string)args[1], (string[])args[3]));

            case OperationType.DoBatchWithParams:
                return(_cnn.DoWithMutiParams((RequestType)args[0], (string[])args[1]));

            case OperationType.DoCluster:
                return(_cnn.DoMutiCmd((RequestType)args[0], (object[])args[1]));

            case OperationType.DoClusterSetSlot:
                return(_cnn.DoClusterSetSlot((RequestType)args[0], (string)args[1], (int)args[2], (string)args[3]));

            case OperationType.DoExpire:
                return(_cnn.DoExpire((string)args[0], (int)args[1]));

            case OperationType.DoExpireAt:
                return(_cnn.DoExpireAt((string)args[0], (int)args[1]));

            case OperationType.DoExpireInsert:
                return(_cnn.DoExpireInsert((RequestType)args[0], (string)args[1], (string)args[2], (int)args[3]));

            case OperationType.DoWithID:
                return(_cnn.DoWithID((RequestType)args[0], (string)args[1], (string)args[2], (string)args[3]));

            case OperationType.DoRang:
                return(_cnn.DoRang((RequestType)args[0], (string)args[1], (double)args[2], (double)args[3]));

            case OperationType.DoRangByScore:
                return(_cnn.DoRangByScore((RequestType)args[0], (string)args[1], (double)args[2], (double)args[3], (RangType)args[4], (long)args[5], (int)args[6], (bool)args[7]));

            case OperationType.DoScan:
                return(_cnn.DoScan((RequestType)args[0], (int)args[1], (string)args[2], (int)args[3]));

            case OperationType.DoScanKey:
                return(_cnn.DoScanKey((RequestType)args[0], (string)args[1], (int)args[2], (string)args[3], (int)args[4]));

            case OperationType.DoSub:
                _cnn.DoSub((string[])args[0], (Action <string, string>)args[1]);
                break;

            case OperationType.DoWithKey:
                return(_cnn.DoWithKey((RequestType)args[0], (string)args[1]));

            case OperationType.DoWithKeyValue:
                return(_cnn.DoWithKeyValue((RequestType)args[0], (string)args[1], (string)args[2]));

            default:
                return(null);
            }
            return(null);
        }
Esempio n. 8
0
        /// <summary>
        /// redis cluster中重置连接事件
        /// </summary>
        /// <param name="ipPort"></param>
        /// <param name="operationType"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private Interface.IResult _redisConnection_OnRedirect(string ipPort, OperationType operationType, params object[] args)
        {
            var cnn = RedisConnectionManager.Get(ipPort);

            if (cnn == null || !cnn.IsConnected)
            {
                this.IsConnected = false;
                this.RedisConfig = new RedisConfig(ipPort, this.RedisConfig.Passwords, this.RedisConfig.ActionTimeOut);
                if (_debugModel)
                {
                    cnn = new RedisConnectionDebug(RedisConfig.GetIPPort(), this.RedisConfig.ActionTimeOut);
                }
                else
                {
                    cnn = new RedisConnection(RedisConfig.GetIPPort(), this.RedisConfig.ActionTimeOut);
                }
                cnn.OnRedirect     += _redisConnection_OnRedirect;
                cnn.OnDisconnected += _cnn_OnDisconnected;
                if (cnn.Connect())
                {
                    if (!string.IsNullOrEmpty(this.RedisConfig.Passwords))
                    {
                        cnn.Auth(this.RedisConfig.Passwords);
                    }
                    _cnn             = cnn;
                    this.IsConnected = true;
                    RedisConnectionManager.Set(ipPort, cnn);
                }
                else
                {
                    throw new Exception($"当前节点不可达ipport:{ipPort}");
                }
            }
            else
            {
                _cnn = cnn;
            }

            switch (operationType)
            {
            case OperationType.Do:
                return(_cnn.Do((RequestType)args[0]));

            case OperationType.DoBatchWithList:
                return(_cnn.DoBatchWithList((RequestType)args[0], (string)args[1], (List <string>)args[2]));

            case OperationType.DoBatchWithDic:
                return(_cnn.DoBatchWithDic((RequestType)args[0], (Dictionary <string, string>)args[1]));

            case OperationType.DoBatchWithIDDic:
                return(_cnn.DoBatchWithIDDic((RequestType)args[0], (string)args[1], (Dictionary <string, string>)args[2]));

            case OperationType.DoBatchZaddWithIDDic:
                return(_cnn.DoBatchZaddWithIDDic((RequestType)args[0], (string)args[1], (Dictionary <double, string>)args[2]));

            case OperationType.DoBatchWithIDKeys:
                return(_cnn.DoBatchWithIDKeys((RequestType)args[0], (string)args[1], (string[])args[3]));

            case OperationType.DoBatchWithParams:
                return(_cnn.DoWithMutiParams((RequestType)args[0], (string[])args[1]));

            case OperationType.DoCluster:
                return(_cnn.DoMutiCmd((RequestType)args[0], (object[])args[1]));

            case OperationType.DoClusterSetSlot:
                return(_cnn.DoClusterSetSlot((RequestType)args[0], (string)args[1], (int)args[2], (string)args[3]));

            case OperationType.DoExpire:
                _cnn.DoExpire((string)args[0], (int)args[1]);
                break;

            case OperationType.DoExpireAt:
                _cnn.DoExpireAt((string)args[0], (int)args[1]);
                break;

            case OperationType.DoExpireInsert:
                _cnn.DoExpireInsert((RequestType)args[0], (string)args[1], (string)args[2], (int)args[3]);
                break;

            case OperationType.DoWithID:
                return(_cnn.DoWithID((RequestType)args[0], (string)args[1], (string)args[2], (string)args[3]));

            case OperationType.DoRang:
                return(_cnn.DoRang((RequestType)args[0], (string)args[1], (double)args[2], (double)args[3]));

            case OperationType.DoRangByScore:
                return(_cnn.DoRangByScore((RequestType)args[0], (string)args[1], (double)args[2], (double)args[3], (RangType)args[4], (long)args[5], (int)args[6], (bool)args[7]));

            case OperationType.DoScan:
                return(_cnn.DoScan((RequestType)args[0], (int)args[1], (string)args[2], (int)args[3]));

            case OperationType.DoScanKey:
                return(_cnn.DoScanKey((RequestType)args[0], (string)args[1], (int)args[2], (string)args[3], (int)args[4]));

            case OperationType.DoSub:
                _cnn.DoSub((string[])args[0], (Action <string, string>)args[1]);
                break;

            case OperationType.DoWithKey:
                return(_cnn.DoWithKey((RequestType)args[0], (string)args[1]));

            case OperationType.DoWithKeyValue:
                return(_cnn.DoWithKeyValue((RequestType)args[0], (string)args[1], (string)args[2]));

            default:
                return(null);
            }
            return(null);
        }