简体   繁体   English

我应该将数据缓存或输出缓存用于复杂的导航菜单吗?

[英]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. 选项1。

Simply cache all required html as strings in the Data Cache. 只需将所有必需的html缓存为数据缓存中的字符串。 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. 选项2。

Have a NavigationController with different child action for each section of the menu. 为菜单的每个部分安装一个具有不同子操作的NavigationController。 (We can apply a different outputCacheProfile on each child action as required.) (我们可以根据需要对每个子操作应用不同的outputCacheProfile。)

But this would require us to call a separate RenderAction for each section of the navigation menu. 但这需要我们为导航菜单的每个部分调用单独的RenderAction。 And I'm worried about this because of a comment on one of Phil Haack's blog posts: 由于对Phil Haack的一篇博文发表评论,我对此感到担心:

[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. 所以每次调用RenderAction都会加起来。

Full post here: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx 完整帖子在这里: http//haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

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) 这种形式的输出缓存是按时间(60000秒和您的参数)自定义的


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. MVC框架非常强大,并提供了很好地处理您想要做的工具。 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 ) . 请记住: Haacks的帖子已超过4年( 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. 最近对MVC 4缓存的引用并没有直接反映Phil先前的担忧,但显然忽略了它们。

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

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