简体   繁体   English

在会话中存储值的最佳实践

[英]Best practice to store values in session

I am storing list of customers in session. 我在会话中存储客户列表。 When I reset, I wanted to know what happens to the previous cached listed, will it be destroyed automatically. 当我重置时,我想知道上面列出的缓存会发生什么,它会自动销毁。 Since I no longer need it, is it possible to destroy it completely and store a new value. 由于我不再需要它,是否可以完全销毁它并存储新值。

public IList<Customer> CachedCustomers
{
  set 
  { 
     HttpContext.Current.Session["Customers"] = null;//<-- Is this line required
     HttpContext.Current.Session["Customers"] = value; 
  }
}

No, setting to null is not needed. 不,不需要设置为null This is no different than any variable or field in .NET. 这与.NET中的任何变量或字段没有什么不同。 Setting to value x , then setting to value y is the same as setting to value y . 设置为值x ,然后设置为值y与设置为值y相同。


Of course, this would not apply to a property, which could run code on every set, and could do something different between setting to null and setting to something else. 当然,这不适用于可以在每个集合上运行代码的属性,并且可以在设置为null和设置为其他内容之间执行不同的操作。

For the Add session: 对于添加会话:

 HttpContext.Current.Session["Customers"] = value; 

For Remove the Session: 对于删除会话:

 HttpContext.Current.Session.Contents.Remove("Customers") 

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

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