[英]What is the best practice for caching user data in asp.net
When a user signs in to my website, I want cache some data like email, confirmation status, mobile confirmation status, etc. Because I don't want fetch this data in each page request. 当用户登录我的网站时,我想缓存一些数据,例如电子邮件,确认状态,移动确认状态等。因为我不想在每个页面请求中都获取此数据。 The requirement is that the user must confirm email and mobile before do anything.
要求是用户必须先确认电子邮件和手机才能做任何事情。
I am using code like this: 我正在使用这样的代码:
public static class CachedData
{
public static bool IsEmailConfirmed
{
get
{
if (HttpContext.Current.Session["IsEmailConfirmed"] == null)
Initialize();
return Convert.ToBoolean(HttpContext.Current.Session["IsEmailConfirmed"]);
}
set
{
HttpContext.Current.Session["IsEmailConfirmed"] = value;
}
}
public static bool IsMobileConfirmed
{
get
{
if (HttpContext.Current.Session["IsMobileConfirmed"] == null)
Initialize();
return Convert.ToBoolean(HttpContext.Current.Session["IsMobileConfirmed"]);
}
set
{
HttpContext.Current.Session["IsMobileConfirmed"] = value;
}
}
public static void Initialize()
{
UserAccount currentUser = UserAccount.GetUser();
if (currentUser == null)
return;
IsEmailConfirmed = currentUser.EmailConfirmed;
IsMobileConfirmed = currentUser.MobileConfirmed;
}
}
I have PageBase
class that all page classes drive from it. 我有所有页面类都
PageBase
驱动的PageBase
类。 I am using class CachedData
in PageBase
class: 我在
PageBase
类中使用类CachedData
:
public class PageBase : Page
{
protected override void OnInit(EventArgs e)
{
if (authentication.Required && User.Identity.IsAuthenticated && !IsPostBack)
{
if (CachedData.HasProfile && (!CachedData.IsEmailConfirmed || !CachedData.IsMobileConfirmed) && !Request.Url.AbsolutePath.ToLower().EndsWith("settings.aspx"))
Response.Redirect("/settings-page", true);
}
}
}
May be it is strange, but this code, sometimes work wrong and redirect to setting page for user confirmed email and mobile. 可能是奇怪的,但是此代码有时可能会出错,并重定向到用户确认的电子邮件和手机的设置页面。
Is there any better solution. 有没有更好的解决方案。
I think, if this is your logic, you should create an object UserInfo
. 我认为,如果这是您的逻辑,则应创建一个对象
UserInfo
。 Something like this: 像这样:
public class UserInfo
{
public string Name {get; set; }
public bool IsEmailConfirmed {get; set; }
public bool IsMobileConfirmed {get; set; }
....
}
Then set this object into session. 然后将此对象设置为会话。 Now!
现在! when any operation on user record are performed in your BLL, you should re-populate new Instance of
UserInfo
and replace old one in the session. 在BLL中对用户记录执行任何操作时,应重新填充
UserInfo
的新实例,并替换会话中的旧实例。 This way your user info will be up to day and will always work. 这样,您的用户信息将是最新的,并且将始终有效。
But your problem may coming from the fact that you use a web farm and your sessions are not synchronized. 但是您的问题可能出自以下事实:您使用Web场并且会话未同步。 You need to use a sticky session so each request from the unique user is processed on the same server.
您需要使用粘性会话,以便在同一服务器上处理来自唯一用户的每个请求。 Right now there is thing called
App Fabric
. 现在有一个叫做
App Fabric
东西。 It is caching on steroids. 它在类固醇上缓存。 It can find an item in cache on another server.
它可以在另一台服务器的缓存中找到一个项目。
You should not store different fields of your object in the different session names. 您不应在不同的会话名称中存储对象的不同字段。 If you do need to use sessions, you can store the whole user object in your session.
如果确实需要使用会话,则可以将整个用户对象存储在会话中。
The choice where to store the data depands on the requirements (including how critical are the data and the requirements for such the things as IIS reset) and what and why you really have to store. 存储数据的选择取决于需求(包括数据的严格程度以及IIS重置等需求的需求)以及存储的内容和原因。 Depending on the answers you could store your data either in a session or in a viewstate or in cache or in application.
根据答案,您可以将数据存储在会话,视图状态,缓存或应用程序中。 You can also look at the cache because it provides some nice features like automatic update, triggering, etc.
您还可以查看缓存,因为它提供了一些不错的功能,例如自动更新,触发等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.