C# HttpRuntime.Cache[] 与应用程序[]

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

HttpRuntime.Cache[] vs Application[]

c#asp.netcachinghttpruntime.cache

提问by Jon Tackabury

I know that most people recommend using HttpRuntime.Cache because it has more flexibility... etc. But what if you want the object to persist in the cache for the life of the application? Is there any big downside to using the Application[] object to cache things?

我知道大多数人推荐使用 HttpRuntime.Cache 因为它具有更大的灵活性……等等。但是如果您希望对象在应用程序的生命周期中一直保留在缓存中怎么办?使用 Application[] 对象缓存东西有什么大的缺点吗?

采纳答案by Tom Jelen

As long as you don't abuse the application state, then I don't see a problem in using it for items that you don't want to expire. Alternatively I would probably use a static variable near the code that uses it. That way you avoid to go through HttpApplicationStateand then be forced to have a reference to System.Web if i want to access my data.

只要您不滥用应用程序状态,那么我认为将它用于您不想过期的项目是没有问题的。或者,我可能会在使用它的代码附近使用一个静态变量。这样HttpApplicationState,如果我想访问我的数据,您就可以避免通过然后被迫引用 System.Web。

But be sure to think through how you use the object(s) that you store in HttpApplicationState. If it's a DataSetwhich you keep adding stuff to for each request, then at some point you end up eating up too much memory on the web-server. The same could happen if you keep adding items to HttpApplicationStatewhen you process requests, at some point you will force the application to restart.

但一定要仔细考虑如何使用存储在HttpApplicationState. 如果DataSet您不断为每个请求添加内容,那么在某些时候您最终会在网络服务器上消耗太多内存。如果您HttpApplicationState在处理请求时不断添加项目,则可能会发生同样的情况,在某些时候您将强制应用程序重新启动。

That's probably the advantage of using Cache in your situation. Consuming larger amounts memory isn't as fatal because you allow ASP.NET to release the items in your cache when memory becomes scarce.

这可能是在您的情况下使用 Cache 的优势。消耗大量内存并不那么致命,因为您允许 ASP.NET 在内存不足时释放缓存中的项目。

回答by Vilx-

Use cache when you want items to automatically expire or get reclaimed when memory is scarse. Otherwise use static variables if you can, because they will yield better performance then digging through the ApplicationState collection. I'm not exactly sure what would be the case when to use ApplicationState, but there are sure to be some.

当您希望项目在内存不足时自动过期或回收时,请使用缓存。否则,如果可以,请使用静态变量,因为与挖掘 ApplicationState 集合相比,它们会产生更好的性能。我不确定何时使用 ApplicationState 会是什么情况,但肯定会有一些。

回答by ssmith

Application is deprecated by Cache. If you need something with application scope, then you should either create it as a static member of a class or use the Cache. If you want to go the Cache route but don't ever want it to expire, you should use the CacheItemPriority.NotRemovable option when you Insert the value into the cache. Note that it is possible to use this priority and still use cache dependencies, for instance if your data depended on something in the file system. All the CacheItemPriority does is prevent the HttpRuntime.Cache from intelligently clearing the item when it feels memory pressure and uses its Least-Recently-Used algorithm to purge items that aren't seeing much use.

应用程序已被缓存弃用。如果您需要具有应用程序范围的东西,那么您应该将其创建为类的静态成员或使用 Cache。如果您想使用 Cache 路由但不希望它过期,则应在将值插入缓存时使用 CacheItemPriority.NotRemovable 选项。请注意,可以使用此优先级并仍然使用缓存依赖项,例如,如果您的数据依赖于文件系统中的某些内容。CacheItemPriority 所做的就是阻止 HttpRuntime.Cache 在感觉到内存压力时智能地清除该项目,并使用其最近最少使用的算法来清除不怎么使用的项目。