public RedisHandler(ILogger <RedisHandler> logger, IOptions <RedisOption> options, IConfiguration configuration) { _logger = logger; _configuration = configuration; _option = options?.Value; if (_option == null) { _option = new RedisOption(); } UpdateFromEnv(_option); if (string.IsNullOrEmpty(_option.ConnectString)) { throw new ArgumentNullException(nameof(RedisOption.ConnectString)); } ConfigurationOptions connectOptions = ConfigurationOptions.Parse(_option.ConnectString); if (_option.ReconnectTimeout < 5000) { _option.ReconnectTimeout = 5000; } connectOptions.ReconnectRetryPolicy = new LinearRetry(_option.ReconnectTimeout); _conn = ConnectionMultiplexer.Connect(connectOptions); Redis = _conn.GetDatabase(); _logger.LogInformation($"Redis [{_option.ConnectString}] connect success"); _logger.LogInformation($"Redis Produce Listening: {string.Join(",", _option.DbTables)}"); AppDomain.CurrentDomain.ProcessExit += (sender, args) => Dispose(); }
private void UpdateFromEnv(RedisOption _rab) { var host = _configuration["redis.connect"]; if (!string.IsNullOrEmpty(host)) { _rab.ConnectString = host; } var retryTimeout = _configuration["redis.reconnectTimeout"]; if (!string.IsNullOrEmpty(retryTimeout)) { _rab.ReconnectTimeout = int.Parse(retryTimeout); } var dbTables = _configuration["redis.dbTables"]; if (!string.IsNullOrEmpty(dbTables)) { _rab.DbTables = dbTables.Split(':').ToList(); } }