简体   繁体   English

缓存策略,输出缓存与数据缓存还是两者兼而有之?

[英]Caching strategy, Output Cache vs Data Cache or both?

I'm working on an ASP.NET MVC project and I've come to the point where I want to start considering my caching strategy. 我正在开发一个ASP.NET MVC项目,我已经开始考虑我的缓存策略了。 I've tried to leave my framework as open as possible for the use in caching. 我试图让我的框架尽可能开放用于缓存。

From what I heard during Scott Hanselman's podcast StackOverflow.com uses page output caching and zips that content and puts it into RAM. 根据我在Scott Hanselman播客期间听到的内容,StackOverflow.com使用页面输出缓存并将内容拉链并将其放入RAM中。 This sounds like this would be great for user-wide cache but for something like personalized pages you would have to cache a version for each user and that could get out of control very quickly. 这听起来像是对用户范围的缓存非常有用,但对于像个性化页面这样的东西,你必须为每个用户缓存一个版本,并且可能很快失控。

So, for a caching strategy. 所以,对于缓存策略。 Which should be used, Output Caching, Data Caching or combined? 哪个应该使用,输出缓存,数据缓存还是组合? My first thoughts are both but as far as cache dependencies it sounds like it could get a bit complex. 我的第一个想法是两者,但就缓存依赖性而言,它听起来有点复杂。

We're doing API and Output caching on a large scale (3 milion visits a day) web site (news portal). 我们正在大规模(每天访问300万次)网站(新闻门户网站)进行API和输出缓存。 The site is primarily used by anonymous users, but we do have authenticated users and we cache a complete site just for them, due to some personalized parts of the site, and I must admit that we had absolutely no problems with memory pressure. 该网站主要由匿名用户使用,但我们确实有经过身份验证的用户,我们只为他们缓存一个完整的网站,因为该网站的一些个性化部分,我必须承认我们绝对没有内存压力问题。

So, my advice would be cache everything you can in API cache so your Output cache rebuilding is even faster. 因此,我的建议是在API缓存中缓存所有内容,以便更快地进行输出缓存重建。

Of course, pay close attention to your cache ratio values in the performance counters. 当然,请密切关注性能计数器中的缓存比值。 You should see numbers >95% of cached hits. 您应该看到数量> 95%的缓存命中数。

Another thing to pay attention is cache invalidation, this is a big issue if you have a lot of related content. 要注意的另一件事是缓存失效,如果你有很多相关内容,这是一个大问题。 For example, you cache music stuff and information about one album or song might be displayed and cached on few hundred pages. 例如,您可以缓存音乐内容,并且可以在几百页上显示和缓存有关一个专辑或歌曲的信息。 If anything changes in that song, you have to invalidate all of these pages which can be problematic. 如果该歌曲有任何变化,您必须使所有这些页面无效,这可能会有问题。

Bottom line, caching is one of the best features of ASP.NET, it's done superbly and you can rely on it. 最重要的是,缓存是ASP.NET的最佳功能之一,它完美无缺,你可以依赖它。

Be careful about over-aggressive caching. 过度激进的缓存要小心。 Although caching is a tool for helping performance, when used incorrectly, it can actually make performance worse. 虽然缓存是一种帮助提高性能的工具,但如果使用不当,实际上可能会使性能变差。

I can't answer whether output caching or data caching would work for you better without knowing more details about your project. 如果不了解有关项目的更多详细信息,我无法回答输出缓存或数据缓存是否会对您更有效。 I can help provide a couple examples of when to use one over another. 我可以帮助提供几个何时使用另一个的例子。

If you have a specific data set which you would use often in many different views, you'd be better off using data caching. 如果您有一个特定的数据集,您可以在许多不同的视图中使用这些数据集,那么最好使用数据缓存。 You'd use this if your data fetch operation was very common and expensive relative to your data rendering. 如果您的数据提取操作相对于数据呈现非常常见且昂贵,则可以使用此方法。 If you had multiple views which used the same data, you would save your data fetching time. 如果您有多个使用相同数据的视图,则可以节省数据提取时间。

If you had a view which used a very specific data set and the rendering of the view was complicated and this view was requested very often (for example, stack overflow's home page), then you would benefit a lot from output caching. 如果您有一个使用非常特定数据集的视图并且视图的呈现很复杂并且经常请求此视图(例如,堆栈溢出的主页),那么您将从输出缓存中受益匪浅。

So in the end, it really depends on your needs and be careful about using caching incorrectly. 所以最后,它确实取决于您的需求,并且小心使用缓存不正确。

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

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