C# 缓存 vs 会话的优势

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/428634/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:59:26  来源:igfitidea点击:

Advantages of Cache vs Session

c#asp.netsessioncachingviewstate

提问by Xaisoft

What is the difference between storing a datatable in Session vs Cache? What are the advantages and disadvantages?

在会话和缓存中存储数据表有什么区别?有什么优点和缺点?

So, if it is a simple search page which returns result in a datatable and binds it to a gridview. If user 'a' searches and user 'b' searches, is it better to store it in Session since each user would most likely have different results or can I still store each of their searches in Cache or does that not make sense since there is only one cache. I guess basically what I am trying to say is that would the Cache be overwritten.

因此,如果它是一个简单的搜索页面,它在数据表中返回结果并将其绑定到 gridview。如果用户 'a' 搜索和用户 'b' 搜索,最好将它存储在 Session 中,因为每个用户很可能有不同的结果,或者我仍然可以将他们的每个搜索存储在缓存中还是没有意义,因为有只有一个缓存。我想基本上我想说的是缓存会被覆盖。

采纳答案by M4N

One important difference is, that items in the cache can expire (will be removed from cache) after a specified amount of time. Items put into a session will stay there, until the session ends.

一个重要的区别是,缓存中的项目可以在指定的时间后过期(将从缓存中删除)。放入会话的项目将保留在那里,直到会话结束。

ASP.NET can also remove items from cache when the amount of available memory gets small.

当可用内存量变小时,ASP.NET 还可以从缓存中删除项目。

Another difference: the session state can be kept external (state server, SQL server) and shared between several instances of your web app (for load balancing). This is not the case with the cache.

另一个区别:会话状态可以保持在外部(状态服务器、SQL 服务器)并在 Web 应用程序的多个实例之间共享(用于负载平衡)。缓存不是这种情况。

Besides of these differences (as others have noted): session is per user/session while cache is per application.

除了这些差异(正如其他人所指出的):会话是每个用户/会话,而缓存是每个应用程序。

回答by Martin Clarke

AFAIK, The key difference is session is per user, while cache will be for application scoped items.

AFAIK,关键区别在于会话是每个用户,而缓存将用于应用程序范围的项目。

As noted in the other answers you can store per user info in the cache, providing you provide a key (either by session or cookie). Then you'd have more control to expire items in the cache and also set dependencies on them. So if the DataTable in question is going to change on a regular basis, then caching is probably an appropriate option. Otherwise, if it is static session might be more appropriate. Steven Smith has an excellent video on caching at dnrtvthat is worth checking out.

正如其他答案中所述,您可以将每个用户的信息存储在缓存中,前提是您提供一个密钥(通过会话或 cookie)。然后,您将有更多的控制权来使缓存中的项目过期并设置对它们的依赖关系。因此,如果有问题的 DataTable 将定期更改,那么缓存可能是一个合适的选择。否则,如果是静态会话可能更合适。Steven Smith 在 dnrtv 上有一个关于缓存的优秀视频,值得一看。

It really depends on what you're trying to achieve, how much time you've got. There are some other alternatives to consider with respect to how you store state in an application. Depending on how large the table is, you could consider storing the state in a cookie (encrypted if it is sensitive information). Alternatively, if it's application scoped data you cold use a static field on a page or class. There is the Application object as well.

这真的取决于你想要达到什么目标,你有多少时间。关于如何在应用程序中存储状态,还有一些其他的替代方案需要考虑。根据表的大小,您可以考虑将状态存储在 cookie 中(如果是敏感信息则加密)。或者,如果它是应用程序范围的数据,您可以在页面或类上使用静态字段。还有 Application 对象。

Update: I think the key question you have to ask yourself, is who should see this data.

更新:我认为您必须问自己的关键问题是谁应该看到这些数据。

Are they going to access the data frequently?  

