C# System.Web.Caching 还是 System.Runtime.Caching 更适合 .NET 4 Web 应用程序

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

Is System.Web.Caching or System.Runtime.Caching preferable for a .NET 4 web application

c#asp.netcaching.net-4.0

提问by Tim Goodman

I am adding caching to an ASP.NET web application. This is .NET 4, so I can use the classes in the System.Runtime.Caching namespace (which, as I understand it, was added to provide similar functionality to that found in System.Web.Caching, but for non-Web-apps.)

我正在向 ASP.NET Web 应用程序添加缓存。这是 .NET 4,所以我可以使用 System.Runtime.Caching 命名空间中的类(据我所知,添加它是为了提供与 System.Web.Caching 中的功能类似的功能,但对于非 Web-应用。)

But since this isa web app, am I better off using System.Web.Caching? Or is the newer System.Runtime.Caching superior in some way?

但既然这一个网络应用程序,我最好使用 System.Web.Caching 吗?还是较新的 System.Runtime.Caching 在某些方面更胜一筹?

采纳答案by dotnetster

Microsoft recommends using System.Runtime.Caching for all caching purposes. See this: http://msdn.microsoft.com/en-us/library/dd997357.aspx

Microsoft 建议将 System.Runtime.Caching 用于所有缓存目的。请参阅:http: //msdn.microsoft.com/en-us/library/dd997357.aspx

Although, I have come across a couple threads where people are having issues with the MemoryCache.Default instance. After a while, it stops working properly. Any item you add using the Add or Set method does not actually get added to the cache. I tried the same and was able to reproduce this issue with explicitly calling MemoryCache.Default.Dispose() method.

虽然,我遇到了几个线程,其中人们对 MemoryCache.Default 实例有问题。一段时间后,它停止正常工作。您使用 Add 或 Set 方法添加的任何项目实际上都不会添加到缓存中。我尝试了相同的方法,并且能够通过显式调用 MemoryCache.Default.Dispose() 方法来重现此问题。

Here are the links: MemoryCache Empty : Returns null after being set

以下是链接: MemoryCache Empty :设置后返回 null

http://forums.asp.net/t/1736572.aspx/1

http://forums.asp.net/t/1736572.aspx/1

My recommendation is to use the System.Web.Caching (HttpContext.Current.Cache)

我的建议是使用 System.Web.Caching (HttpContext.Current.Cache)

UPDATE:

更新:

This issue has been fixed by MS. Check the accepted answer in the post below: Runtime Cache Issue Resolved

MS 已修复此问题。检查下面帖子中接受的答案: 运行时缓存问题已解决

回答by Brad Ruderman

System.Runtime.Caching allows you to cache across all .Net apps, not just the IIS worker process. So if you have a requirement that will access the cache in multiple scenarios, use System.Runtime. Also you can check this cache adapter which allows you to swap between runtime, web, and app fabric caching. https://bitbucket.org/glav/cacheadapter

System.Runtime.Caching 允许您缓存所有 .Net 应用程序,而不仅仅是 IIS 工作进程。因此,如果您需要在多个场景中访问缓存,请使用 System.Runtime。您还可以检查此缓存适配器,它允许您在运行时、Web 和应用程序结构缓存之间进行交换。https://bitbucket.org/glav/cacheadapter

One more thing, if you have a multi-server farm, with a load balanced configuration make sure you have sticky-sessions or a distributed cache model.

还有一件事,如果你有一个多服务器群,负载平衡配置确保你有粘性会话或分布式缓存模型。