Beispiel #1
0
 public static void Close(RedisInstanceSetting setting)
 {
     if (_connectionDict.TryGetValue(setting.InstanceName, out ConnectionMultiplexer? connection))
     {
         connection.Dispose();
         _connectionDict.TryRemove(setting.InstanceName, out ConnectionMultiplexer _);
     }
 }
Beispiel #2
0
        private static ConnectionMultiplexer GetConnectionMultiplexer(RedisInstanceSetting setting, ILogger logger)
        {
            if (_connectionDict.TryGetValue(setting.InstanceName, out ConnectionMultiplexer? cached))
            {
                return(cached);
            }

            try
            {
                _connectionLock.Wait();

                //Double check
                if (_connectionDict.TryGetValue(setting.InstanceName, out ConnectionMultiplexer? cached2))
                {
                    return(cached2);
                }

                ConfigurationOptions configurationOptions = ConfigurationOptions.Parse(setting.ConnectionString);

                //TODO: 调查参数
                //configurationOptions.KeepAlive = 30;
                //configurationOptions.ConnectTimeout = 10 * 1000;
                //configurationOptions.SyncTimeout = 100 * 1000;
                //configurationOptions.ReconnectRetryPolicy = new ExponentialRetry(5000);

                ConnectionMultiplexer connection = null !;

                ReConnectPolicy(setting.ConnectionString, logger).Execute(() =>
                {
                    connection = ConnectionMultiplexer.Connect(configurationOptions);

                    _connectionDict[setting.InstanceName] = connection;

                    SetConnectionEvents(connection, logger);

                    logger.LogInformation($"Redis 链接建立 Connected : {setting.InstanceName}");
                });

                _connectionLock.Release();

                return(connection);
            }
            catch (Exception ex)
            {
                _connectionLock.Release();

                logger.LogCritical(ex, $"Redis Database获取失败.尝试重新获取。 SettingName:{setting.InstanceName}, Connection:{setting.ConnectionString}");

                Close(setting);

                Thread.Sleep(5000);

                return(GetConnectionMultiplexer(setting, logger));
            }
        }
Beispiel #3
0
        public static async Task <IServer> GetServerAsync(RedisInstanceSetting setting, ILogger logger)
        {
            ConnectionMultiplexer connection = await GetConnectionMultiplexerAsync(setting, logger).ConfigureAwait(false);

            return(connection.GetServer(setting.ServerEndPoint));
        }
Beispiel #4
0
        public static IServer GetServer(RedisInstanceSetting setting, ILogger logger)
        {
            ConnectionMultiplexer connection = GetConnectionMultiplexer(setting, logger);

            return(connection.GetServer(setting.ServerEndPoint));
        }
Beispiel #5
0
        public static async Task <IDatabase> GetDatabaseAsync(RedisInstanceSetting setting, ILogger logger)
        {
            ConnectionMultiplexer connection = await GetConnectionMultiplexerAsync(setting, logger).ConfigureAwait(false);

            return(connection.GetDatabase(setting.DatabaseNumber));
        }
Beispiel #6
0
 public static IDatabase GetDatabase(RedisInstanceSetting setting, ILogger logger)
 {
     //StackExchange.Redis会在GetDatabase缓存,所以这里我们不用缓存IDatabase
     return(GetConnectionMultiplexer(setting, logger).GetDatabase(setting.DatabaseNumber));
 }