首页 > 解决方案 > Redis 缓存显示相同的用户详细信息,不同的用户也登录。我应该如何、何时以及从何处处理连接

问题描述

如何刷新 Redis 连接信息。我什么时候应该刷新信息。我应该从哪里刷新/处理 Redis 缓存连接。

我将会话信息存储在 Redis 缓存中。我们正在将应用程序从物理服务器移动到 Azure,因此我实现了 Redis 缓存来代替会话。

Redis 连接我存储在一个连接到 Redis 数据库的类文件中。

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();
}`

我在 asp.net 应用程序的页面加载中调用 Redis 缓存,如下所示:

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

        }


    }

标签: visual-studioredisazure-devopssession-stateazure-redis-cache

解决方案


推荐阅读