简体   繁体   English

在asp.net应用程序中缓存用户数据

[英]Caching user data in asp.net application

What's the best way to cache web site user data in asp.net 4.0? 在asp.net 4.0中缓存网站用户数据的最佳方法是什么?

I have a table of user settings that track all kinds of user or session specific stuff like the state of UI elements (open/closed), preferences, whether some dialog has been dismissed, and so on. 我有一个用户设置表,该表可以跟踪各种用户或特定于会话的内容,例如UI元素的状态(打开/关闭),首选项,是否关闭了某些对话框等等。 Since these don't change very often (for each user, anyway) but are looked up frequently it seems sensible to cache them. 由于这些更改不是很频繁(无论如何对于每个用户),而是经常查找,因此缓存它们似乎是明智的。 What's the best way? 最好的方法是什么? These are the options I've identified... 这些是我确定的选项...

  1. Store them in HttpContext.Current.Session directly (eg Session["setting_name"] ) 将它们直接存储在HttpContext.Current.Session (例如Session["setting_name"]
  2. Store them in HttpContext.Current.Cache 将它们存储在HttpContext.Current.Cache
  3. Use a global static dictionary, eg static ConcurrentDictionary<string,string> where the key is a unique userID + setting name value 使用全局静态字典,例如static ConcurrentDictionary<string,string> ,其中键是唯一的userID +设置名称值
  4. Store a dictionary object for each session in Session or Cache 将每个会话的字典对象存储在SessionCache

What's the most sensible way to do this? 最明智的方法是什么? How does Session differ from Cache from a practical standpoint? 从实际的角度来看, SessionCache有何不同? Would it ever make sense to store a dictionary as a single session/cache object versus just adding lots of values directly? 将字典存储为单个会话/缓存对象而不是直接添加大量值是否有意义? I would think lookups might be faster, but updates would be slower since I'd have to re-store the entire dictionary when it changed. 我认为查找可能会更快,但更新会更慢,因为当更改字典时,我必须重新存储整个字典。

What problems or benefits might there be to using a global static cache? 使用全局静态缓存可能会有什么问题或好处? Seems like this would be the fastest, but I'd have to manage the size. 看来这将是最快的,但我必须控制大小。 I could just flush it periodically if it hits a certain size, or keep a cross reference queue and remove things oldest first when it gets to a certain size. 如果它达到某个大小,我可以定期刷新它,或者保留一个交叉引用队列,并在达到一定大小时首先删除最旧的东西。 Does this make any sense or is it just trying too hard? 这有意义还是只是太努力了?

Session may end up being stored out-of-process or in a database, which can make retrieving it expensive. Session可能最终会存储在进程外或数据库中,这可能会使检索变得昂贵。 You would likely be using a session database if your application is to be hosted in a server farm, as opposed to a single server. 如果您的应用程序要托管在服务器场(而不是单个服务器)中,则可能会使用会话数据库。 A server farm provides improved scalability and reliability, and it's often a common deployment scenario. 服务器场提供了改进的可伸缩性和可靠性,并且通常是常见的部署方案。 Have you thought about that? 你有想过吗

Also, when you use Session not in-process, it ends up getting serialized to be sent out-of-process or to a database, and deserialized when retrieved, and you are effectively doing what you describe above: 同样,当您使用非进程Session时,它最终会被序列化以发送到进程外或数据库中,并在检索时进行反序列化 ,从而有效地完成了上述操作:

... updates would be slower since I'd have to re-store the entire dictionary when it changed. ... 更新会比较慢,因为更改后我必须重新存储整个字典。 ... ...

.. since, even if you use individual session keys, the entire Session object for a user is serialized and deserialized together (all at once). ..因为,即使您使用单独的会话密钥,用户的整个Session对象也会一起序列化和反序列化(一次全部)。

Whereas, Cache would be in memory on a particular server in the farm, and therefore much more efficient than going out of process or to the database. Cache位于服务器场中特定服务器上的内存中,因此比进行处理或进入数据库要高效得多。 However, something in cache on one server might not be in cache on another. 但是,一台服务器上的缓存中的内容可能不在另一台服务器上的缓存中。 So if a user's subsequent request is directed to another server in the farm, the cache on that server might not yet hold any of the user's items. 因此,如果用户的后续请求被定向到服务器场中的另一台服务器,则该服务器上的缓存可能尚未保存该用户的任何项目。

Nevertheless, I'd suggest you use Cache if you're caching for performance reasons. 不过,如果出于性能原因进行缓存,建议您使用Cache

ps Yes, you're trying too hard. ps是的,您正在努力。 Don't reinvent the wheel unless you really need to. 除非确实需要,否则不要重新发明轮子。 :-) :-)

将您的信息放入memcached以实现可伸缩性可能更好

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

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