Example #1
0
        private Connection GetConnection(RedisServiceInfo serviceInfo, IConfiguration configuration)
        {
            var redisConfig = new RedisCacheConnectorOptions(configuration);
            var configurer  = new RedisCacheConfigurer();

            return(new Connection(configurer.Configure(serviceInfo, redisConfig).ToString(), "Redis", serviceInfo));
        }
Example #2
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))
            {
                if (configuration.UrlEncodedCredentials)
                {
                    configuration.Password = WebUtility.UrlDecode(si.Password);
                }
                else
                {
                    configuration.Password = si.Password;
                }
            }

            if (si.Scheme == RedisServiceInfo.REDIS_SECURE_SCHEME)
            {
                configuration.Ssl = true;
            }
        }
Example #3
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;
 }
        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(redisConfig, serviceName));
        }
        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));
            }
        }
        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);

            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));
            }
        }
        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));
        }
        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));
            }

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

            var redisConnection = ReflectionHelpers.FindType(redisAssemblies, redisTypeNames);
            var redisOptions    = ReflectionHelpers.FindType(redisAssemblies, redisOptionNames);
            var initializer     = ReflectionHelpers.FindMethod(redisConnection, "Connect");

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

            return(new RedisServiceConnectorFactory(info, connectorOptions, redisConnection, redisOptions, initializer ?? null));
        }
Example #10
0
 /// <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);
 }