Beispiel #1
0
        public void Init()
        {
            var services    = new ServiceCollection();
            var redisConfig = new RedisConfiguration
            {
                MaxValueLength     = 2810,
                PoolSize           = 20,
                AbortOnConnectFail = false,
                Database           = 0,
                KeyPrefix          = "lyl:",
                ConnectTimeout     = 100,
                Hosts = new RedisHost[] {
                    new RedisHost {
                        Host = "198.185.15.16", Port = 6379
                    }
                },
            };

            redisConfig.ServerEnumerationStrategy = new ServerEnumerationStrategy()
            {
                Mode       = ServerEnumerationStrategy.ModeOptions.All,
                TargetRole = ServerEnumerationStrategy.TargetRoleOptions.Any,
                UnreachableServerAction = ServerEnumerationStrategy.UnreachableServerActionOptions.Throw
            };
            IRedisCacheConnectionPoolManager poolManager = new RedisCacheConnectionPoolManager(redisConfig);

            _redisDatabase = new RedisDatabase(connectionPoolManager: poolManager, new SystemTextJsonSerializer(), redisConfig.ServerEnumerationStrategy, 0, 0, redisConfig.KeyPrefix);
        }
Beispiel #2
0
 public CacheClient(RedisConfiguration config)
 {
     this.config    = config;
     ConnectionPool = new RedisCacheConnectionPoolManager(this.config);
     Client         = new RedisCacheClient(ConnectionPool, new Utf8JsonSerializer(), config);
     RedisDb        = Client.GetDbFromConfiguration();
     Db             = RedisDb.Database;
 }
Beispiel #3
0
        public RedisDbConnector()
        {
            var config = new RedisConfiguration {
                Hosts = new[] { new RedisHost {
                                    Host = "localhost", Port = 6379
                                } }
            };

            _connectionPool = new RedisCacheConnectionPoolManager(config);
        }
Beispiel #4
0
 public CacheClient(RedisConfiguration config)
 {
     this.config = config;
     config.ConfigurationOptions.CertificateSelection += ConfigurationOptions_CertificateSelection;
     // MEMO: Don't do this in production (Dev only)
     // hack for local redis tls docker
     config.ConfigurationOptions.CertificateValidation += ConfigurationOptions_CertificateValidation;
     ConnectionPool = new RedisCacheConnectionPoolManager(this.config);
     Client         = new RedisCacheClient(ConnectionPool, new Utf8JsonSerializer(), config);
     RedisDb        = Client.GetDbFromConfiguration();
     Db             = RedisDb.Database;
 }
Beispiel #5
0
        private static IRedisCacheClient GetCacheClient(RedisConfiguration redisConfig, ISerializer sz = null)
        {
            var poolManager = new RedisCacheConnectionPoolManager(redisConfig);

            if (sz == null)
            {
                sz = new MsgPackObjectSerializer();
            }
            var cacheClient = new RedisCacheClient(poolManager, sz, redisConfig);

            return(cacheClient);
        }
