简体   繁体   中英

Using static lists vs static variables in ASP.NET

//ASP.NET.. Using GridListCache as global variable for single user
public static List<GridItems> GridListCache = new List<GridItems>();

In ASP.NET I am using a global static list to keep track of grid items.

Will this list remains specific to each user or will it be at the AppPool Level and shared across all users.

I definitely know for a fact that public static int flag = 0; will be shared across all users. But when i used the list declared above i have not noticed it begin shared across all users and being specific to each user but i wanted the experts to weigh in on this and let me know if putting this list in Session variable is the right way to accomplish this.

Thanks in advance.

Edit:

Thanks for your responses I am now using the following:

System.Web.HttpContext.Current.Cache.Add("GridListCache", GridItems, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);

Things seem to be working fine. Any comments on the settings that i am using for the cached value?

Thanks once again for your useful comments.

"Per-user" information should be stored in Session state or custom storage that allows to pick data based on user identity.

Note that caching large amount of data in session state may actually decrease performance is it consumes too much space (in-memory state) or need to be serialized on each request for out-of-process sessions state like SQL/state server.

Static fields are always shared across all requests in the same application (on same machine for multi-server cases). As such they should be used to store static data that does not depend on user identity like list of countries/states. Additionally since it is shared across all requests it may require synchronization (ie using lock ) if data structure is mutable (ie you can add more countries to list/dictionary).

It is often better to use .Net in Cache class instead static variables as it allows automatic expiration/refreshing of data and let you unload unused objects from memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM