Пример #1
0
        protected ConnectionMultiplexer GetConnection()
        {
            if (_connection != null && _connection.IsConnected)
            {
                return(_connection);
            }

            lock (_lock)
            {
                if (_connection != null && _connection.IsConnected)
                {
                    return(_connection);
                }

                if (_connection != null)
                {
                    _connection.Dispose();
                }

                //创建Redis Connection的新实例
                _connection = ConnectionMultiplexer.Connect(_connectionString.Value);
            }

            return(_connection);
        }
Пример #2
0
 public void Dispose()
 {
     if (redisConnection != null)
     {
         redisConnection.Dispose();
     }
 }
Пример #3
0
        private ConnectionMultiplexer GetConnection()
        {
            if (connection != null && connection.IsConnected)
            {
                return(connection);
            }

            lock (_lock)
            {
                if (connection != null && connection.IsConnected)
                {
                    return(connection);
                }

                if (connection != null)
                {
                    logger.Info("Connection disconnected. Disposing connection...");
                    connection.Dispose();
                }

                logger.Info("Creating new instance of Redis Connection");
                connection = ConnectionMultiplexer.Connect(connectionString);
            }

            return(connection);
        }
Пример #4
0
        public ConnectionMultiplexer Create()
        {
            if (_instance != null && _instance.IsConnected)
            {
                return(_instance);
            }

            lock (_syncLock)
            {
                if (_instance != null && _instance.IsConnected)
                {
                    return(_instance);
                }

                if (_instance != null)
                {
                    _instance.Dispose();
                }

                var connectionStringSetting = ConfigurationManager.ConnectionStrings[_settings.ConnectionStringOrName];

                _instance = ConnectionMultiplexer.Connect(connectionStringSetting == null ? _settings.ConnectionStringOrName : connectionStringSetting.ConnectionString);
            }

            return(_instance);
        }
Пример #5
0
        private ConnectionMultiplexer GetConnection()
        {
            if (connection != null && connection.IsConnected)
            {
                return(connection);
            }

            lock (_lock)
            {
                if (connection != null && connection.IsConnected)
                {
                    return(connection);
                }

                if (connection != null)
                {
                    connection.Dispose();
                }
                if (string.IsNullOrWhiteSpace(connectionString))
                {
                    connection = ConnectionMultiplexer.Connect(options);
                }
                else
                {
                    connection = ConnectionMultiplexer.Connect(connectionString);
                }
            }

            return(connection);
        }
Пример #6
0
        /// <summary>
        /// 获取与Redis服务器的连接
        /// </summary>
        /// <returns></returns>
        protected ConnectionMultiplexer GetConnection()
        {
            if (_connection != null && _connection.IsConnected)
            {
                return(_connection);
            }

            lock (_lock)
            {
                if (_connection != null && _connection.IsConnected)
                {
                    return(_connection);
                }

                if (_connection != null)
                {
                    //连接断开。正在释放连接…
                    _connection.Dispose();
                }

                //新建redis连接实例
                _connection = ConnectionMultiplexer.Connect(_connectionString.Value);
            }

            return(_connection);
        }
Пример #7
0
        private ConnectionMultiplexer GetConnection()
        {
            if (_connection != null && _connection.IsConnected)
            {
                return(_connection);
            }

            lock (_lock)
            {
                if (_connection != null && _connection.IsConnected)
                {
                    return(_connection);
                }

                if (_connection != null)
                {
                    _logger.Debug("Connection disconnected. Disposing connection...");
                    _connection.Dispose();
                }

                _logger.Debug("Creating new instance of Redis Connection");
                _connection = ConnectionMultiplexer.Connect(_connectionString.Value);
            }

            return(_connection);
        }
        /// <summary>
        /// Get connection to Redis servers
        /// </summary>
        /// <returns></returns>
        protected ConnectionMultiplexer GetConnection()
        {
            if (_connection != null && _connection.IsConnected)
            {
                return(_connection);
            }

            lock (_lock)
            {
                if (_connection != null && _connection.IsConnected)
                {
                    return(_connection);
                }

                if (_connection != null)
                {
                    //Connection disconnected. Disposing connection...
                    _connection.Dispose();
                }

                //Creating new instance of Redis Connection
                _connection = ConnectionMultiplexer.Connect(_connectionString.Value);
            }

            return(_connection);
        }
Пример #9
0
 public void Dispose()
 {
     _connection?.Close();
     _connection?.Dispose();
     _redisLockFactory?.Dispose();
     _connection       = null;
     _redisLockFactory = null;
 }
Пример #10
0
 protected virtual void Dispose(bool disposing)
 {
     if (!this._disposed)
     {
         if (disposing)
         {
             _conn.Dispose();
         }
     }
     this._disposed = true;
 }
