Пример #1
0
        internal void UpdateOptions(RedisCacheConnectorOptions config, RedisCacheOptions options)
        {
            if (config == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(config.ConnectionString))
            {
                options.Configuration = config.ConnectionString;
                options.InstanceName  = config.InstanceId;
            }
            else
            {
                string msConfiguration = config.Host + ":" + config.Port;
                if (!string.IsNullOrEmpty(config.Password))
                {
                    msConfiguration = msConfiguration + ",password=" + config.Password;
                }

                options.Configuration = msConfiguration;
                options.InstanceName  = config.InstanceId;
            }

            return;
        }
        internal void UpdateOptions(RedisServiceInfo si, RedisCacheConnectorOptions configuration)
        {
            if (si == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(si.Host))
            {
                configuration.Host      = si.Host;
                configuration.Port      = si.Port;
                configuration.EndPoints = null;
            }

            if (!string.IsNullOrEmpty(si.Password))
            {
                if (configuration.UrlEncodedCredentials)
                {
                    configuration.Password = WebUtility.UrlDecode(si.Password);
                }
                else
                {
                    configuration.Password = si.Password;
                }
            }
        }
Пример #3
0
        internal ConfigurationOptions ConfigureConnection(RedisServiceInfo si, RedisCacheConnectorOptions options)
        {
            UpdateOptions(si, options);
            ConfigurationOptions redisOptions = ConfigurationOptions.Parse(options.ToString());

            return(redisOptions);
        }
Пример #4
0
        internal IOptions <RedisCacheOptions> Configure(RedisServiceInfo si, RedisCacheConnectorOptions configuration)
        {
            RedisCacheOptions redisOptions = new RedisCacheOptions();

            UpdateOptions(configuration, redisOptions);
            UpdateOptions(si, redisOptions);
            return(new ConnectorIOptions <RedisCacheOptions>(redisOptions));
        }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RedisServiceConnectorFactory"/> class.
 /// Factory for creating Redis connections with either Microsoft.Extensions.Caching.Redis or StackExchange.Redis
 /// </summary>
 /// <param name="sinfo">Service Info</param>
 /// <param name="config">Service Configuration</param>
 /// <param name="connectionType">Redis connection Type</param>
 /// <param name="optionsType">Options Type used to establish connection</param>
 /// <param name="initalizer">Method used to open connection</param>
 public RedisServiceConnectorFactory(RedisServiceInfo sinfo, RedisCacheConnectorOptions config, Type connectionType, Type optionsType, MethodInfo initalizer)
 {
     _info         = sinfo;
     _config       = config ?? throw new ArgumentNullException(nameof(config), "Cache connector options must be provided");
     ConnectorType = connectionType;
     OptionsType   = optionsType;
     Initializer   = initalizer;
 }
Пример #6
0
        internal IOptions <RedisCacheOptions> Configure(RedisServiceInfo si, RedisCacheConnectorOptions options)
        {
            UpdateOptions(si, options);

            RedisCacheOptions redisOptions = new RedisCacheOptions();

            UpdateOptions(options, redisOptions);

            return(new ConnectorIOptions <RedisCacheOptions>(redisOptions));
        }
Пример #7
0
        public static RedisServiceConnectorFactory CreateRedisServiceConnectorFactory(this IConfiguration config, string serviceName = null)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var redisConfig = new RedisCacheConnectorOptions(config);

            return(config.CreateRedisServiceConnectorFactory(config, serviceName));
        }
        private static void DoAddIDistributedCache(IServiceCollection services, RedisServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime)
        {
            Type interfaceType  = RedisTypeLocator.MicrosoftInterface;
            Type connectionType = RedisTypeLocator.MicrosoftImplementation;
            Type optionsType    = RedisTypeLocator.MicrosoftOptions;

            RedisCacheConnectorOptions   redisConfig = new RedisCacheConnectorOptions(config);
            RedisServiceConnectorFactory factory     = new RedisServiceConnectorFactory(info, redisConfig, connectionType, optionsType, null);

            services.Add(new ServiceDescriptor(interfaceType, factory.Create, contextLifetime));
            services.Add(new ServiceDescriptor(connectionType, factory.Create, contextLifetime));
            services.Add(new ServiceDescriptor(typeof(IHealthContributor), ctx => new RedisHealthContributor(factory, connectionType, ctx.GetService <ILogger <RedisHealthContributor> >()), ServiceLifetime.Singleton));
        }
        private static void DoAddConnectionMultiplexer(IServiceCollection services, RedisServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime)
        {
            Type       redisInterface      = RedisTypeLocator.StackExchangeInterface;
            Type       redisImplementation = RedisTypeLocator.StackExchangeImplementation;
            Type       redisOptions        = RedisTypeLocator.StackExchangeOptions;
            MethodInfo initializer         = RedisTypeLocator.StackExchangeInitializer;

            RedisCacheConnectorOptions   redisConfig = new RedisCacheConnectorOptions(config);
            RedisServiceConnectorFactory factory     = new RedisServiceConnectorFactory(info, redisConfig, redisImplementation, redisOptions, initializer ?? null);

            services.Add(new ServiceDescriptor(redisInterface, factory.Create, contextLifetime));
            services.Add(new ServiceDescriptor(redisImplementation, factory.Create, contextLifetime));
            services.Add(new ServiceDescriptor(typeof(IHealthContributor), ctx => new RedisHealthContributor(factory, redisImplementation, ctx.GetService <ILogger <RedisHealthContributor> >()), ServiceLifetime.Singleton));
        }
