diff --git a/RedisSessionProvider/Config/RedisConnectionConfig.cs b/RedisSessionProvider/Config/RedisConnectionConfig.cs index 2b9a82f..f7349c4 100644 --- a/RedisSessionProvider/Config/RedisConnectionConfig.cs +++ b/RedisSessionProvider/Config/RedisConnectionConfig.cs @@ -32,6 +32,16 @@ static RedisConnectionConfig() /// public static Func> GetSERedisServerConfig = null; + //Redis High Availability (Cluster/Sentinel) Feature + + /// + /// A delegate for returning a StackExchange.Redis.ConnectionMultiplexer instance which will allow + /// application to pass an existing ConnectionMultiplexer for persisting session data. + /// Please assign a string key to your connection as well, in case you want to connect to multiple + /// sets of Redis instances. + /// + public static Func> GetSERedisServerConnection = null; + /// /// Gets or sets a logging delegate that takes as input the server ip and port of the connection used as /// a string and the number of total redis messages to it as a long diff --git a/RedisSessionProvider/Redis/RedisConnectionWrapper.cs b/RedisSessionProvider/Redis/RedisConnectionWrapper.cs index ee85df1..679b19f 100644 --- a/RedisSessionProvider/Redis/RedisConnectionWrapper.cs +++ b/RedisSessionProvider/Redis/RedisConnectionWrapper.cs @@ -36,6 +36,13 @@ static RedisConnectionWrapper() /// private ConfigurationOptions connData; + //Redis High Availability (Cluster/Sentinel) Feature + + /// + /// Gets or sets the StackExchanage.Redis.ConnectionMultiplexer to use when connecting to a redis server + /// + private ConnectionMultiplexer _connection; + /// /// A string identifier for the connection, which will be used as the connection's key in the /// this.RedisConnections dictionary. @@ -118,6 +125,21 @@ public RedisConnectionWrapper(string connIdentifier, int dbIndex, ConfigurationO this.ConnectionID = connIdentifier; } + //Redis High Availability (Cluster/Sentinel) Feature + + /// + /// Initializes a new instance of the RedisConnectionWrapper class, which contains methods for accessing + /// a static concurrentdictionary of already created and open redis connection instances + /// + /// Because it is possible to have connections to multiple redis instances, we store + /// a dictionary of them to reuse. This parameter is used as the key to that dictionary. + /// A StackExchange.Redis.ConnectionMultiplexer instance to interact with Redis + public RedisConnectionWrapper(string connIdentifier, ConnectionMultiplexer connection) + { + this.ConnectionID = connIdentifier; + this._connection = connection; + } + /// /// Method that returns a StackExchange.Redis.IDatabase object with ip and port number matching /// what was passed into the constructor for this instance of RedisConnectionWrapper @@ -132,10 +154,21 @@ public IDatabase GetConnection() { if (!RedisConnectionWrapper.RedisConnections.ContainsKey(this.ConnectionID)) { - RedisConnectionWrapper.RedisConnections.Add( - this.ConnectionID, - ConnectionMultiplexer.Connect( - this.connData)); + //Redis High Availability (Cluster/Sentinel) Feature + + //If _connection wasn't null, it means that the application has passed in + //an existing ConnectionMultiplexer instance and RedisConnectionConfig is + //of type GetSERedisServerConnection + if (this._connection != null) + { + RedisConnectionWrapper.RedisConnections.Add( + this.ConnectionID, this._connection); + } + else + { + RedisConnectionWrapper.RedisConnections.Add( + this.ConnectionID, ConnectionMultiplexer.Connect(this.connData)); + } } } } diff --git a/RedisSessionProvider/RedisSessionStateStoreProvider.cs b/RedisSessionProvider/RedisSessionStateStoreProvider.cs index ae967d7..305da6d 100644 --- a/RedisSessionProvider/RedisSessionStateStoreProvider.cs +++ b/RedisSessionProvider/RedisSessionStateStoreProvider.cs @@ -471,6 +471,13 @@ public static RedisConnectionWrapper RedisConnWrapperFromContext(HttpContextBase RedisConnectionConfig.GetRedisServerAddress(context)); } #pragma warning restore 0618 + //Redis High Availability (Cluster/Sentinel) Feature + else if (RedisConnectionConfig.GetSERedisServerConnection != null) + { + KeyValuePair connData = + RedisConnectionConfig.GetSERedisServerConnection(context); + return new RedisConnectionWrapper(connData.Key, connData.Value); + } throw new ConfigurationErrorsException( "RedisSessionProvider.Config.RedisConnectionWrapper.GetSERedisServerConfig delegate not set " +