Esempio n. 1
0
        public ConsumerService(ILogger <ConsumerService> logger, IOptions <RedisOption> redisOptions, IConfiguration configuration, IDbTransfer dbTypeMapper)
        {
            _logger        = logger;
            _configuration = configuration;
            _dbTypeMapper  = dbTypeMapper;
            var redisOptions1 = redisOptions?.Value;

            if (redisOptions1 == null)
            {
                redisOptions1 = new RedisOption();
            }

            UpdateFromEnv(redisOptions1);

            if (string.IsNullOrEmpty(redisOptions1.ConnectString))
            {
                throw new ArgumentNullException(nameof(RedisOption.ConnectString));
            }

            if (!redisOptions1.DbTables.Any())
            {
                throw new ArgumentNullException(nameof(RedisOption.DbTables));
            }


            ConfigurationOptions connectOptions = ConfigurationOptions.Parse(redisOptions1.ConnectString);

            if (redisOptions1.ReconnectTimeout < 5000)
            {
                redisOptions1.ReconnectTimeout = 5000;
            }
            connectOptions.ReconnectRetryPolicy = new LinearRetry(redisOptions1.ReconnectTimeout);

            _conn = ConnectionMultiplexer.Connect(connectOptions);
            Redis = _conn.GetDatabase();

            foreach (var dbtable in redisOptions1.DbTables)
            {
                var queueName = !string.IsNullOrEmpty(redisOptions1.CanalDestinationName) ? $"{redisOptions1.CanalDestinationName}." : "";
                queueName += dbtable;
                _topicList.Add(queueName);
            }

            _logger.LogInformation($"Redis [{redisOptions1.ConnectString}] connect success");
        }
Esempio n. 2
0
        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();
            }
        }