How to flush the Redis connection information. when I should flush the information. And from where I should flush/dispose the Redis cache connection.
I am storing the session information in the Redis cache. We are moving the application from the physical server to Azure so I have implemented the Redis cache in place of the session.
Redis Connection I am storing in one class file which connects to the Redis database.
public class RedisStore
{
private static readonly Lazy<ConnectionMultiplexer> LazyConnection;`
static RedisStore()
{
//var configurationOptions = new ConfigurationOptions
//{
// EndPoints = { ConfigurationManager.AppSettings["redis.connection"] }
//};
LazyConnection = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect("server:343, server2:344, PASSWORD = abc$"));
}
public static ConnectionMultiplexer Connection => LazyConnection.Value;
public static IDatabase RedisCache => Connection.GetDatabase();
}`
I am calling the Redis cache in the Page load of the asp.net application like this:
protected void Page_Load(object sender, EventArgs e)
{
try
{
var redis = RedisStore.RedisCache;
clsLogin objLogin = new clsLogin();
var key = "Username";
var _userName = redis.StringSet(key, HttpContext.Current.User.Identity.Name;);
DataSet dtLoginInfo = objLogin.ValidateLoginInfo();
if (dtLoginInfo.Tables[0].Rows.Count > 0)
{
redis.StringSet("ResourceName", dtLoginInfo.Tables[0].Rows[0]["ResourceName"].ToString());
var val = redis.StringGet("ResourceName");
redis.StringSet("Role", dtLoginInfo.Tables[0].Rows[0]["IsAdmin"].ToString());
lblProgress.Text = HttpContext.Current.User.Identity.Name + " Please wait logging in progress. ";
Response.Redirect("abc.aspx",false);
}
else
lblProgress.Text = HttpContext.Current.User.Identity.Name + " is not authorized to access this page. Please check with Admin";
}
catch (Exception ex)
{
}
}