Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use Data Cache or Output Cache for a complex navigation menu?

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

like image 855
Martin Hansen Lennox Avatar asked Nov 23 '13 19:11

Martin Hansen Lennox


People also ask

When should you not use cache?

Three caching challenges to consider Caches take up space on the disk, so we have to assess whether the time we are saving is worth the amount of disk space used. Cached data might not be the most accurate, particularly for volatile real-time data. Therefore, volatile data should not be cached.

What data should be cached?

In-memory data lookup: If you have a mobile / web app front end you might want to cache some information like user profile, some historical / static data, or some api response according to your use cases. Caching will help in storing such data.

What is output caching?

Output caching stores the generated output of pages, controls, and HTTP responses in memory. Output caching enables you to cache different versions of content depending on the query string and on form-post parameters to a page, on browser type, or on the language of the user.

What happens when private and shared items are cached?

A public, or “shared” cache is used by more than one client. As such, it gives a greater performance gain and a much greater scalability gain, as a user may receive cached copies of representations without ever having obtained a copy directly from the origin server.


1 Answers

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.

like image 179
Dave Alperovich Avatar answered Sep 28 '22 07:09

Dave Alperovich