/// <summary>
        /// Initializes a new instance of the <see cref="RedisCacheBackPlate"/> class.
        /// </summary>
        /// <param name="configuration">The cache manager configuration.</param>
        public RedisCacheBackPlate(CacheManagerConfiguration configuration)
            : base(configuration)
        {
            NotNull(configuration, nameof(configuration));

            this.logger = configuration.LoggerFactory.CreateLogger(this);
            this.channelName = configuration.BackPlateChannelName ?? "CacheManagerBackPlate";
            this.identifier = Guid.NewGuid().ToString();

            RetryHelper.Retry(
                () =>
                {
                    // throws an exception if not found for the name
                    var cfg = RedisConfigurations.GetConfiguration(this.ConfigurationKey);

                    var connection = RedisConnectionPool.Connect(cfg);

                    this.redisSubscriper = connection.GetSubscriber();
                },
                configuration.RetryTimeout,
                configuration.MaxRetries,
                this.logger);

            this.Subscribe();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="RedisCacheBackPlate"/> class.
        /// </summary>
        /// <param name="configuration">The cache manager configuration.</param>
        /// <param name="cacheName">The cache name.</param>
        public RedisCacheBackPlate(CacheManagerConfiguration configuration, string cacheName)
            : base(configuration, cacheName)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            this.channelName = string.Format(
                CultureInfo.InvariantCulture,
                "CacheManagerBackPlate_{0}",
                cacheName);

            this.identifier = Guid.NewGuid().ToString();

            RetryHelper.Retry(
                () =>
                {
                    // throws an exception if not found for the name
                    var cfg = RedisConfigurations.GetConfiguration(this.Name);

                    var connection = RedisConnectionPool.Connect(cfg);

                    this.redisSubscriper = connection.GetSubscriber();
                },
                configuration.RetryTimeout,
                configuration.MaxRetries);

            this.Subscribe();
        }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RedisCacheBackPlate"/> class.
        /// </summary>
        /// <param name="configuration">The cache manager configuration.</param>
        /// <param name="cacheName">The cache name.</param>
        public RedisCacheBackPlate(CacheManagerConfiguration configuration, string cacheName)
            : base(configuration, cacheName)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            this.channelName = string.Format(
                CultureInfo.InvariantCulture,
                "CacheManagerBackPlate_{0}",
                cacheName);

            this.identifier = Guid.NewGuid().ToString();

            RetryHelper.Retry(
                () =>
            {
                // throws an exception if not found for the name
                var cfg = RedisConfigurations.GetConfiguration(this.Name);

                var connection = RedisConnectionPool.Connect(cfg);

                this.redisSubscriper = connection.GetSubscriber();
            },
                configuration.RetryTimeout,
                configuration.MaxRetries);

            this.Subscribe();
        }
예제 #4
0
 public TreeUpdator(Tree.Tree tree, StackExchange.Redis.ISubscriber subscriber)
 {
     _thisSender = Guid.NewGuid().ToString();
     _tree       = tree;
     _subscriber = subscriber;
     _channelMessageSerializer = new JsonSerializer <TreeUpdateChannelMessageData>();
     subscriber.SubscribeAsync(DatabaseSchema.TreeUpdateChannel).ContinueWith(t => t.Result.OnMessage(ProcessItemUpdate), TaskContinuationOptions.LongRunning);
 }
예제 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RedisCacheBackPlate"/> class.
        /// </summary>
        /// <param name="configuration">The cache manager configuration.</param>
        /// <param name="loggerFactory">The logger factory</param>
        public RedisCacheBackPlate(CacheManagerConfiguration configuration, ILoggerFactory loggerFactory)
            : base(configuration)
        {
            NotNull(configuration, nameof(configuration));
            NotNull(loggerFactory, nameof(loggerFactory));

            this.logger      = loggerFactory.CreateLogger(this);
            this.channelName = configuration.BackPlateChannelName ?? "CacheManagerBackPlate";
            this.identifier  = Guid.NewGuid().ToString();

            RetryHelper.Retry(
                () =>
            {
                // throws an exception if not found for the name
                var cfg = RedisConfigurations.GetConfiguration(this.ConfigurationKey);

                var connection = RedisConnectionPool.Connect(cfg);

                this.redisSubscriper = connection.GetSubscriber();
            },
                configuration.RetryTimeout,
                configuration.MaxRetries,
                this.logger);

            this.Subscribe();

            this.timer = new Timer(
                (obj) =>
            {
                lock (this.messageLock)
                {
                    try
                    {
                        if (this.messages != null && this.messages.Count > 0)
                        {
                            var msgs = string.Join(",", this.messages);
                            if (this.logger.IsEnabled(LogLevel.Debug))
                            {
                                this.logger.LogDebug("Back-plate is sending {0} messages ({1} skipped).", this.messages.Count, this.skippedMessages);
                            }

                            this.Publish(msgs);
                            this.skippedMessages = 0;
                            this.messages.Clear();
                        }
                    }
                    catch (Exception ex)
                    {
                        this.logger.LogError(ex, "Error occurred sending back plate messages.");
                        throw;
                    }
                }
            },
                this,
                TimeSpan.FromMilliseconds(100),
                TimeSpan.FromMilliseconds(100));
        }
