internal ConfigurationOptions ConfigureConnection(RedisServiceInfo si, RedisCacheConnectorOptions options) { UpdateOptions(si, options); ConfigurationOptions redisOptions = ConfigurationOptions.Parse(options.ToString()); return(redisOptions); }
public void Create_CanReturnConnectionMultiplexer() { // arrange RedisCacheConnectorOptions config = new RedisCacheConnectorOptions() { Host = "localhost", Port = 1234, Password = "******", InstanceName = "instanceId", AbortOnConnectFail = false, ConnectTimeout = 1 }; RedisServiceInfo si = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "127.0.0.1", 4321, "sipassword") { ApplicationInfo = new ApplicationInstanceInfo() { InstanceId = "instanceId" } }; // act var factory = new RedisServiceConnectorFactory(si, config, typeof(ConnectionMultiplexer), typeof(ConfigurationOptions), RedisTypeLocator.StackExchangeInitializer); var multi = factory.Create(null); // assert Assert.NotNull(multi); Assert.IsType <ConnectionMultiplexer>(multi); }
public void ConfigureConnection_ServiceInfoOveridesConfig_ReturnsExpected() { RedisCacheConfigurer configurer = new RedisCacheConfigurer(); RedisCacheConnectorOptions config = new RedisCacheConnectorOptions() { Host = "localhost", Port = 1234, Password = "******" }; RedisServiceInfo si = new RedisServiceInfo("myId", "foobar", 4321, "sipassword"); si.ApplicationInfo = new ApplicationInstanceInfo() { InstanceId = "instanceId" }; var opts = configurer.ConfigureConnection(si, config); Assert.NotNull(opts); Assert.NotNull(opts.EndPoints); var ep = opts.EndPoints[0] as DnsEndPoint; Assert.NotNull(ep); Assert.Equal("foobar", ep.Host); Assert.Equal(4321, ep.Port); Assert.Equal("sipassword", opts.Password); }
public void Create_CanReturnRedisCache() { // arrange RedisCacheConnectorOptions config = new RedisCacheConnectorOptions() { Host = "localhost", Port = 1234, Password = "******", InstanceName = "instanceId" }; RedisServiceInfo si = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "foobar", 4321, "sipassword") { ApplicationInfo = new ApplicationInstanceInfo() { InstanceId = "instanceId" } }; // act var factory = new RedisServiceConnectorFactory(si, config, typeof(RedisCache), typeof(RedisCacheOptions), null); var cache = factory.Create(null); // assert Assert.NotNull(cache); Assert.IsType <RedisCache>(cache); }
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)); }
public void Create_CanReturnConnectionMultiplexer() { // arrange RedisCacheConnectorOptions config = new RedisCacheConnectorOptions() { Host = "localhost", Port = 1234, Password = "******", InstanceName = "instanceId", AbortOnConnectFail = false, ConnectTimeout = 1 }; RedisServiceInfo si = new RedisServiceInfo("myId", "127.0.0.1", 4321, "sipassword") { ApplicationInfo = new ApplicationInstanceInfo() { InstanceId = "instanceId" } }; MethodInfo initializer = typeof(ConnectionMultiplexer).GetMethod("Connect", new Type[] { typeof(ConfigurationOptions), typeof(TextWriter) }); // act var factory = new RedisServiceConnectorFactory(si, config, typeof(ConnectionMultiplexer), typeof(ConfigurationOptions), initializer); var multi = factory.Create(null); // assert Assert.NotNull(multi); Assert.IsType <ConnectionMultiplexer>(multi); }
public void ConfigureConnection_ServiceInfoOveridesConfig_ReturnsExpected() { // arrange var configurer = new RedisCacheConfigurer(); var config = new RedisCacheConnectorOptions() { Host = "localhost", Port = 1234, Password = "******" }; var si = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "foobar", 4321, "sipassword"); // act var opts = configurer.Configure(si, config); Assert.NotNull(opts); // assert Assert.NotNull(((ConfigurationOptions)opts.ToStackExchangeObject(typeof(ConfigurationOptions))).EndPoints); var ep = ((ConfigurationOptions)opts.ToStackExchangeObject(typeof(ConfigurationOptions))).EndPoints[0] as DnsEndPoint; Assert.NotNull(ep); Assert.Equal("foobar", ep.Host); Assert.Equal(4321, ep.Port); Assert.Equal("sipassword", opts.Password); }
/// <summary> /// Adds ConnectionMultiplexer (as ConnectionMultiplexer and IConnectionMultiplexer) to your Autofac Container /// </summary> /// <param name="container">Your Autofac Container Builder</param> /// <param name="config">Application configuration</param> /// <param name="serviceName">Cloud Foundry service name binding</param> /// <returns>the RegistrationBuilder for (optional) additional configuration</returns> public static IRegistrationBuilder <object, SimpleActivatorData, SingleRegistrationStyle> RegisterRedisConnectionMultiplexer( this ContainerBuilder container, IConfiguration config, string serviceName = null) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } Type redisInterface = RedisTypeLocator.StackExchangeInterface; Type redisImplementation = RedisTypeLocator.StackExchangeImplementation; Type redisOptions = RedisTypeLocator.StackExchangeOptions; MethodInfo initializer = RedisTypeLocator.StackExchangeInitializer; RedisServiceInfo info = serviceName != null ? config.GetRequiredServiceInfo <RedisServiceInfo>(serviceName) : config.GetSingletonServiceInfo <RedisServiceInfo>(); RedisCacheConnectorOptions redisConfig = new RedisCacheConnectorOptions(config); RedisServiceConnectorFactory factory = new RedisServiceConnectorFactory(info, redisConfig, redisImplementation, redisOptions, initializer ?? null); container.Register(c => new RedisHealthContributor(factory, redisImplementation, c.ResolveOptional <ILogger <RedisHealthContributor> >())).As <IHealthContributor>(); return(container.Register(c => factory.Create(null)).As(redisInterface, redisImplementation)); }
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; } } }
public void Configure_ServiceInfoOveridesConfig_ReturnsExpected() { RedisCacheConfigurer configurer = new RedisCacheConfigurer(); RedisCacheConnectorOptions config = new RedisCacheConnectorOptions() { Host = "localhost", Port = 1234, Password = "******", InstanceId = "instanceId" }; RedisServiceInfo si = new RedisServiceInfo("myId", "foobar", 4321, "sipassword"); si.ApplicationInfo = new ApplicationInstanceInfo() { InstanceId = "instanceId" }; var opts = configurer.Configure(si, config); Assert.NotNull(opts); var redisOptions = opts.Value; Assert.NotNull(redisOptions); Assert.Equal("foobar:4321,password=sipassword,allowAdmin=false,abortConnect=true,resolveDns=false,ssl=false", redisOptions.Configuration); Assert.Equal("instanceId", redisOptions.InstanceName); }
/// <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; }
internal IOptions <RedisCacheOptions> Configure(RedisServiceInfo si, RedisCacheConfiguration configuration) { RedisCacheOptions redisOptions = new RedisCacheOptions(); UpdateOptions(configuration, redisOptions); UpdateOptions(si, redisOptions); return(new ConnectorIOptions <RedisCacheOptions>(redisOptions)); }
internal IOptions <RedisCacheOptions> Configure(RedisServiceInfo si, RedisCacheConnectorOptions options) { UpdateOptions(si, options); RedisCacheOptions redisOptions = new RedisCacheOptions(); UpdateOptions(options, redisOptions); return(new ConnectorIOptions <RedisCacheOptions>(redisOptions)); }
public void UpdateOptions_FromServiceInfo_ReturnsExcpected() { var configurer = new RedisCacheConfigurer(); var connOptions = new RedisCacheConnectorOptions(); var si = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "foobar", 4321, "sipassword"); configurer.UpdateOptions(si, connOptions); Assert.Equal("foobar:4321,password=sipassword,allowAdmin=false,abortConnect=true,resolveDns=false,ssl=false", connOptions.ToString()); Assert.Null(connOptions.InstanceName); }
public void Microsoft_Is_Connected_Returns_Up_Status() { var redisOptions = new RedisCacheConnectorOptions(); var sInfo = new RedisServiceInfo("MyId", "redis://localhost:6379"); var logrFactory = new LoggerFactory(); var connFactory = new RedisServiceConnectorFactory(sInfo, redisOptions, RedisTypeLocator.MicrosoftImplementation, RedisTypeLocator.MicrosoftOptions, null); var h = new RedisHealthContributor(connFactory, RedisTypeLocator.MicrosoftImplementation, logrFactory.CreateLogger <RedisHealthContributor>()); var status = h.Health(); Assert.Equal(HealthStatus.UP, status.Status); }
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)); }
public void Constructor_CreatesExpected_WithSchema(string scheme) { string uri = $"{scheme}://:joes_password@localhost:6380/"; RedisServiceInfo redisInfo = new RedisServiceInfo("myId", scheme, "localhost", 1527, "joes_password"); Assert.Equal("myId", redisInfo.Id); Assert.Equal(scheme, redisInfo.Scheme); Assert.Equal("localhost", redisInfo.Host); Assert.Equal(1527, redisInfo.Port); Assert.Equal("joes_password", redisInfo.Password); Assert.Null(redisInfo.Path); Assert.Null(redisInfo.Query); }
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)); }
public void UpdateOptions_FromServiceInfo_ReturnsExcpected() { RedisCacheConfigurer configurer = new RedisCacheConfigurer(); RedisCacheOptions redisOptions = new RedisCacheOptions(); RedisServiceInfo si = new RedisServiceInfo("myId", "foobar", 4321, "sipassword"); si.ApplicationInfo = new ApplicationInstanceInfo() { InstanceId = "instanceId" }; configurer.UpdateOptions(si, redisOptions); Assert.Equal("foobar:4321,password=sipassword", redisOptions.Configuration); Assert.Equal("instanceId", redisOptions.InstanceName); }
public void UpdateOptions_FromServiceInfo_ReturnsExcpected() { RedisCacheConfigurer configurer = new RedisCacheConfigurer(); RedisCacheConnectorOptions connOptions = new RedisCacheConnectorOptions(); RedisServiceInfo si = new RedisServiceInfo("myId", "foobar", 4321, "sipassword"); si.ApplicationInfo = new ApplicationInstanceInfo() { ApplicationId = "applicationId" }; configurer.UpdateOptions(si, connOptions); Assert.Equal("foobar:4321,password=sipassword,allowAdmin=false,abortConnect=true,resolveDns=false,ssl=false", connOptions.ToString()); Assert.Null(connOptions.InstanceId); }
public void StackExchange_Is_Connected_Returns_Up_Status() { // arrange var redisOptions = new RedisCacheConnectorOptions(); var sInfo = new RedisServiceInfo("MyId", "redis://localhost:6379"); var logrFactory = new LoggerFactory(); var connFactory = new RedisServiceConnectorFactory(sInfo, redisOptions, RedisTypeLocator.StackExchangeImplementation, RedisTypeLocator.StackExchangeOptions, RedisTypeLocator.StackExchangeInitializer); var h = new RedisHealthContributor(connFactory, RedisTypeLocator.StackExchangeImplementation, logrFactory.CreateLogger <RedisHealthContributor>()); // act var status = h.Health(); // assert Assert.Equal(HealthStatus.UP, status.Status); }
public void Microsoft_Not_Connected_Returns_Down_Status() { var redisOptions = new RedisCacheConnectorOptions() { ConnectTimeout = 1 }; var sInfo = new RedisServiceInfo("MyId", "redis://localhost:6378"); var logrFactory = new LoggerFactory(); var connFactory = new RedisServiceConnectorFactory(sInfo, redisOptions, RedisTypeLocator.MicrosoftImplementation, RedisTypeLocator.MicrosoftOptions, null); var h = new RedisHealthContributor(connFactory, RedisTypeLocator.MicrosoftImplementation, logrFactory.CreateLogger <RedisHealthContributor>()); var status = h.Health(); Assert.Equal(HealthStatus.DOWN, status.Status); Assert.Equal("Redis health check failed", status.Description); }
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 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); 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 void Configure_ServiceInfoOveridesConfig_ReturnsExpected() { var configurer = new RedisCacheConfigurer(); var config = new RedisCacheConnectorOptions() { Host = "localhost", Port = 1234, Password = "******", InstanceName = "instanceId" }; var si = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "foobar", 4321, "sipassword"); var connectionSettings = configurer.Configure(si, config); Assert.NotNull(connectionSettings); Assert.Equal("foobar:4321,password=sipassword,allowAdmin=false,abortConnect=true,resolveDns=false,ssl=false", connectionSettings.ToString()); Assert.Equal("instanceId", connectionSettings.InstanceName); }
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)); }
/// <summary> /// Add Redis Connection Multiplexer and its IHealthContributor to ServiceCollection /// </summary> /// <param name="services">Service collection to add to</param> /// <param name="applicationConfiguration">App configuration</param> /// <param name="connectorConfiguration">Connector configuration</param> /// <param name="serviceName">Name of service to add</param> /// <param name="contextLifetime"><see cref="ServiceLifetime"/> of the service to inject</param> /// <param name="healthChecksBuilder">Microsoft HealthChecksBuilder</param> /// <returns>IServiceCollection for chaining</returns> /// <remarks>ConnectionMultiplexer is retrievable as both ConnectionMultiplexer and IConnectionMultiplexer</remarks> public static IServiceCollection AddRedisConnectionMultiplexer(this IServiceCollection services, IConfiguration applicationConfiguration, IConfiguration connectorConfiguration, string serviceName, ServiceLifetime contextLifetime = ServiceLifetime.Singleton, IHealthChecksBuilder healthChecksBuilder = null) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (applicationConfiguration == null) { throw new ArgumentNullException(nameof(applicationConfiguration)); } var configToConfigure = connectorConfiguration ?? applicationConfiguration; RedisServiceInfo info = serviceName == null?configToConfigure.GetSingletonServiceInfo <RedisServiceInfo>() : configToConfigure.GetRequiredServiceInfo <RedisServiceInfo>(serviceName); DoAddConnectionMultiplexer(services, info, configToConfigure, contextLifetime, healthChecksBuilder); return(services); }
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); }
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); }