Reputation:
i have used one static variable and my static variable default value is 1
. then this variable value change based on user login..and my static variable value used for changing theme based on user login.
public static int theme = 1;
i have checked 5 user login at the same time it's working fine but i want to know when millions of user login at the same time in my site whether this static variable fails or not?
please give me suggestion
regards, jatin
Upvotes: 0
Views: 84
Reputation: 48415
If this is an asp.net site, which it sounds like it is, then you should not use a static
variable for this.
The reason why you shouldn't use static is because it will not be unique to the user, it will be the same variable to all users that connect to your website (I believe it's actually unique to the application pool). That means that when one user changes it, then it will change for all other users too.
Use a session variable instead:
// Set.
Session["Theme"] = 1;
// Get.
int theme = (int)Session["Theme"];
For convenience you could wrap this in a static property...
public static int Theme
{
get
{
if(Session["Theme"] == null)
return 0;// Or an alternate default value.
return (int)Session["Theme"];
}
set { Session["Theme"] = value; }
}
Upvotes: 2
Reputation: 1062865
You mention a static
variable, and you mention multiple users. This is an alarm bell, because the entire point of a static
variable is that there is exactly one value (caveat, see below). It will work fine if all your users always have exactly the same theme
(i.e. it is a global configuration setting); but: it this is meant to be a per-user setting, it should be a property of your user model, or some other per-user storage (cookies, session-state, etc).
The caveat here is [ThreadStatic]
; but that really doesn't change anything in your scenario, especially if this is asp.net (since threads are not synonymous with either users or requests).
Upvotes: 5