Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions RedisSessionProvider/Config/RedisConnectionConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ static RedisConnectionConfig()
/// </summary>
public static Func<HttpContextBase, KeyValuePair<string, ConfigurationOptions>> GetSERedisServerConfig = null;

//Redis High Availability (Cluster/Sentinel) Feature

/// <summary>
/// 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.
/// </summary>
public static Func<HttpContextBase, KeyValuePair<string, ConnectionMultiplexer>> GetSERedisServerConnection = null;

/// <summary>
/// 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
Expand Down
41 changes: 37 additions & 4 deletions RedisSessionProvider/Redis/RedisConnectionWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ static RedisConnectionWrapper()
/// </summary>
private ConfigurationOptions connData;

//Redis High Availability (Cluster/Sentinel) Feature

/// <summary>
/// Gets or sets the StackExchanage.Redis.ConnectionMultiplexer to use when connecting to a redis server
/// </summary>
private ConnectionMultiplexer _connection;

/// <summary>
/// A string identifier for the connection, which will be used as the connection's key in the
/// this.RedisConnections dictionary.
Expand Down Expand Up @@ -118,6 +125,21 @@ public RedisConnectionWrapper(string connIdentifier, int dbIndex, ConfigurationO
this.ConnectionID = connIdentifier;
}

//Redis High Availability (Cluster/Sentinel) Feature

/// <summary>
/// Initializes a new instance of the RedisConnectionWrapper class, which contains methods for accessing
/// a static concurrentdictionary of already created and open redis connection instances
/// </summary>
/// <param name="connIdentifier">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.</param>
/// <param name="connection">A StackExchange.Redis.ConnectionMultiplexer instance to interact with Redis</param>
public RedisConnectionWrapper(string connIdentifier, ConnectionMultiplexer connection)
{
this.ConnectionID = connIdentifier;
this._connection = connection;
}

/// <summary>
/// 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
Expand All @@ -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));
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions RedisSessionProvider/RedisSessionStateStoreProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ConnectionMultiplexer> connData =
RedisConnectionConfig.GetSERedisServerConnection(context);
return new RedisConnectionWrapper(connData.Key, connData.Value);
}

throw new ConfigurationErrorsException(
"RedisSessionProvider.Config.RedisConnectionWrapper.GetSERedisServerConfig delegate not set " +
Expand Down