public RedisConnectionManager(RedisConfiguration configuration, ILoggerFactory loggerFactory)
        {
            NotNull(configuration, nameof(configuration));
            NotNull(loggerFactory, nameof(loggerFactory));

            this.connectionString = GetConnectionString(configuration);

            this.configuration = configuration;
            this.logger = loggerFactory.CreateLogger(this);
        }
        public static string GetConnectionString(RedisConfiguration configuration)
        {
            string connectionString = configuration.ConnectionString;

            if (string.IsNullOrWhiteSpace(configuration.ConnectionString))
            {
                var options = CreateConfigurationOptions(configuration);
                connectionString = options.ToString();
            }

            return connectionString;
        }
        /// <summary>
        /// Adds the configuration.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <exception cref="System.ArgumentNullException">If configuration is null.</exception>
        public static void AddConfiguration(RedisConfiguration configuration)
        {
            lock (configLock)
            {
                NotNull(configuration, nameof(configuration));

                if (!Configurations.ContainsKey(configuration.Key))
                {
                    Configurations.Add(configuration.Key, configuration);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Adds the configuration.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <exception cref="System.ArgumentNullException">If configuration is null.</exception>
        public static void AddConfiguration(RedisConfiguration configuration)
        {
            lock (_configLock)
            {
                NotNull(configuration, nameof(configuration));
                NotNullOrWhiteSpace(configuration.Key, nameof(configuration.Key));

                if (!Configurations.ContainsKey(configuration.Key))
                {
                    Configurations.Add(configuration.Key, configuration);
                }
            }
        }
Exemplo n.º 5
0
        public RedisCacheHandle(CacheManagerConfiguration managerConfiguration, CacheHandleConfiguration configuration, ILoggerFactory loggerFactory, ICacheSerializer serializer)
            : base(managerConfiguration, configuration)
        {
            NotNull(loggerFactory, nameof(loggerFactory));
            NotNull(managerConfiguration, nameof(managerConfiguration));
            EnsureNotNull(serializer, "A serializer is required for the redis cache handle");

            this.managerConfiguration = managerConfiguration;
            this.Logger             = loggerFactory.CreateLogger(this);
            this.valueConverter     = new RedisValueConverter(serializer);
            this.redisConfiguration = RedisConfigurations.GetConfiguration(configuration.Key);
            this.connection         = new RedisConnectionManager(this.redisConfiguration, loggerFactory);
        }
        /// <summary>
        /// Adds the configuration.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <exception cref="System.ArgumentNullException">If configuration is null.</exception>
        public static void AddConfiguration(RedisConfiguration configuration)
        {
            lock (configLock)
            {
                if (configuration == null)
                {
                    throw new ArgumentNullException("configuration");
                }

                if (!Configurations.ContainsKey(configuration.Key))
                {
                    Configurations.Add(configuration.Key, configuration);
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Adds the configuration.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <exception cref="System.ArgumentNullException">If configuration is null.</exception>
        public static void AddConfiguration(RedisConfiguration configuration)
        {
            lock (configLock)
            {
                if (configuration == null)
                {
                    throw new ArgumentNullException("configuration");
                }

                if (!Configurations.ContainsKey(configuration.Key))
                {
                    Configurations.Add(configuration.Key, configuration);
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RedisCacheHandle{TCacheValue}"/> class.
        /// </summary>
        /// <param name="managerConfiguration">The manager configuration.</param>
        /// <param name="configuration">The cache handle configuration.</param>
        /// <param name="loggerFactory">The logger factory.</param>
        /// <param name="serializer">The serializer.</param>
        public RedisCacheHandle(ICacheManagerConfiguration managerConfiguration, CacheHandleConfiguration configuration, ILoggerFactory loggerFactory, ICacheSerializer serializer)
            : base(managerConfiguration, configuration)
        {
            NotNull(loggerFactory, nameof(loggerFactory));
            NotNull(managerConfiguration, nameof(managerConfiguration));
            NotNull(configuration, nameof(configuration));
            EnsureNotNull(serializer, "A serializer is required for the redis cache handle");

            Logger = loggerFactory.CreateLogger(this);
            _managerConfiguration = managerConfiguration;
            _valueConverter       = new RedisValueConverter(serializer);
            _redisConfiguration   = RedisConfigurations.GetConfiguration(configuration.Key);
            _connection           = new RedisConnectionManager(_redisConfiguration, loggerFactory);
            _isLuaAllowed         = _connection.Features.Scripting;

            // disable preloading right away if twemproxy mode, as this is not supported.
            _canPreloadScripts = _redisConfiguration.TwemproxyEnabled ? false : true;

            if (_redisConfiguration.KeyspaceNotificationsEnabled)
            {
                // notify-keyspace-events needs to be set to "Exe" at least! Otherwise we will not receive any events.
                // this must be configured per server and should probably not be done automagically as this needs admin rights!
                // Let's try to check at least if those settings are configured (the check also works only if useAdmin is set to true though).
                try
                {
                    var configurations = _connection.GetConfiguration("notify-keyspace-events");
                    foreach (var cfg in configurations)
                    {
                        if (!cfg.Value.Contains("E"))
                        {
                            Logger.LogWarn("Server {0} is missing configuration value 'E' in notify-keyspace-events to enable keyevents.", cfg.Key);
                        }

                        if (!(cfg.Value.Contains("A") ||
                              (cfg.Value.Contains("x") && cfg.Value.Contains("e"))))
                        {
                            Logger.LogWarn("Server {0} is missing configuration value 'A' or 'x' and 'e' in notify-keyspace-events to enable keyevents for expired and evicted keys.", cfg.Key);
                        }
                    }
                }
                catch
                {
                    Logger.LogDebug("Could not read configuration from redis to validate notify-keyspace-events. Most likely useAdmin is not set to true.");
                }

                SubscribeKeyspaceNotifications();
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Gets the configuration.
        /// </summary>
        /// <param name="configurationName">The identifier.</param>
        /// <returns>The <c>RedisConfiguration</c>.</returns>
        /// <exception cref="System.ArgumentNullException">If id is null.</exception>
        /// <exception cref="System.InvalidOperationException">
        /// If no configuration was added for the id.
        /// </exception>
        public static RedisConfiguration GetConfiguration(string configurationName)
        {
            NotNullOrWhiteSpace(configurationName, nameof(configurationName));

            if (!Configurations.ContainsKey(configurationName))
            {
                // check connection strings if there is one matching the name
                var connectionStringHolder = ConfigurationManager.ConnectionStrings[configurationName];
                if (connectionStringHolder == null || string.IsNullOrWhiteSpace(connectionStringHolder.ConnectionString))
                {
                    throw new InvalidOperationException("No configuration added for configuration name " + configurationName);
                }

                var configuration = new RedisConfiguration(configurationName, connectionStringHolder.ConnectionString);
                Configurations.Add(configurationName, configuration);
            }

            return(Configurations[configurationName]);
        }
        private static StackRedis.ConfigurationOptions CreateConfigurationOptions(RedisConfiguration configuration)
        {
            var configurationOptions = new StackRedis.ConfigurationOptions()
            {
                AllowAdmin = configuration.AllowAdmin,
                ConnectTimeout = configuration.ConnectionTimeout,
                Password = configuration.Password,
                Ssl = configuration.IsSsl,
                SslHost = configuration.SslHost,
                ConnectRetry = 10,
                AbortOnConnectFail = false
            };

            foreach (var endpoint in configuration.Endpoints)
            {
                configurationOptions.EndPoints.Add(endpoint.Host, endpoint.Port);
            }

            return configurationOptions;
        }
Exemplo n.º 11
0
        private static StackRedis.ConfigurationOptions CreateConfigurationOptions(RedisConfiguration configuration)
        {
            var configurationOptions = new StackRedis.ConfigurationOptions()
            {
                AllowAdmin         = configuration.AllowAdmin,
                ConnectTimeout     = configuration.ConnectionTimeout,
                Password           = configuration.Password,
                Ssl                = configuration.IsSsl,
                SslHost            = configuration.SslHost,
                ConnectRetry       = 10,
                AbortOnConnectFail = false
            };

            foreach (var endpoint in configuration.Endpoints)
            {
                configurationOptions.EndPoints.Add(endpoint.Host, endpoint.Port);
            }

            return(configurationOptions);
        }
        public static StackRedis.ConnectionMultiplexer Connect(RedisConfiguration configuration)
        {
            string connectionString = configuration.ConnectionString;

            if (string.IsNullOrWhiteSpace(configuration.ConnectionString))
            {
                var options = CreateConfigurationOptions(configuration);
                connectionString = options.ToString();
            }

            StackRedis.ConnectionMultiplexer connection;
            lock (connectLock)
            {
                if (!connections.TryGetValue(connectionString, out connection))
                {
                    var builder = new StringBuilder();
                    using (var log = new StringWriter(builder, CultureInfo.InvariantCulture))                        
                    {
                        connection = StackRedis.ConnectionMultiplexer.Connect(connectionString, log);
                    }

                    connection.ConnectionFailed += (sender, args) =>
                    {
                        connections.Remove(connectionString);
                    };

                    if (!connection.IsConnected)
                    {
                        throw new InvalidOperationException("Connection failed.\n" + builder.ToString());
                    }

                    connection.PreserveAsyncOrder = false;
                    connections.Add(connectionString, connection);                    
                }
            }

            return connection;
        }
Exemplo n.º 13
0
        public static StackRedis.ConnectionMultiplexer Connect(RedisConfiguration configuration)
        {
            string connectionString = configuration.ConnectionString;

            if (string.IsNullOrWhiteSpace(configuration.ConnectionString))
            {
                var options = CreateConfigurationOptions(configuration);
                connectionString = options.ToString();
            }

            StackRedis.ConnectionMultiplexer connection;
            lock (connectLock)
            {
                if (!connections.TryGetValue(connectionString, out connection))
                {
                    var builder = new StringBuilder();
                    using (var log = new StringWriter(builder, CultureInfo.InvariantCulture))
                    {
                        connection = StackRedis.ConnectionMultiplexer.Connect(connectionString, log);
                    }

                    connection.ConnectionFailed += (sender, args) =>
                    {
                        connections.Remove(connectionString);
                    };

                    if (!connection.IsConnected)
                    {
                        throw new InvalidOperationException("Connection failed.\n" + builder.ToString());
                    }

                    connection.PreserveAsyncOrder = false;
                    connections.Add(connectionString, connection);
                }
            }

            return(connection);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Gets the configuration.
        /// </summary>
        /// <param name="configurationName">The identifier.</param>
        /// <returns>The <c>RedisConfiguration</c>.</returns>
        /// <exception cref="System.ArgumentNullException">If id is null.</exception>
        /// <exception cref="System.InvalidOperationException">
        /// If no configuration was added for the id.
        /// </exception>
        public static RedisConfiguration GetConfiguration(string configurationName)
        {
            NotNullOrWhiteSpace(configurationName, nameof(configurationName));

            if (!Configurations.ContainsKey(configurationName))
            {
#if NETSTANDARD
                throw new InvalidOperationException("No configuration added for configuration name " + configurationName);
#else
                // check connection strings if there is one matching the name
                var connectionStringHolder = ConfigurationManager.ConnectionStrings[configurationName];
                if (connectionStringHolder == null || string.IsNullOrWhiteSpace(connectionStringHolder.ConnectionString))
                {
                    throw new InvalidOperationException("No configuration added for configuration name " + configurationName);
                }

                // defaulting to database 0, no way to set it via connection strings atm.
                var configuration = new RedisConfiguration(configurationName, connectionStringHolder.ConnectionString, 0, false);
                AddConfiguration(configuration);
#endif
            }

            return(Configurations[configurationName]);
        }
 public static void DisposeConnection(RedisConfiguration configuration)
 {
     NotNull(configuration, nameof(configuration));
     DisposeConnection(GetConnectionString(configuration));
 }
 public static StackRedis.ConnectionMultiplexer Connect(RedisConfiguration configuration)
 {
     NotNull(configuration, nameof(configuration));
     return Connect(GetConnectionString(configuration));
 }
Exemplo n.º 17
0
        /// <summary>
        /// Gets the configuration.
        /// </summary>
        /// <param name="configurationName">The identifier.</param>
        /// <returns>The <c>RedisConfiguration</c>.</returns>
        /// <exception cref="System.ArgumentNullException">If id is null.</exception>
        /// <exception cref="System.InvalidOperationException">
        /// If no configuration was added for the id.
        /// </exception>
        public static RedisConfiguration GetConfiguration(string configurationName)
        {
            NotNullOrWhiteSpace(configurationName, nameof(configurationName));

            if (!Configurations.ContainsKey(configurationName))
            {
                // check connection strings if there is one matching the name
                var connectionStringHolder = ConfigurationManager.ConnectionStrings[configurationName];
                if (connectionStringHolder == null || string.IsNullOrWhiteSpace(connectionStringHolder.ConnectionString))
                {
                    throw new InvalidOperationException("No configuration added for configuration name " + configurationName);
                }

                var configuration = new RedisConfiguration(configurationName, connectionStringHolder.ConnectionString);
                Configurations.Add(configurationName, configuration);
            }

            return Configurations[configurationName];
        }
Exemplo n.º 18
0
 public static void DisposeConnection(RedisConfiguration configuration)
 {
     NotNull(configuration, nameof(configuration));
     DisposeConnection(GetConnectionString(configuration));
 }
Exemplo n.º 19
0
 public static StackRedis.ConnectionMultiplexer Connect(RedisConfiguration configuration)
 {
     NotNull(configuration, nameof(configuration));
     return(Connect(GetConnectionString(configuration)));
 }