简体   繁体   English

在ASP.NET中使用静态列表与静态变量

[英]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. 在ASP.NET中,我使用全局静态列表来跟踪网格项目。

Will this list remains specific to each user or will it be at the AppPool Level and shared across all users. 该列表将仍然是特定于每个用户的列表,还是处于AppPool级别并在所有用户之间共享的列表。

I definitely know for a fact that public static int flag = 0; 我绝对知道, 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. 但是,当我使用上面声明的列表时,我没有注意到它开始在所有用户之间共享并且特定于每个用户,但是我希望专家对此进行权衡,并让我知道是否将此列表放入Session变量中是正确的方法完成这个。

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. 请注意,以会话状态缓存大量数据实际上可能会降低性能,因为它会占用过多的空间(内存中状态),或者需要在每个进程外会话状态(如SQL /状态服务器)的请求上进行序列化。

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). 另外,由于它在所有请求中共享,因此如果数据结构可变(例如,您可以将更多国家/地区添加到列表/词典),则可能需要同步(即使用lock )。

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. 通常最好在Cache类中使用.Net而不是静态变量,因为它可以自动使数据过期/刷新,并允许您从内存中卸载未使用的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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