/// <summary>
        /// 使用一个静态属性来返回已连接的实例,如下列中所示。这样,一旦 ConnectionMultiplexer 断开连接,便可以初始化新的连接实例。
        /// </summary>
        public void CreateInstance()
        {
            string redisMasterHostsStr = "";

            foreach (var itemHost in zkhelper.pools)
            {
                redisMasterHostsStr += itemHost.Addr + ",";
            }
            var constr = "{0}DefaultDatabase={1}";

            if (Instance != null)
            {
                ConnectionMultiplexer oldInstance = null;
                lock (Instance)
                {
                    oldInstance = Instance;
                    //log.InfoFormat("重新创建Redis实例,RedisHosts:{0}", redisMasterHostsStr.TrimEnd(','));
                    Instance = ConnectionMultiplexer.Connect(string.Format(constr, redisMasterHostsStr, defaultDb));
                }
                System.Threading.Thread.Sleep(1000);
                oldInstance.CloseAsync();
                oldInstance.Dispose();
                //log.InfoFormat("销毁Redis实例完成");
            }
            else
            {
                #region 只为打日志
                //log.InfoFormat("创建Redis实例,RedisHosts:{0}", redisMasterHostsStr.TrimEnd(','));
                #endregion 只为打日志
                Instance = ConnectionMultiplexer.Connect(string.Format(constr, redisMasterHostsStr, defaultDb));
            }
        }
 public void Disconnect()
 {
     _logger.LogInformation("Stopping handler");
     _sub.UnsubscribeAll();
     _redis.CloseAsync();
     OnTweetEventHandled = null;
 }
        public async Task CloseAsync(string correlationId)
        {
            if (_client != null)
            {
                await _client.CloseAsync();

                _client   = null;
                _database = null;
            }
        }
Example #4
0
        private void CloseConnectionMultiplexer(ConnectionMultiplexer connectionMultiplexer, bool allowFinish)
        {
            if (connectionMultiplexer != null)
            {
                return;
            }

            connectionMultiplexer.CloseAsync(allowFinish).ContinueWith(t =>
                                                                       ErrorHandler.Error(string.Format("Unable to successfully close the connection to the Redis host ({0}).", RemoteUrl), t.Exception, ErrorCode.WriteFailure), TaskContinuationOptions.OnlyOnFaulted);
        }
Example #5
0
        public async Task RunAsync(CancellationToken stoppingToken)
        {
            _redis = await ConnectionMultiplexer.ConnectAsync(_connectionString);

            _sub = _redis.GetSubscriber();
            stoppingToken.Register(async() => {
                _logger.LogDebug("Closing redis connection");
                await _redis.CloseAsync();
            });
        }
Example #6
0
        private async Task Close(CancellationToken cancellationToken)
        {
            if (_connection is null)
            {
                return;
            }

            await _connection.CloseAsync();

            _connection.Dispose();
        }
Example #7
0
 public async Task CloseAsync()
 {
     try
     {
         await _redis.CloseAsync();
     }
     catch (Exception exception)
     {
         throw new ArgumentException($"An exception occured, message: { exception }");
     }
 }
Example #8
0
        private async void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // Terminate the sessions and efficiently close the streams.
            if (redis != null)
            {
                var sub = redis.GetSubscriber();
                await sub.UnsubscribeAsync(username.Text.Trim().ToLower());

                await redis.CloseAsync();
            }
        }
Example #9
0
        private async Task ReconnectAsync(CancellationToken token = default)
        {
            token.ThrowIfCancellationRequested();

            await _connectionLock.WaitAsync(token);

            try
            {
                _logger.LogWarning("redis-attempting-to-reconnect");
                if (_connection != null)
                {
                    try
                    {
                        await _connection.CloseAsync();
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError("redis-reconnection-close-old-connection-error",
                                         new Dictionary <string, string> {
                            ["error_message"] = ex.Message
                        });
                    }
                }

                if (_options.ConfigurationOptions != null)
                {
                    _connection = await ConnectionMultiplexer.ConnectAsync(_options.ConfigurationOptions);
                }
                else
                {
                    _connection = await ConnectionMultiplexer.ConnectAsync(_options.Configuration);
                }

                _database = _connection.GetDatabase();
                _logger.LogInformation("redis-reconnection-successful");
            }
            catch (Exception ex)
            {
                _logger.LogError("redis-reconnection-attempt-error",
                                 new Dictionary <string, string> {
                    ["error_message"] = ex.Message
                });
            }
            finally
            {
                _connectionLock.Release();
            }
        }
    public async Task CloseAsync(bool allowCommandsToComplete = true)
    {
        await _connectionLock.WaitAsync();

        try
        {
            if (_connection?.IsConnected == true)
            {
                await _connection.CloseAsync(allowCommandsToComplete).ConfigureAwait(false);

                _connection = null;
            }
        }
        finally
        {
            _connectionLock.Release();
        }
    }
Example #11
0
        private async Task Init(CancellationToken ct)
        {
            if (options.Serializer == SerializerType.BinaryFormatter)
            {
                serializer = new BinarySerializer();
            }

            if (connection != null && !connection.IsConnected)
            {
                await connection.CloseAsync();
            }

            ConfigurationOptions configOptions = GetRedisConfiguration();

            connection = await ConnectionMultiplexer.ConnectAsync(configOptions);

            database = connection.GetDatabase();
            logger.LogInformation("Redis grain state connection open.");
        }
Example #12
0
 /// <summary>
 /// 断开连接
 /// </summary>
 private void Button_Redis_Disconnect(object sender, RoutedEventArgs e)
 {
     redis.CloseAsync();
 }
Example #13
0
 /// <summary>
 /// Closes the connection to redis. (Allows pending operations to be finished)
 /// </summary>
 public async Task DisconnectAsync()
 {
     await _redisCon.CloseAsync(true);
 }