Пример #11
0
        /// <summary>
        ///     Connects this instance.
        /// </summary>
        private void Connect( )
        {
            if (_multiplexer != null)
            {
                _multiplexer.Dispose( );
                _multiplexer = null;
            }

            string config = $"{PluginSettings.RedisSettings.ServerName}:{PluginSettings.RedisSettings.Port}";

            Task <ConnectionMultiplexer> task = ConnectionMultiplexer.ConnectAsync(config);

            task.ContinueWith(ConnectComplete);
        }
Пример #12
0
        private ConnectionMultiplexer GetRedisConnection(ConfigurationOptions options)
        {
            if (this._redisConnection != null && this._redisConnection.IsConnected)
            {
                return(this._redisConnection);
            }

            lock (_redisConnectLockObject)
            {
                //释放连接
                _redisConnection?.Dispose();
                return(ConnectionMultiplexer.Connect(options));
            }
        }
Пример #13
0
        public void Close(string key, bool allowCommandsToComplete = true)
        {
            if (_redisSubscriber != null)
            {
                _redisSubscriber.Unsubscribe(key);
            }

            if (_connection != null)
            {
                _connection.Close(allowCommandsToComplete);
            }

            _connection.Dispose();
        }
Пример #14
0
        public void Close(string key, bool allowCommandsToComplete = true)
        {
            _trace.TraceInformation("Closing key: " + key);
            if (_redisSubscriber != null)
            {
                _redisSubscriber.Unsubscribe(key);
            }

            if (_connection != null)
            {
                _connection.Close(allowCommandsToComplete);
            }

            _connection.Dispose();
        }
Пример #15
0
        /// @brief save socket auth key in redis db0
        public static bool SetRedisKey(string key, string value, int?expTimeMin)     // todo: value as oject or ...?
        {
            ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(redisConnectionStringSocket);

            // try to connect database
            try
            {
                // StringSet task
                IDatabase cache = connection.GetDatabase(0);
                if (expTimeMin == null)
                {
                    // save without expire time
                    cache.StringSet(key, value);
                }
                else
                {
                    cache.StringSet(key, value, TimeSpan.FromMinutes(Convert.ToDouble(expTimeMin)));
                }

                connection.Close();
                connection.Dispose();

                return(true);
            }
            catch (Exception)
            {
                throw;
            }
        }
Пример #16
0
        /// save log to redis db2 and keep 365 days
        public static void saveRedisLog(string key, string message, int?expTimeDays)
        {
            ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(CloudBreadGameLogRedisServer);

            // try to connect database
            try
            {
                // StringSet task
                IDatabase cache = connection.GetDatabase(2);
                if (expTimeDays == null)
                {
                    // save without expire time
                    cache.StringSetAsync(key, message);
                }
                else
                {
                    cache.StringSetAsync(key, message, TimeSpan.FromDays(Convert.ToDouble(expTimeDays)));
                }

                connection.Close();
                connection.Dispose();
            }
            catch (Exception)
            {
                throw;
            }
        }
Пример #17
0
 public void Dispose()
 {
     if (_connection != null && _dispose)
     {
         _connection.Dispose();
     }
 }
Пример #18
0
 public override void Dispose()
 {
     if (database != null)
     {
         connectionMultiplexer.Dispose();
     }
 }
Пример #19
0
 /// <summary>
 /// Dispose
 /// </summary>
 public virtual void Dispose()
 {
     if (_muxer != null)
     {
         _muxer.Dispose();
     }
 }
Пример #20
0
        private ConnectionMultiplexer GetConnection()
        {
            if (s_connection != null && s_connection.IsConnected)
            {
                return(s_connection);
            }

            lock (s_objlock)
            {
                if (s_connection != null && s_connection.IsConnected)
                {
                    return(s_connection);
                }

                if (s_connection != null)
                {
                    s_connection.Dispose();
                }
                try
                {
                    s_connection = ConnectionMultiplexer.Connect(s_connectionString);
                }
                catch
                {
                    s_connection = null;
                }
            }

            return(s_connection);
        }
 public void Dispose()
 {
     m_RedisServerProcess?.Dispose();
     m_RedisServerProcess = null;
     _m_Controller?.Dispose();
     _m_Controller = null;
 }
