c# - What is the best practice for caching user data in asp.net -
when user signs in website, want cache data email, confirmation status, mobile confirmation status, etc. because don't want fetch data in each page request. requirement user must confirm email , mobile before anything.
using code this:
public static class cacheddata { public static bool isemailconfirmed { { if (httpcontext.current.session["isemailconfirmed"] == null) initialize(); return convert.toboolean(httpcontext.current.session["isemailconfirmed"]); } set { httpcontext.current.session["isemailconfirmed"] = value; } } public static bool ismobileconfirmed { { 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 page classes drive it. using class cacheddata
in pagebase
class:
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 strange, code, work wrong , redirect setting page user confirmed email , mobile.
there better solution.
i think, if logic, should create object userinfo
. this:
public class userinfo { public string name {get; set; } public bool isemailconfirmed {get; set; } public bool ismobileconfirmed {get; set; } .... }
then set object session. now! when operation on user record performed in bll, should re-populate new instance of userinfo
, replace old 1 in session. way user info day , work.
but problem may coming fact use web farm , sessions not synchronized. need use sticky session each request unique user processed on same server. right there thing called app fabric
. caching on steroids. can find item in cache on server.
Comments
Post a Comment