Beispiel #6
0
        public RedisCache()
        {
            var configuration = ConfigurationManagerExtension.GetSection("redisCacheClient") as RedisCachingSectionHandler;

            if (configuration == null)
            {
                throw new ConfigurationErrorsException("Unable to locate <redisCacheClient> section into your configuration file. Take a look https://github.com/imperugo/StackExchange.Redis.Extensions");
            }

            var redisConfiguration = RedisCachingSectionHandler.GetConfig();

            var connectionPoolManager = new RedisCacheConnectionPoolManager(redisConfiguration);

            _redis = new RedisCacheClient(connectionPoolManager, new NewtonsoftSerializer(), redisConfiguration).GetDbFromConfiguration();
        }
        /// <summary>
        /// Add AddCacheStores to ServiceCollection
        /// </summary>
        /// <param name="services"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IServiceCollection AddCacheStores(this IServiceCollection services, IOptions <CacheStoreOptions> options)
        {
            Console.WriteLine("Deregistering providers for ICacheStore");

            services.Remove <ICacheStore>();
            services.Remove <ISerializer>();

            services
            .AddOptions()
            .AddOptions <CacheStoreOptions>();

            if (options.Value.CacheStorage.Equals("redis", StringComparison.OrdinalIgnoreCase))
            {
                Console.WriteLine("Registering RedisStore provider for ICacheStore");

                Console.WriteLine($"Using {options.Value.Serializer} serializer.");

                if (options.Value.Serializer == CacheStoreSerializer.Protobuf)
                {
                    services.AddSingleton <ISerializer, ProtobufSerializer>();
                }
                else
                {
                    services.AddSingleton <ISerializer, MsgPackObjectSerializer>();
                }

                services.AddSingleton(RedisCacheConnectionPoolManager.GetRedisConfiguration(options.Value.RedisHost));
                services.AddSingleton <IRedisCacheClient, RedisCacheClient>();
                services.AddSingleton <IRedisCacheConnectionPoolManager, RedisCacheConnectionPoolManager>();
                services.AddSingleton <IRedisDefaultCacheClient, StackExchange.Redis.Extensions.Core.Implementations.RedisDefaultCacheClient>();
                services.AddScoped <ICacheStore, RedisStore>();
            }
            else if (options.Value.CacheStorage.Equals("memory", StringComparison.OrdinalIgnoreCase))
            {
                Console.WriteLine("Registering MemoryStore provider for ICacheStore");

                services.AddMemoryCache();
                services.AddScoped <ICacheStore, MemoryStore>();
            }
            else
            {
                Console.WriteLine("Registering NullStore provider for ICacheStore");

                services.AddScoped <ICacheStore, NullStore>();
            }

            return(services);
        }
Beispiel #8
0
        public static void Connect(string redisIp, int port)
        {
            if (ConnectionPoolManager != null)
            {
                return;
            }

            ConnectionPoolManager = new RedisCacheConnectionPoolManager(new RedisConfiguration {
                Hosts = new RedisHost[] {
                    new RedisHost {
                        Host = redisIp,
                        Port = port
                    }
                }
            });
        }
Beispiel #9
0
        public RedisDistributedAppLockProviderTest()
        {
            _hostname = "127.0.0.1";
            _port     = 6379;

            _connectionPool = new RedisCacheConnectionPoolManager(new RedisConfiguration
            {
                Hosts = new RedisHost[]
                {
                    new RedisHost()
                    {
                        Host = _hostname,
                        Port = _port
                    }
                }
            });
        }
Beispiel #10
0
        private static ICodeStorage GetRedisCacheStorage()
        {
            var redisConfig = new RedisConfiguration
            {
                Hosts = new RedisHost[] {
                    new RedisHost {
                        Host = "127.0.0.1",
                        Port = 6379
                    }
                }
            };
            var redisManager = new RedisCacheConnectionPoolManager(redisConfig);
            var redisClient  = new RedisCacheClient(redisManager,
                                                    new NewtonsoftSerializer(), redisConfig);//new ProtobufSerializer();
            var storage = new RedisCacheStorage(redisClient);

            return(storage);
        }