Пример #22
0
 /// <summary>
 /// 获取Redis连接实例
 /// </summary>
 /// <returns>Redis连接实例</returns>
 private ConnectionMultiplexer GetRedisConnect()
 {
     if (redisConnection != null && redisConnection.IsConnected)
     {
         return(redisConnection);
     }
     lock (redisConnectionLock)
     {
         if (redisConnection != null)
         {
             redisConnection.Dispose();
         }
         try
         {
             var config = new ConfigurationOptions
             {
                 AbortOnConnectFail = false,
                 AllowAdmin         = true,
                 ConnectTimeout     = 15000, // 15s
                 SyncTimeout        = 5000,
                 EndPoints          = { redisConnectionString }
             };
             redisConnection = ConnectionMultiplexer.Connect(config);
         }
         catch (Exception ex)
         {
             throw new Exception("Redis服务未启用,请开启该服务,并且请注意端口号。", ex);
         }
     }
     return(redisConnection);
 }
Пример #23
0
        private void _KillRedisClient()
        {
            try
            {
                if (m_Redis != null)
                {
                    Stopwatch sw = Stopwatch.StartNew();
                    LoggerQueue.Debug(">>>>>> KILLING REDIS CONNECTION >>>>>>");

                    ConnectionMultiplexer redis = m_Redis;
                    m_Redis = null;

                    if (this.IsSentinel == true)
                    {
                        redis.ConnectionFailed   -= _ConnectionFailure;
                        redis.ConnectionRestored -= _ConnectionRestored;
                    }

                    redis.Close(false);
                    redis.Dispose();

                    LoggerQueue.Debug(">>>>>> KILLED REDIS CONNECTION >>>>>> " + sw.Elapsed);
                }
            }
            catch (Exception ex)
            {
                LoggerQueue.Error(">>>>>> Error during _KillRedisClient", ex);
            }
        }
Пример #24
0
        /// <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));
            }
        }
Пример #25
0
        /// <summary>
        /// 释放掉坏掉的连接
        /// </summary>
        /// <param name="srvCfg">srvCfg</param>
        /// <param name="conn">conn</param>
        private static void Dispose(RedisServer srvCfg, ConnectionMultiplexer conn)
        {
            lock (_lockObj)
            {
                try
                {
                    if (conn != null)
                    {
                        conn.Dispose();
                        conn = null;
                    }
                }
                catch (Exception ex)
                {
                    Logger?.Log(LogLevel.Error, new Exception("RedisConnectionManager.Dispose.Error", ex), "", null);
                }

                string key = string.Format("{0}_{1}", srvCfg.Server, srvCfg.Port);

                if (ConnectionCache.ContainsKey(key))
                {
                    ConnectionCache.Remove(key);
                }
            }
        }
Пример #26
0
 public void Dispose()
 {
     Database.ExecuteAsync("FLUSHALL").Wait();
     Client.Dispose();
     Cluster.StopAllSilos();
     _redis?.Dispose();
 }
Пример #27
0
 public void Dispose()
 {
     if (redis != null)
     {
         redis.Dispose();
     }
 }
Пример #28
0
        /// <summary>
        /// 单例获取
        /// </summary>
        public static ConnectionMultiplexer GetConnection(RedisServer srvCfg)
        {
            // var date = DateTime.Now;
            lock (_lockObj)
            {
                // 业务模型节点+索引确定一个连接
                string key = string.Format("{0}_{1}_{2}", srvCfg.Node.NodeName, srvCfg.Server, srvCfg.Port);
                ConnectionMultiplexer conn = null;
                if (ConnectionCache.ContainsKey(key))
                {
                    conn = ConnectionCache[key];
                }

                if (conn == null || !conn.IsConnected)
                {
                    if (conn != null)
                    {
                        conn.Dispose(); // 如果一个连接断了,必须释放掉,才能重新建
                    }

                    // Console.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffffff") + "---Wait---" + DateTime.Now.Subtract(date).TotalMilliseconds);
                    conn = getConnection(srvCfg);
                    ConnectionCache[key] = conn;
                }

                return(conn);
            }
        }
Пример #29
0
 public void Dispose()
 {
     if (allowRelease)
     {
         _connection?.Dispose();
     }
 }
Пример #30
0
        /// <summary>
        /// 释放掉坏掉的连接
        /// </summary>
        /// <param name="srvCfg">srvCfg</param>
        /// <param name="conn">conn</param>
        public static void Dispose(RedisServer srvCfg, ConnectionMultiplexer conn)
        {
            lock (_lockObj)
            {
                try
                {
                    if (conn != null)
                    {
                        conn.Dispose();
                        conn = null;
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "RedisConnectionManager.Dispose.Error");
                }

                string key = string.Format("{0}_{1}_{2}", srvCfg.Node.NodeName, srvCfg.Server, srvCfg.Port);

                if (ConnectionCache.ContainsKey(key))
                {
                    ConnectionCache.Remove(key);
                }
            }
        }