Пример #10
0
        public Connection Get(IConfiguration configuration, string serviceName)
        {
            var info = serviceName == null
                ? configuration.GetSingletonServiceInfo <RedisServiceInfo>()
                : configuration.GetRequiredServiceInfo <RedisServiceInfo>(serviceName);

            var redisConfig = new RedisCacheConnectorOptions(configuration);
            var configurer  = new RedisCacheConfigurer();
            var connString  = configurer.Configure(info, redisConfig).ToString();

            return(new Connection
            {
                ConnectionString = connString,
                Name = "Redis" + serviceName?.Insert(0, "-")
            });
        }
        private static void DoAddIDistributedCache(IServiceCollection services, RedisServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime, bool addSteeltoeHealthChecks = false)
        {
            var interfaceType  = RedisTypeLocator.MicrosoftInterface;
            var connectionType = RedisTypeLocator.MicrosoftImplementation;
            var optionsType    = RedisTypeLocator.MicrosoftOptions;

            var redisConfig = new RedisCacheConnectorOptions(config);
            var factory     = new RedisServiceConnectorFactory(info, redisConfig, connectionType, optionsType, null);

            services.Add(new ServiceDescriptor(interfaceType, factory.Create, contextLifetime));
            services.Add(new ServiceDescriptor(connectionType, factory.Create, contextLifetime));
            if (!services.Any(s => s.ServiceType == typeof(HealthCheckService)) || addSteeltoeHealthChecks)
            {
                services.Add(new ServiceDescriptor(typeof(IHealthContributor), ctx => new RedisHealthContributor(factory, connectionType, ctx.GetService <ILogger <RedisHealthContributor> >()), ServiceLifetime.Singleton));
            }
        }
Пример #12
0
        public static IHealthContributor GetRedisContributor(IConfiguration configuration, ILogger <RedisHealthContributor> logger = null)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var redisImplementation = RedisTypeLocator.StackExchangeImplementation;
            var redisOptions        = RedisTypeLocator.StackExchangeOptions;
            var initializer         = RedisTypeLocator.StackExchangeInitializer;

            var info        = configuration.GetSingletonServiceInfo <RedisServiceInfo>();
            var redisConfig = new RedisCacheConnectorOptions(configuration);
            var factory     = new RedisServiceConnectorFactory(info, redisConfig, redisImplementation, redisOptions, initializer);

            return(new RedisHealthContributor(factory, redisImplementation, logger));
        }
        private static void DoAddIDistributedCache(IServiceCollection services, RedisServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime)
        {
            Type interfaceType  = RedisTypeLocator.MicrosoftRedisInterface;
            Type connectionType = RedisTypeLocator.MicrosoftRedisImplementation;
            Type optionsType    = RedisTypeLocator.MicrosoftRedisOptions;

            if (interfaceType == null || connectionType == null || optionsType == null)
            {
                throw new ConnectorException("Unable to find required Redis types, are you missing the Microsoft.Extensions.Caching.Redis Nuget package?");
            }

            RedisCacheConnectorOptions   redisConfig = new RedisCacheConnectorOptions(config);
            RedisServiceConnectorFactory factory     = new RedisServiceConnectorFactory(info, redisConfig, connectionType, optionsType, null);

            services.Add(new ServiceDescriptor(interfaceType, factory.Create, contextLifetime));
            services.Add(new ServiceDescriptor(connectionType, factory.Create, contextLifetime));
        }
        private static void DoAddConnectionMultiplexer(IServiceCollection services, RedisServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime, bool addSteeltoeHealthChecks)
        {
            var redisInterface      = RedisTypeLocator.StackExchangeInterface;
            var redisImplementation = RedisTypeLocator.StackExchangeImplementation;
            var redisOptions        = RedisTypeLocator.StackExchangeOptions;
            var initializer         = RedisTypeLocator.StackExchangeInitializer;

            var redisConfig = new RedisCacheConnectorOptions(config);
            var factory     = new RedisServiceConnectorFactory(info, redisConfig, redisImplementation, redisOptions, initializer ?? null);

            services.Add(new ServiceDescriptor(redisInterface, factory.Create, contextLifetime));
            services.Add(new ServiceDescriptor(redisImplementation, factory.Create, contextLifetime));
            if (!services.Any(s => s.ServiceType == typeof(HealthCheckService)) || addSteeltoeHealthChecks)
            {
                services.Add(new ServiceDescriptor(typeof(IHealthContributor), ctx => new RedisHealthContributor(factory, redisImplementation, ctx.GetService <ILogger <RedisHealthContributor> >()), ServiceLifetime.Singleton));
            }
        }
        private static void DoAddConnectionMultiplexer(IServiceCollection services, RedisServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime)
        {
            Type       redisInterface      = RedisTypeLocator.StackExchangeRedisInterface;
            Type       redisImplementation = RedisTypeLocator.StackExchangeRedisImplementation;
            Type       redisOptions        = RedisTypeLocator.StackExchangeRedisOptions;
            MethodInfo initializer         = RedisTypeLocator.StackExchangeInitializer;

            if (redisInterface == null || redisImplementation == null || redisOptions == null || initializer == null)
            {
                throw new ConnectorException("Unable to find required Redis types, are you missing a StackExchange.Redis Nuget Package?");
            }

            RedisCacheConnectorOptions   redisConfig = new RedisCacheConnectorOptions(config);
            RedisServiceConnectorFactory factory     = new RedisServiceConnectorFactory(info, redisConfig, redisImplementation, redisOptions, initializer ?? null);

            services.Add(new ServiceDescriptor(redisInterface, factory.Create, contextLifetime));
            services.Add(new ServiceDescriptor(redisImplementation, factory.Create, contextLifetime));
        }
