【问题标题】:Cache partial view using Umbraco CachedPartial for different model使用 Umbraco CachedPartial 缓存不同模型的局部视图
【发布时间】:2016-12-21 20:25:57
【问题描述】:

我正在使用 CachedPartial html 助手来缓存该部分视图。

@Html.CachedPartial("PartialView", MyModel, 3600, true);

在我看来,我有以下情况:

@Html.CachedPartial("PartialView", MyModel, 3600, true);

@Html.CachedPartial("AnotherPartialView", MyModel1, 3600, true);

@Html.CachedPartial("PartialView", MyModel3, 3600, true); // I want to reuse partial view

似乎第一个和第三个视图相同,因为CachedPartial ...

如何通过模型参数使缓存部分?

我尝试使用

@Html.CachedPartial("PartialView", MyModel, 3600, true, false, new ViewDataDictionary(MyModel3));

但同样的事情。


编辑:我使用了与 DZL 不同的方法,它可以工作

  public static IHtmlString CachedPartial( this HtmlHelper helper, string partialViewName, object model, string cacheKey = null )
  {
     if ( string.IsNullOrWhiteSpace( cacheKey ) ) {
        return helper.CachedPartial( partialViewName, model, AppSettings.PartialCachingSeconds, true );
     }

     Func<object, ViewDataDictionary, string> fc = ( o, v ) => cacheKey;

     return helper.CachedPartial( partialViewName, model, AppSettings.PartialCachingSeconds, true, contextualKeyBuilder: fc );
  }

然后

@Html.CachedPartial("PartialView", MyModel, "a_key");

@Html.CachedPartial("AnotherPartialView", MyModel1);

@Html.CachedPartial("PartialView", MyModel3, "another_key"); // I want to reuse partial view

【问题讨论】:

    标签: asp.net-mvc caching umbraco


    【解决方案1】:

    如果您愿意,您需要创建自己的 CachedPartial 实现,如下所示:

    using System;
    using System.Web.Mvc;
    using System.Web.Mvc.Html;
    using Umbraco.Web;
    using System.Web;
    using System.Runtime.Caching;
    
    public static class CachedPartialExtensions
    {
        public static IHtmlString MyCachedPartial(
            this HtmlHelper htmlHelper,
            string partialViewName,
            object model,
            int cachedSeconds,
            bool cacheByPage = false,
            string cacheKey = null,
            ViewDataDictionary viewData = null
        )
        {
            var newCacheKey = "fpc-";  //prefix to know which keys to clear on page publish (in Bootstraper.cs file)
            newCacheKey += partialViewName;
            if (cacheByPage)
            {
                newCacheKey += "page-" + UmbracoContext.Current.PageId;
            }
            if (!string.IsNullOrEmpty(cacheKey))
            {
                newCacheKey += "key-" + cacheKey;
            }
    
            var result = MemoryCache.Default.Get(newCacheKey) as MvcHtmlString;
            if(result == null)
            {
                result = htmlHelper.Partial(partialViewName, model, viewData);
                MemoryCache.Default.Add(new CacheItem(newCacheKey, result), new CacheItemPolicy
                {
                    AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(cachedSeconds)
                });
            }
    
            return result;
        }
    }
    

    然后您就可以提供自己的缓存密钥了:

    @Html.MyCachedPartial("PartialView", Model, 60, cacheKey: "model1key", cacheByPage: true)
    @Html.MyCachedPartial("PartialView", Model2, 60, cacheKey: "model2key", cacheByPage: true)
    

    编辑:

    从版本 7 开始,CachedPartial 有一个重载,允许传入密钥

    public static IHtmlString CachedPartial(
        this HtmlHelper htmlHelper, 
        string partialViewName, 
        object model, 
        int cachedSeconds, 
        bool cacheByPage = false, 
        bool cacheByMember = false, 
        ViewDataDictionary viewData = null, 
        Func<object, ViewDataDictionary, string> contextualKeyBuilder = null);
    

    这种情况的用例是:

    @Html.CachedPartial(
        "PartialView", 
        MyModel3, 
        3600, 
        cacheByPage: true,
        contextualKeyBuilder: (model, viewData) =>
        {
           return (model as MyViewModel).Id.ToString();
        });
    

    【讨论】:

    • @SnakeEyes 很棒 - 适用于版本 7+。如果您想使用该方法,则不需要扩展方法,可以直接编写,请参阅我编辑的答案-如果您想简化它:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 2013-06-14
    相关资源
    最近更新 更多