Reputation: 2939
I am trying to understand how to implement redis by replacing all IIS session state values with the redis cache. I have redis working using a docker image. I am using a C# .Net Forms web app. I have included the StackExchange.Redis nuget package and set this up so far.
public class Redis
{
private static readonly Lazy<ConnectionMultiplexer> LazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
var redisConnectionString = ConfigurationManager.AppSettings["RedisConnectionString"];
var options = ConfigurationOptions.Parse(redisConnectionString);
options.AbortOnConnectFail = false;
return ConnectionMultiplexer.Connect(options);
});
public static ConnectionMultiplexer Connection => LazyConnection.Value;
}
But I am confused how to store the user context session key value pairs. By this I mean when I store a key for the user, say "UserId" can the key just be "UserId"? or do I needs to prefix it with a unique user specific context id. I cant find anything on how redis would work based on different users. How does it know the context of the user and hence how to get the correct key?
All I'm reading is that its a hashtable that stores values, which is fine for a single UserId, but I'm going to have lots of users with a UserId?
If anyone can help me understand this, that would be great, thanks you
Upvotes: 4
Views: 6439
Reputation: 2939
Ok so after looking at the following link I was able to get it all working
https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-aspnet-session-state-provider
docker run -p 8055:6379 --name redis --restart=always -d redis –-requirepass <your_long_password_here>
docker container ls
Install-Package Microsoft.Web.RedisSessionStateProvider
<sessionState mode="InProc" timeout="60" />
<sessionState mode="Custom" customProvider="MySessionStateStore"> <providers> <add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" connectionString="localhost:8055,password=your_long_password_here"/> </providers> </sessionState>
You should now see that you can run your website seamlessly, and you will now be using the redis cache and no longer the session state.
Upvotes: 7