Пример #16
0
        public static IServiceCollection AddRedisConnectionMultiplexer(this IServiceCollection services, IConfiguration config)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            RedisCacheConnectorOptions   redisConfig = new RedisCacheConnectorOptions(config);
            RedisServiceInfo             info        = config.GetSingletonServiceInfo <RedisServiceInfo>();
            RedisServiceConnectorFactory factory     = new RedisServiceConnectorFactory(info, redisConfig);;

            services.AddSingleton(typeof(ConnectionMultiplexer), factory.CreateConnection);
            return(services);
        }
Пример #17
0
        public static IServiceCollection AddDistributedRedisCache(this IServiceCollection services, IConfiguration config, ILoggerFactory logFactory = null)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            RedisCacheConnectorOptions   redisConfig = new RedisCacheConnectorOptions(config);
            RedisServiceInfo             info        = config.GetSingletonServiceInfo <RedisServiceInfo>();
            RedisServiceConnectorFactory factory     = new RedisServiceConnectorFactory(info, redisConfig);;

            services.AddSingleton(typeof(IDistributedCache), factory.CreateCache);
            return(services);
        }
Пример #18
0
        internal void UpdateOptions(RedisServiceInfo si, RedisCacheConnectorOptions configuration)
        {
            if (si == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(si.Host))
            {
                configuration.Host      = si.Host;
                configuration.Port      = si.Port;
                configuration.EndPoints = null;
            }

            if (!string.IsNullOrEmpty(si.Password))
            {
                configuration.Password = si.Password;
            }
        }
Пример #19
0
        public static RedisServiceConnectorFactory CreateRedisServiceConnectorFactory(this IConfiguration config, RedisCacheConnectorOptions connectorOptions, string serviceName = null)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            if (connectorOptions == null)
            {
                throw new ArgumentNullException(nameof(connectorOptions));
            }

            string[] redisAssemblies  = new string[] { "StackExchange.Redis", "StackExchange.Redis.StrongName", "Microsoft.Extensions.Caching.Redis" };
            string[] redisTypeNames   = new string[] { "StackExchange.Redis.ConnectionMultiplexer", "Microsoft.Extensions.Caching.Distributed.IDistributedCache" };
            string[] redisOptionNames = new string[] { "StackExchange.Redis.ConfigurationOptions", "Microsoft.Extensions.Caching.Redis.RedisCacheOptions" };

            Type       redisConnection = ConnectorHelpers.FindType(redisAssemblies, redisTypeNames);
            Type       redisOptions    = ConnectorHelpers.FindType(redisAssemblies, redisOptionNames);
            MethodInfo initializer     = ConnectorHelpers.FindMethod(redisConnection, "Connect");

            var info = serviceName == null?config.GetSingletonServiceInfo <RedisServiceInfo>() : config.GetRequiredServiceInfo <RedisServiceInfo>(serviceName);

            return(new RedisServiceConnectorFactory(info, connectorOptions, redisConnection, redisOptions, initializer ?? null));
        }
Пример #20
0
 public RedisServiceConnectorFactory(RedisServiceInfo sinfo, RedisCacheConnectorOptions config)
 {
     _info   = sinfo;
     _config = config;
 }
 /// <summary>
 /// Create a configuration object to be used to connect to Redis
 /// </summary>
 /// <param name="si">Redis Service Info</param>
 /// <param name="configuration">Configuration parameters</param>
 /// <returns>A dynamically typed object for use connecting to Redis</returns>
 public RedisCacheConnectorOptions Configure(RedisServiceInfo si, RedisCacheConnectorOptions configuration)
 {
     // apply service info to exising configuration
     UpdateOptions(si, configuration);
     return(configuration);
 }
Пример #22
0
 internal void UpdateOptions(RedisCacheConnectorOptions options, RedisCacheOptions redisOptions)
 {
     redisOptions.Configuration = options.ToString();
     redisOptions.InstanceName  = options.InstanceId;
 }