简体   繁体   English

ASP.Net中的数据缓存与会话对象

[英]Data cache vs session object in ASP.Net

Should dynamic business objects for a site be stored in the users session or use ASP.Net caching (objects such as orders, profile information etc)? 站点的动态业务对象是应该存储在用户会话中还是使用ASP.Net缓存(诸如订单,配置文件信息等对象)?

I have worked with sites that used sessions to store business objects, but I was wondering...What are the advantages or disadvantages of caching? 我曾经使用会话来存储业务对象的网站,但我想知道......缓存有哪些优点或缺点?

If the objects are shareable between user sessions, then use the cache. 如果对象在用户会话之间可共享,则使用缓存。 If the objects are unique to each session -- perhaps because they are governed by permissions -- then store it in the session. 如果对象对每个会话都是唯一的 - 可能是因为它们受权限控制 - 然后将其存储在会话中。 The in-process session itself is stored in the cache so the deciding factor really should be the scope of the data. 进程内会话本身存储在缓存中,因此决定因素确实应该是数据的范围。

Caching is just that -- caching. 缓存只是 - 缓存。 You can never rely on entries being there, so no assumptions must be made in that respect: be prepared to go straight to the DB (or wherever else) to refetch data. 您永远不能依赖于那里的条目,因此不必在这方面做出任何假设:准备直接进入DB(或其他任何地方)来重新获取数据。

Session, on the other hand, is more suited towards storing objects, though personally I try to avoid session store in favour of a DB. 另一方面,会话更适合存储对象,但我个人试图避免使用会话存储来支持数据库。 I usually do that by abstracting away the store behind an opaque ISessionStoreService interface: 我通常通过抽象不透明的ISessionStoreService接口后面的存储来做到这一点:

interface ISessionStore
{
    T GetEntry<T>(string key);
    void SaveEntry<T>(string key, T entry);
}

and then "dependency-injecting" appropriate implementation, be it InmemorySessionStore , DbSessionStore or whatever. 然后“依赖注入”适当的实现,无论是InmemorySessionStoreDbSessionStore还是其他什么。

The ASP.NET system cache is global to the application where as the session is unique to the current user. ASP.NET系统缓存对于应用程序是全局的,因为会话对于当前用户是唯一的。 If you chose to use the global cache to store objects you would need to create an object identification strategy so you could get the correct objects for your per user basis. 如果您选择使用全局缓存来存储对象,则需要创建对象标识策略,以便为每个用户获取正确的对象。

If you are looking to enhance performance you would be better off replacing the ASP.NET session state with a distributed memory cache such as Microsoft's velocity. 如果您希望提高性能,最好使用分布式内存缓存(如Microsoft的速度)替换ASP.NET会话状态。 Microsoft has posted articles on how to replace session usage to target Velocity. Microsoft发布了有关如何将会话使用替换为目标Velocity的文章。 You could also use Memcache or other similar products in a related fashion. 您也可以以相关方式使用Memcache或其他类似产品。

Session objects are suitable for user-only-data, on the other hand, Cache objects are more suitable for data shared for the application. 会话对象适用于仅用户数据,另一方面,Cache对象更适合于为应用程序共享的数据。
The key to save in one or the other is to determine if what are you trying to store will be user-only data or it needs to be shared in all the application. 保存在一个或另一个中的关键是确定您尝试存储的内容是仅用户数据还是需要在所有应用程序中共享。

Session => A webpage with a step by step interface (an online test for example). Session =>具有分步界面的网页(例如在线测试)。
Cache => The info displayed in some kind of weather widget (like the one that google has in its igoogle.com page). 缓存=>在某种天气小部件中显示的信息(如谷歌在其igoogle.com页面中的那个)。
Hope this helps. 希望这可以帮助。

Although you can store your business object in Cache, but Cache is designed for performance improvement not state management. 虽然您可以将业务对象存储在Cache中,但Cache旨在提高性能而不是状态管理。 Imagine you that you have a process of getting 1000 record from database (and it take about 3 seconds) and you will need it for a few minutes. 想象一下,你有一个从数据库中获取1000条记录的过程(大约需要3秒钟),你需要它几分钟。 You can store your objects in Cache and set expire date, priority and dependency to it (like SqlDependency or FileDependency), so for next requests you can use Cached data instead of retriving it from database. 您可以将对象存储在Cache中并设置过期日期,优先级和依赖关系(如SqlDependency或FileDependency),因此对于下一个请求,您可以使用缓存数据而不是从数据库中重新获取它。 You can store your object in Session but you can not set dependency for Session by default. 您可以将对象存储在Session中,但默认情况下不能为Session设置依赖关系。 Also Cache has a unique behavior that when system needs memory it will release objects from cache depending on its priority. 此外,Cache具有一种独特的行为,当系统需要内存时,它将根据其优先级从缓存中释放对象。 Cache objects are global to Application and shared between all users but Session is not shared and it's usable for each user (Session). 缓存对象对应用程序是全局的,并在所有用户之间共享,但Session不是共享的,并且它可用于每个用户(会话)。

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

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