public RedisClient(RedisConfig config, bool debugModel = false) { _debugModel = debugModel; RedisConfig = config; if (_debugModel) { _cnn = new RedisConnectionDebug(RedisConfig.GetIPPort(), RedisConfig.ActionTimeOut); } else { _cnn = new RedisConnection(RedisConfig.GetIPPort(), RedisConfig.ActionTimeOut); } _cnn.OnRedirect += _redisConnection_OnRedirect; _cnn.OnDisconnected += _cnn_OnDisconnected; }
/// <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); } }
public RedisClient(RedisConfig config, bool debugModel = false) { _debugModel = debugModel; RedisConfig = config; _cnn = new RedisConnection(RedisConfig.GetIPPort(), RedisConfig.ActionTimeOut, debugModel); _cnn.OnDisconnected += _cnn_OnDisconnected; }
/// <summary> /// 使用密码连接到RedisServer /// </summary> /// <returns></returns> public string Connect() { lock (_syncLocker) { if (!IsConnected) { _cnn.Connect(); IsConnected = _cnn.IsConnected; var infoMsg = Info(); if (infoMsg.Contains("NOAUTH Authentication required.")) { 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.KeepAlived(() => this.KeepAlive()); _clusterCnn.Add(RedisConfig.GetIPPort(), _cnn); return(OK); } }
/// <summary> /// GetRedisGroupConsumer /// </summary> /// <param name="groupName"></param> /// <param name="consumerName"></param> /// <param name="topicName"></param> /// <param name="count"></param> /// <param name="autoCommit"></param> /// <param name="redisId"></param> /// <param name="noAck"></param> /// <param name="blocked"></param> /// <param name="timeout"></param> /// <param name="asc"></param> /// <returns></returns> public RedisGroupConsumer GetRedisGroupConsumer(string groupName, string consumerName, string topicName, int count = 1, bool autoCommit = false, string redisId = "", bool noAck = false, bool blocked = false, int timeout = 1000, bool asc = true) { var cnn = new RedisConnection(RedisConfig.GetIPPort(), RedisConfig.ActionTimeOut); cnn.Connect(); if (!string.IsNullOrEmpty(RedisConfig.Passwords)) { cnn.Auth(RedisConfig.Passwords); } return(new RedisGroupConsumer(cnn, groupName, consumerName, topicName, count, autoCommit, redisId, noAck, blocked, timeout, asc)); }
/// <summary> /// GetRedisConsumer /// </summary> /// <param name="topicIDs"></param> /// <param name="count"></param> /// <param name="blocked"></param> /// <param name="timeout"></param> /// <returns></returns> public RedisConsumer GetRedisConsumer(IEnumerable <TopicID> topicIDs, int count = 1, bool blocked = false, int timeout = 1000) { var cnn = new RedisConnection(RedisConfig.GetIPPort(), RedisConfig.ActionTimeOut); cnn.Connect(); if (!string.IsNullOrEmpty(RedisConfig.Passwords)) { cnn.Auth(RedisConfig.Passwords); } return(new RedisConsumer(cnn, topicIDs, count, blocked, timeout)); }
/// <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); } }
/// <summary> /// 在cluster中重置连接 /// </summary> /// <param name="ipPort"></param> /// <returns></returns> private RedisConnection _redisDataBase_OnRedirect(string ipPort) { lock (_syncLocker) { if (_clusterCnn.ContainsKey(ipPort)) { return(_clusterCnn[ipPort]); } else { this.IsConnected = false; this.RedisConfig = new RedisConfig(ipPort, this.RedisConfig.Passwords); _cnn = new RedisConnection(RedisConfig.GetIPPort(), _debugModel); this.Connect(); return(_cnn); } } }
/// <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); }
public RedisClient(RedisConfig config, bool debugModel = false) { _debugModel = debugModel; RedisConfig = config; _cnn = new RedisConnection(RedisConfig.GetIPPort(), debugModel); }
/// <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); }
/// <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); }