Should I use Data Cache or Output Cache for a comp

2019-04-05 00:19发布

I am attempting to cache sections of a navigation menu according to different criteria.

For example, news and articles need to be refreshed on a duration basis, whereas login and profile stuff should be cached on a per user basis.

I'm considering 2 options - would anyone be kind enough to enlighten me about the pros / cons of each? And if possible suggest a better approach to take!

Option 1.

Simply cache all required html as strings in the Data Cache. Check for user differences manually when required.

I (perhaps incorrectly) imagine that this would be the most work to implement, but also the most efficient way of caching the different sections.

Option 2.

Have a NavigationController with different child action for each section of the menu. (We can apply a different outputCacheProfile on each child action as required.)

But this would require us to call a separate RenderAction for each section of the navigation menu. And I'm worried about this because of a comment on one of Phil Haack's blog posts:

[Render Action] is very similar to making another request since we need to run through routing to make sure we have the appropriate route data and context to call the action method. So each call to RenderAction is going to add up.

Full post here: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

1条回答
一纸荒年 Trace。
2楼-- · 2019-04-05 01:01

I think there's been a general avoidance of this question because there is no right answer here.

In fact, your choice of Architecture for implementing complex navigation will determine the best Caching strategy.


I'm deeply partial to Navigation through Partial Views with Child Actions.

I agree with you that referencing files is more work. I prefer to have database entries with navigation options grouped by keys and referenced by argument to child actions.

So your Navigation Table may look like this

grpId    Title        Path
1        Home Page    /
1        About Page   /Home/About
1        Reports Page /Reports
2        Home Page    /
2        Admin Page   /Admin
2        Reports Page /Reports

and your child action would take a grpId

[OutputCache(Duration = 60000, VaryByParam = "grpId")]
public PartialViewResult NavigationPage(int grpId)

could pull all your navigation group options and render a custom navigation menu. This form of output cache is customized by time (60000 secs and your param)


Conclusion:

I suspect I have told you nothing new, but have only confirmed what you were already leaning toward. The MVC framework is very robust and provides tools to handle nicely what you want to do. Using files and Data Cache is also a valid method, but it would be greater head-ache and work on your part to implement.

Keep in mind: Haacks's post is over 4 years old (MVC 2 Beta) . The framework and output cache has evolved nicely since then. You can now Cache Partials without worrying about caching the entire page. This recent reference to caching with MVC 4 doesn't speak directly to Phil's earlier concerns, but notably neglects them.

查看更多
登录 后发表回答