Beispiel #11
0
        /// <summary>
        /// Adds the services.
        /// </summary>
        /// <param name="services">The services.</param>
        private void AddServices(IServiceCollection services)
        {
            services.AddHttpContextAccessor();

            services.AddSingleton <ISerializer>(service => new NewtonsoftSerializer(new JsonSerializerSettings
            {
                TypeNameHandling  = TypeNameHandling.All,
                NullValueHandling = NullValueHandling.Include
            }));

            services.AddSingleton <IRedisCacheClient>(service =>
            {
                RedisConfiguration redisConfiguration = new RedisConfiguration()
                {
                    AbortOnConnectFail = true,
                    Hosts = new RedisHost[]
                    {
                        new RedisHost
                        {
                            Host = Configuration["KeyValueStore:host"],
                            Port = Convert.ToInt32(Configuration["KeyValueStore:port"])
                        }
                    },
                    AllowAdmin = true,
                    Database   = 0,
                    ServerEnumerationStrategy = new ServerEnumerationStrategy()
                    {
                        Mode       = ServerEnumerationStrategy.ModeOptions.All,
                        TargetRole = ServerEnumerationStrategy.TargetRoleOptions.Any,
                        UnreachableServerAction = ServerEnumerationStrategy.UnreachableServerActionOptions.Throw
                    },
                    PoolSize = 50
                };

                IRedisCacheConnectionPoolManager redisCacheConnectionPoolManager = new RedisCacheConnectionPoolManager(redisConfiguration);
                ISerializer serializer = service.GetRequiredService <ISerializer>();
                return(new RedisCacheClient(redisCacheConnectionPoolManager, serializer, redisConfiguration));
            });

            services.AddSingleton <IUrlService>(service => new UrlService(service.GetRequiredService <IKeyValueStore>(), service.GetRequiredService <IHttpContextAccessor>()));
            services.AddSingleton <IKeyValueStore>(service => new KeyValueStore(service.GetRequiredService <IRedisCacheClient>()));

            services.AddControllers(options => options.Filters.Add(new HttpResponseExceptionFilter()));
        }
        public async Task Open_Tons_of_concurrent_connections()
        {
            var sut = new RedisCacheConnectionPoolManager(new RedisConfiguration()
            {
                AbortOnConnectFail = false,
                Hosts = new RedisHost[]
                {
                    new RedisHost()
                    {
                        Host = "localhost", Port = 6379
                    }
                },
                AllowAdmin                = true,
                ConnectTimeout            = 5000,
                Database                  = 8,
                PoolSize                  = 10,
                ServerEnumerationStrategy = new ServerEnumerationStrategy()
                {
                    Mode       = ServerEnumerationStrategy.ModeOptions.All,
                    TargetRole = ServerEnumerationStrategy.TargetRoleOptions.Any,
                    UnreachableServerAction = ServerEnumerationStrategy.UnreachableServerActionOptions.Throw
                }
            });

            await sut.GetConnection().GetDatabase().StringSetAsync("key", "value");

            var conn = sut.GetConnection();

            Parallel.For(0, 1000, x =>
            {
                try
                {
                    sut.GetConnection().GetDatabase().StringGet("key");
                }
                catch (Exception exc)
                {
                    output.WriteLine($"Index: {x} - Exception {exc.ToString()}");
                    throw;
                }
            });
        }
Beispiel #13
0
        public RedisCacheConnectionPoolManagerTests(ITestOutputHelper output)
        {
            // See more info here: https://gist.github.com/JonCole/e65411214030f0d823cb#file-threadpool-md
            // Everything started from here https://gist.github.com/JonCole/925630df72be1351b21440625ff2671f#file-redis-bestpractices-stackexchange-redis-md
            ThreadPool.GetMaxThreads(out var maxThread, out var maxIOThread);
            ThreadPool.SetMinThreads(maxThread / 2, maxIOThread);

            this.output = output;

            var configuration = new RedisConfiguration()
            {
                AbortOnConnectFail = true,
                KeyPrefix          = "MyPrefix__",
                Hosts = new RedisHost[]
                {
                    new RedisHost {
                        Host = "localhost", Port = 6379
                    }
                },
                AllowAdmin                = true,
                ConnectTimeout            = 3000,
                Database                  = 0,
                PoolSize                  = 5,
                ServerEnumerationStrategy = new ServerEnumerationStrategy()
                {
                    Mode       = ServerEnumerationStrategy.ModeOptions.All,
                    TargetRole = ServerEnumerationStrategy.TargetRoleOptions.Any,
                    UnreachableServerAction = ServerEnumerationStrategy.UnreachableServerActionOptions.Throw
                }
            };

            var logger = output.BuildLoggerFor <RedisCacheConnectionPoolManager>();

            sut         = new RedisCacheConnectionPoolManager(configuration, logger);
            this.output = output;
        }
