C# ASP.Net 中的数据缓存与会话对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/492043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Data cache vs session object in ASP.Net
提问by Miyagi Coder
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?
我曾与使用会话存储业务对象的站点合作过,但我想知道......缓存的优点或缺点是什么?
采纳答案by tvanfosson
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.
如果对象可在用户会话之间共享,则使用缓存。如果对象对于每个会话都是唯一的——也许是因为它们受权限管理——那么将它存储在会话中。进程内会话本身存储在缓存中,因此决定因素实际上应该是数据的范围。
回答by Chris Marisic
The ASP.NET system cache is global to the application where as the session is unique to the current user. 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.
ASP.NET 系统缓存对于应用程序是全局的,因为会话对于当前用户是唯一的。如果您选择使用全局缓存来存储对象,您将需要创建一个对象识别策略,以便您可以为每个用户获取正确的对象。
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 has posted articles on how to replace session usage to target Velocity. You could also use Memcache or other similar products in a related fashion.
如果您希望提高性能,最好将 ASP.NET 会话状态替换为分布式内存缓存,例如 Microsoft 的速度。Microsoft 已发布有关如何将会话使用替换为目标 Velocity 的文章。您还可以以相关方式使用 Memcache 或其他类似产品。
回答by Jaime Febres
Session objects are suitable for user-only-data, on the other hand, Cache objects are more suitable for data shared for the application.
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 对象适用于 user-only 数据,而 Cache 对象更适用于应用程序共享的数据。
保存在一个或另一个中的关键是确定您要存储的内容是仅用户数据还是需要在所有应用程序中共享。
Session => A webpage with a step by step interface (an online test for example).
Cache => The info displayed in some kind of weather widget (like the one that google has in its igoogle.com page).
Hope this helps.
会话 => 带有分步界面的网页(例如在线测试)。
缓存 => 显示在某种天气小部件中的信息(就像谷歌在其 igoogle.com 页面上的那个)。
希望这可以帮助。
回答by Anton Gogolev
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.
缓存就是——缓存。你永远不能依赖那里的条目,所以在这方面不必做任何假设:准备直接去数据库(或其他任何地方)重新获取数据。
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 ISessionStoreServiceinterface:
另一方面,会话更适合存储对象,尽管我个人尽量避免会话存储以支持数据库。我通常通过在不透明的ISessionStoreService接口后面抽象出存储来做到这一点:
interface ISessionStore
{
T GetEntry<T>(string key);
void SaveEntry<T>(string key, T entry);
}
and then "dependency-injecting" appropriate implementation, be it InmemorySessionStore, DbSessionStoreor whatever.
然后“依赖注入”适当的实现,无论是InmemorySessionStore、DbSessionStore还是其他什么。
回答by Masoud Tabatabaei
Although you can store your business object in Cache, but Cache is designed for performance improvement not state management. 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. 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. You can store your object in Session but you can not set dependency for Session by default. Also Cache has a unique behavior that when system needs memory it will release objects from cache depending on its priority. Cache objects are global to Application and shared between all users but Session is not shared and it's usable for each user (Session).
虽然您可以将您的业务对象存储在 Cache 中,但 Cache 旨在提高性能而不是状态管理。想象一下,您有一个从数据库中获取 1000 条记录的过程(大约需要 3 秒),并且您需要几分钟的时间。您可以将对象存储在 Cache 中并为其设置过期日期、优先级和依赖项(如 SqlDependency 或 FileDependency),因此对于下一个请求,您可以使用缓存数据而不是从数据库中检索它。您可以将对象存储在 Session 中,但默认情况下不能为 Session 设置依赖项。缓存还有一个独特的行为,当系统需要内存时,它会根据其优先级从缓存中释放对象。缓存对象对 Application 是全局的,并在所有用户之间共享,但 Session 不是共享的,它可用于每个用户(会话)。