带有缓存的 ASP.Net MVC 数据库驱动菜单 [英] ASP.Net MVC Database-driven menu with caching

查看:19
本文介绍了带有缓存的 ASP.Net MVC 数据库驱动菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的网站创建菜单.需要满足以下要求

I am trying to create a menu for my website. It needs to meet the following requirements

  • 它必须是数据库驱动的,从数据库中提取数据来构建菜单结构
  • 需要缓存从数据库中提取的数据 - 我不想为每个页面请求访问数据库

目前,我有一个简单的例子在运行,但我不知道如何集成缓存.我想我可能不得不重新设计我做这件事的整个方式.这是:

At the moment, I have a simplistic example running, but I don't know how to integrate caching. I think I might have to rework the entire way I do this. Here it is:

我有一个 ProductMenuAttribute,它从数据库中提取数据,并将其存储在 ViewData 中:

I have a ProductMenuAttribute, which pulls the data from the DB, and stores it in the ViewData:

public class ProductMenuAttribute: FilterAttribute, IActionFilter
{
    #region IActionFilter Members

    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext != null)
        {
            var context = filterContext.Result as ViewResult;
            if (context != null)
            {
                ProductsRepository repository = new ProductsRepository(Properties.Settings.Default.SqlConnectionString);

                context.ViewData.Add("ProductsList", repository.GetAllProductRanges());
            }
        }
    }

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {

    }

    #endregion
}

在我的 Site.master 中,我从 ViewData 中提取数据并使用它来呈现我的菜单.这是我的无序菜单列表中的一个小片段,它使用 CSS 设置样式.代码如下:

In my Site.master I pull the data from the ViewData and use it to render my menu. This is a small snippet in my un-ordered menu list, which gets styled using CSS. Here is the code:

            <li>
                <%= Html.ActionLink("Products", "Index", "Products")%>

                <%  IQueryable<ProductRange> productRanges = ViewData["ProductsList"] as IQueryable<ProductRange>; %>

                    <% if (productRanges != null)
                       { %>

                    <ul>
                        <% foreach (ProductRange range in productRanges) 
                           { %>   
                            <li><%= Html.ActionLink(range.Name, "RangeDetails", "Products", new { id = range.ID }, null)%></li>
                        <% } %>
                    </ul>

                    <% } %>
            </li>

然后我用 [ProductMenu] 属性装饰每个控制器,如下所示:

I then decorate each controller with the [ProductMenu] attribuate as follows:

[ProductMenu]
public class HomeController : BaseController
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult About()
    {
        return View();
    }
}

现在,每当我的控制器上的任何操作被点击时,ProductMenuAttribute 类中的 OnActionExecuted 方法将被调用,该方法将设置 ViewData,最终将在我的 Site.Master 上使用以从数据库构建我的菜单,这时候我会调用任何操作.

Now whenever any of the actions on my controller are hit, the OnActionExecuted method in the ProductMenuAttribute class will be called, which will set the ViewData, which will eventually be used on my Site.Master to build up my menu from the DB, which time I call any of the actions.

现在的问题是 - 我如何在这种情况下添加缓存??我不知道从哪里开始,并且感觉我的解决方案不可缓存.

The problem now is - how do I add caching to this scenario?? I have no clue where to start, and have a feeling the solution I have is not cacheable.

推荐答案

我想我真正想要的是使用来自 MVC Futures 项目的 Html.RenderAction() 辅助扩展.

I think what I am really looking for is to use the Html.RenderAction() helper extension from the MVC Futures project.

这个想法是使用上面的方法在控制器上调用一个动作,它会通过从数据库中提取数据来生成 HTML 菜单结构.然后我还使用简单的输出缓存来缓存数据几分钟..

The idea is to use the above to call an action on a controller, which will generate the HTML menu structure by pulling data from the DB. I then also use simple Output Caching to cache the data for a few minutes..

这是迄今为止我找到的最简单的方法来实现我想要的.

This is the simplest approach I've found so far to achieve what I want.

Phil Haack 最近在博客上写了这个 - Html.RenderAction 和 Html.Action.一篇很棒的博文,涵盖了我所有的确切需求,并解释了所有问题.

Phil Haack blogged about this recently - Html.RenderAction and Html.Action. A great blog post covering all my exact needs, with explanations of all the issues.

为了使缓存正常工作,我需要将我的 Html.RenderAction() 调用放在 ViewUserControl 中,并设置如下的 OutputCaching 指令:

To get the caching to work correctly, I would need to put my Html.RenderAction() call inside a ViewUserControl with the OutputCaching directive set as follows:

<@ OutputCache Duration="100" VaryByParam="None" %>

然后我用 Html.RenderPartial() 调用上面的代码,瞧,一切正常!

I then call the above with Html.RenderPartial(), and voila, it all works!

这篇关于带有缓存的 ASP.Net MVC 数据库驱动菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