(No, don't bother).

(不,不要打扰)。

Is it going to change?  

(No, use a static field or Application).

(不,使用静态字段或应用程序)。

Is it acceptable for user a and user b to see the same results?  

(No, use the cache with a key comprising of the username and the search term.).
(Yes, use the cache using a key of the search term).

(不,使用包含用户名和搜索词的键的缓存。)。
(是的,使用搜索词的关键字使用缓存)。

Honestly though, if you're not far along in your development, I would consider parking the caching/state issue to a later date - you might not even need it.

老实说,如果您在开发中的进展不远,我会考虑将缓存/状态问题搁置到以后的日期 - 您甚至可能不需要它。

The first three rules of performance tuning are: 1. Measure, 2. Measure some more. 3. Measure again...

性能调优的前三个规则是:1. 测量,2. 再测量一些。3. 再次测量...

回答by Greg Ogle

Cache is in the Application scope with the purpose of reducing the number of times a piece of data is obtained. Session is in a user's session scope with the purpose of giving a particular user state.

Cache属于Application范围,目的是减少获取一条数据的次数。会话在用户的会话范围内,目的是提供特定的用户状态。

回答by Andrew Hare

Well it depends on how you have session configured for ASP.NET. Are you storing session in a database or in memory? If in memory are you using a separate server or are you using the current webserver for session?

好吧,这取决于您如何为 ASP.NET 配置会话。您是将会话存储在数据库中还是内存中?如果在内存中您使用的是单独的服务器还是使用当前的网络服务器进行会话?

Depending on how things are set up for you there may be performance implications when you are using something like a datatable which tells me that you are perhaps storing large amounts of data.

根据您的设置方式,当您使用数据表之类的东西时,可能会影响性能,该数据表告诉我您可能正在存储大量数据。

Also Session is stored per user and is retrieved per user by means of their Session ticket that is stored either in a Session cookie or on the URL if they do not accept cookies and you have set up ASP.NET to cookieless mode. Anything that you cache will be cached at the application level and will be available to all user sessions which may or may not be what you want.

此外,会话按用户存储,并通过存储在会话 cookie 或 URL 中的会话票证按用户检索,如果他们不接受 cookie 并且您已将 ASP.NET 设置为无 cookie 模式。您缓存的任何内容都将在应用程序级别缓存,并且可供所有用户会话使用,这些会话可能是您想要的,也可能不是。

回答by Greg

Session is per user, Cache is for the application.

会话是针对每个用户的,缓存是针对应用程序的。

Items in Cache can and will be removed automatically based upon expiration times (sliding or fixed) and memory constraints of the IIS worker process.

缓存中的项目可以并且将根据 IIS 工作进程的过期时间(滑动或固定)和内存限制自动删除。

So basically items in Cache are never guaranteed to exist but Session will stay there until the session ends.

所以基本上 Cache 中的项目永远不会保证存在,但 Session 将保持在那里直到会话结束。

Storing items on a per-user basis (via Session or a creative use of Cache) can lead to a lot of memory usage and should be considered carefully.

按用户存储项目(通过会话或创造性地使用缓存)可能会导致大量内存使用,应仔细考虑。

On top of all of this, if IIS resets the worker process, you can lose your Cache and Session.

最重要的是,如果 IIS 重置工作进程,您可能会丢失缓存和会话。

回答by StingyHyman

See this answer.

看到这个答案

Session can kill your app performance, unless you use some backend provider like memcached or velocity. Generally you should avoid it.

除非您使用某些后端提供程序,如 memcached 或 Velocity,否则会话可能会扼杀您的应用程序性能。一般来说,你应该避免它。

回答by Nabeel

Another important difference, Session State will be blockedif concurrent async Ajax requests are executed, it will effect performance

另一个重要的区别,如果并发异步 Ajax 请求被执行,Session State 会被阻塞,这会影响性能

回答by Usman Khair

As far as I know, it all depends on your needs.

据我所知,这一切都取决于您的需求。

Whenever you need to maintain state for the users, then you have to be very careful while using sessions. The default setting is 'InProc', which uses the individual server's memory, does not work well in Cloud-based applications. This may be applicable for those of you who host your application in multi-instance web-farm environments. Windows Azure load balancer uses round-robin allocation within connected nodes.

每当您需要为用户维护状态时,就必须在使用会话时非常小心。默认设置是“InProc”,它使用单个服务器的内存,在基于云的应用程序中不能很好地工作。这可能适用于在多实例 Web 场环境中托管应用程序的人。Windows Azure 负载平衡器在连接的节点内使用循环分配。

You have multiple options in the session storage. SQL Server can also be used as a storage of session state. Custom session techniques are available on azure like table storage provider etc.

您在会话存储中有多个选项。SQL Server 还可以用作会话状态的存储。自定义会话技术可在 azure 上使用,例如表存储提供程序等。

The Cache is also stored on the server's memory, but it doesn't have a concern with the users. Any user within the same pool can access the application cache data. In short, on the cloud, we need to use Caching service provided by the cloud providers. Azure provides Windows Azure distributed caching service.

Cache 也存储在服务器的内存中,但它与用户无关。同一池中的任何用户都可以访问应用程序缓存数据。总之,在云上,我们需要使用云提供商提供的缓存服务。Azure 提供 Windows Azure 分布式缓存服务。

As a matter of fact, developers do not care about the impact of state management techniques when applying the technique in the applications. It

事实上,开发人员在应用程序中应用状态管理技术时并不关心状态管理技术的影响。它

"If your client doesn't have cloud support, then you don't worry about the cloud scenarios"

“如果您的客户没有云支持,那么您就不必担心云场景”