Beispiel #14
0
        public void ConfigureServices(IServiceCollection services)
        {
            var redisConfiguration = _configuration.GetSection(ConfigConstants.REDIS).Get <RedisConfiguration>();

            if (redisConfiguration == null)
            {
                throw new InvalidOperationException(
                          $"Unable to locate the [{ConfigConstants.REDIS}] configuration block.");
            }

            var redisCacheConnectionPoolManager = new RedisCacheConnectionPoolManager(redisConfiguration);
            var serializer       = new NewtonsoftSerializer();
            var redisCacheClient = new RedisCacheClient(redisCacheConnectionPoolManager, serializer,
                                                        redisConfiguration);
            var redisCacheProvider = new JdtRedisCacheProvider(Log.Logger, _configuration, redisCacheClient);

            services.AddSingleton(redisConfiguration);
            services.AddSingleton <ISerializer>(serializer);
            services.AddSingleton <IRedisCacheClient, RedisCacheClient>();
            services.AddSingleton <IRedisCacheConnectionPoolManager>(redisCacheConnectionPoolManager);
            services.AddSingleton <IRedisCacheClient>(redisCacheClient);

            Log.Logger.Information("A Redis connection has been initialized for this service");

            services.AddAuthentication(options =>
            {
                options.DefaultScheme          = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
            .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
            {
                options.ClientId     = _configuration["oidc:clientId"];
                options.ClientSecret = _configuration["oidc:clientSecret"];
                options.Authority    = _configuration["oidc:authority"];
                options.ResponseType = _configuration["oidc:responseType"];
                options.GetClaimsFromUserInfoEndpoint = bool.TryParse(_configuration["oidc:getClaimsFromUserInfoEndpoint"], out var option) && option;
                options.RequireHttpsMetadata          = true;
                options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.SaveTokens   = true;

                var stsDiscoveryEndpoint = _configuration[ConfigConstants.STS_DISCOVERY_ENDPOINT];

                options.ConfigurationManager =
                    new ConfigurationManager <OpenIdConnectConfiguration>(stsDiscoveryEndpoint,
                                                                          new JdtOpenIdRedisConfigurationRetriever(redisCacheProvider, Log.Logger));

                options.Validate();
            })
            .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
            {
                options.Cookie.HttpOnly     = true;
                options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
                options.Cookie.SameSite     = SameSiteMode.Lax;
            })
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
            {
                var stsDiscoveryEndpoint = _configuration[ConfigConstants.STS_DISCOVERY_ENDPOINT];

                options.ConfigurationManager =
                    new ConfigurationManager <OpenIdConnectConfiguration>(stsDiscoveryEndpoint,
                                                                          new JdtOpenIdRedisConfigurationRetriever(redisCacheProvider, Log.Logger));

                options.Audience  = _configuration["oidc:audience"];
                options.Authority = _configuration["oidc:authority"];
            });

            services.AddAuthorization(options =>
            {
                var defaultAuthorizationPolicyBuilder = new AuthorizationPolicyBuilder(
                    JwtBearerDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme,
                    CookieAuthenticationDefaults.AuthenticationScheme);

                defaultAuthorizationPolicyBuilder =
                    defaultAuthorizationPolicyBuilder.RequireAuthenticatedUser();

                var defaultAuthorizationPolicy = defaultAuthorizationPolicyBuilder.Build();

                options.DefaultPolicy = defaultAuthorizationPolicy;


                // oidc
                var oidcAuthorizationPolicyBuilder = new AuthorizationPolicyBuilder(OpenIdConnectDefaults.AuthenticationScheme);

                oidcAuthorizationPolicyBuilder =
                    oidcAuthorizationPolicyBuilder.RequireAuthenticatedUser();

                var oidcAuthorizationPolicy = oidcAuthorizationPolicyBuilder.Build();

                options.AddPolicy(OpenIdConnectDefaults.AuthenticationScheme, oidcAuthorizationPolicy);


                // bearer

                var bearerAuthorizationPolicyBuilder = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme);

                bearerAuthorizationPolicyBuilder =
                    bearerAuthorizationPolicyBuilder.RequireAuthenticatedUser();

                var bearerAuthorizationPolicy = bearerAuthorizationPolicyBuilder.Build();

                options.AddPolicy(JwtBearerDefaults.AuthenticationScheme, bearerAuthorizationPolicy);
            });

            services.AddSingleton(redisCacheClient);
            services.AddControllers();
        }