简体   繁体   English

ASP.NET MVC中的缓存和会话之间的访问差异

[英]Access differences between cache and session in ASP.NET MVC

I'm writing an ASP.NET MVC5 Application, I know that the actions where session["foo"] = bar are ran sequentially, now to avoid this, i want to store some informations into a MemoryCache object and not in session, but my doubt is: Is the cache managed like the session? 我正在编写一个ASP.NET MVC5应用程序,我知道session["foo"] = bar是顺序运行的,现在要避免这种情况,我想将一些信息存储到MemoryCache对象中,而不是在会话中,但是我的疑问是:是否像会话一样管理缓存? So the actions where i put ObjectCache.Set("foo", bar, null) are ran sequentially like for session? 所以我放ObjectCache.Set("foo", bar, null)是像会话一样顺序运行的? I know the scope difference between cache and session but for me and in this case it's not important. 我知道缓存和会话之间的范围差异,但是对我而言,在这种情况下,这并不重要。

Thank to everyone 谢谢大家

I understand that you try to avoid the session lock on the page. 我了解您尝试避免会话锁定在页面上。

The cache is not lock the full page access so the answer is that the cache is not run sequentially. 缓存未锁定完整页面访问,因此答案是缓存未按顺序运行。

There are two kind of cache, one in memory that use static dictionary to keep the data and one that save the cache on database, that use files to save the data. 缓存有两种,一种是在内存中使用静态字典来保存数据,另一种是将缓存保存在数据库上,它们使用文件来保存数据。 Both of them locks the data only for the period of read/write , while the session is lock the full access on the page from start to the end of it . 两者都只在读/写期间锁定数据 ,而会话则锁定页面从头到尾的完全访问权限

So use cache, but close the session on the page you have this issue. 因此,请使用缓存,但是在您遇到此问题的页面上关闭会话。 Also have in mind that if you use web garden then the cache on memory can have multiple different data because memory cache have its own static space on each pool. 还请记住,如果您使用Web Garden,则内存上的缓存可以具有多个不同的数据,因为内存缓存在每个池上都有其自己的静态空间。

Also the session is different for each user, the cache is the same for all users. 每个用户的会话也不同,所有用户的缓存均相同。

some more to read : ASP.NET Server does not process pages asynchronously 更多阅读内容: ASP.NET Server不会异步处理页面

I think the term you are looking for is thread safety - especially around concurrent access, typically writing. 我认为您正在寻找的术语是线程安全性-尤其是在并发访问(通常是编写)方面。

Seems that according to MSDN, System.Runtime.Caching.MemoryCache is indeed thread safe. 似乎根据MSDN, System.Runtime.Caching.MemoryCache确实是线程安全的。 See also: Is MemoryCache.Set() thread-safe? 另请参阅: MemoryCache.Set()线程安全吗?

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

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