0

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)
        {

        }


    }
Jayendran
  • 9,638
  • 8
  • 60
  • 103
Rohit Sharma
  • 179
  • 1
  • 3
  • 18
  • It's nothing to do with how and when you're creating the connection, your keys are global - ie `"Username"` and `"ResourceName"` - they will be stamping over each other. Are you trying to use this in place of a Session? – James Thorpe Oct 17 '18 at 15:49
  • Yes... I have replaced the Username and resourcename with redis from the session. Is it the correct way ? – Rohit Sharma Oct 18 '18 at 06:08

0 Answers0