예제 #6
0
        public RedisBus(IConnectionMultiplexer connectionMultiplexer, ISerializationService serializationService)
        {
            this._serializationService = serializationService;


            _connectionMultiplexer = connectionMultiplexer;


            this._subscriber = this._connectionMultiplexer.GetSubscriber();
        }
예제 #7
0
				public async Task ConnectAsync(string connectionString, TraceSource trace)
        {
            _connection = await ConnectToRedis(connectionString);
            _connection.ConnectionFailed += OnConnectionFailed;
            _connection.ConnectionRestored += OnConnectionRestored;
            _connection.ErrorMessage += OnError;

            _trace = trace;

            _redisSubscriber = _connection.GetSubscriber();
        }
예제 #8
0
        public async Task ConnectAsync(string connectionString, TraceSource trace)
        {
            _connection = await ConnectionMultiplexer.ConnectAsync(connectionString);

            _connection.ConnectionFailed   += OnConnectionFailed;
            _connection.ConnectionRestored += OnConnectionRestored;
            _connection.ErrorMessage       += OnError;

            _trace = trace;

            _redisSubscriber = _connection.GetSubscriber();
        }
예제 #9
0
        public async Task ConnectAsync(string connectionString, ILogger logger)
        {
            _connection = await ConnectionMultiplexer.ConnectAsync(connectionString);

            _connection.ConnectionFailed += OnConnectionFailed;
            _connection.ConnectionRestored += OnConnectionRestored;
            _connection.ErrorMessage += OnError;

            _logger = logger;

            _redisSubscriber = _connection.GetSubscriber();
        }
예제 #10
0
        public async Task ConnectAsync(string connectionString, ILogger logger)
        {
            _connection = await ConnectionMultiplexer.ConnectAsync(connectionString);

            _connection.ConnectionFailed   += OnConnectionFailed;
            _connection.ConnectionRestored += OnConnectionRestored;
            _connection.ErrorMessage       += OnError;

            _logger = logger;

            _redisSubscriber = _connection.GetSubscriber();
        }
예제 #11
0
        public async Task ConnectAsync(string connectionString, TraceSource trace)
        {
            if (!_isSharedConnection)
                _connection = await ConnectionMultiplexer.ConnectAsync(connectionString);

            _connection.ConnectionFailed += OnConnectionFailed;
            _connection.ConnectionRestored += OnConnectionRestored;
            _connection.ErrorMessage += OnError;

            _trace = trace;

            _redisSubscriber = _connection.GetSubscriber();
        }
예제 #12
0
        public async Task ConnectAsync(string connectionString, TraceSource trace)
        {
            _connection = await ConnectionMultiplexer.ConnectAsync(connectionString, new TraceTextWriter("ConnectionMultiplexer: ", trace));

            if (!_connection.IsConnected)
            {
                _connection.Dispose();
                _connection = null;
                throw new InvalidOperationException("Failed to connect to Redis");
            }

            _connection.ConnectionFailed   += OnConnectionFailed;
            _connection.ConnectionRestored += OnConnectionRestored;
            _connection.ErrorMessage       += OnError;

            _trace = trace;

            _redisSubscriber = _connection.GetSubscriber();
        }
예제 #13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RedisCacheBackPlate"/> class.
        /// </summary>
        /// <param name="configuration">The cache manager configuration.</param>
        public RedisCacheBackPlate(CacheManagerConfiguration configuration)
            : base(configuration)
        {
            NotNull(configuration, nameof(configuration));

            this.channelName = configuration.BackPlateChannelName ?? "CacheManagerBackPlate";
            this.identifier  = Guid.NewGuid().ToString();

            RetryHelper.Retry(
                () =>
            {
                // throws an exception if not found for the name
                var cfg = RedisConfigurations.GetConfiguration(this.ConfigurationKey);

                var connection = RedisConnectionPool.Connect(cfg);

                this.redisSubscriper = connection.GetSubscriber();
            },
                configuration.RetryTimeout,
                configuration.MaxRetries);

            this.Subscribe();
        }
예제 #14
0
				private void MasterWasSwitched(RedisChannel redisChannel, RedisValue redisValue)
        {
	        _connection.ConnectionFailed -= OnConnectionFailed;
	        _connection.ConnectionRestored -= OnConnectionRestored;
	        _connection.ErrorMessage -= OnError;
          _connection.Close();
          if (redisValue.IsNullOrEmpty) return;
          var message = redisValue.ToString();
          var messageParts = message.Split(' ');
          var ip = IPAddress.Parse(messageParts[3]);
          var port = int.Parse(messageParts[4]);
          EndPoint newMasterEndpoint = new IPEndPoint(ip, port);
          if (_options.EndPoints.Any() && newMasterEndpoint == _options.EndPoints[0]) return;
          _options.EndPoints.Clear();
          _options.EndPoints.Add(newMasterEndpoint);

          _connection = ConnectionMultiplexer.Connect(_options);
          _connection.ConnectionFailed += OnConnectionFailed;
          _connection.ConnectionRestored += OnConnectionRestored;
          _connection.ErrorMessage += OnError;
          _redisSubscriber = _connection.GetSubscriber();
          var handler = ConnectionRestored;
          if (handler != null) handler(new ApplicationException("Redis master was switched"));
        }