使用 Umbraco CachedPartial 为不同模型缓存部分视图

Cache partial view using Umbraco CachedPartial for different model

我正在使用 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

如果需要,您需要创建自己的 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();
    });