asp.net-mvc - 使用 Umbraco CachedPartial 为不同模型缓存部分 View

标签 asp.net-mvc caching umbraco

我正在使用CachedPartial html 助手缓存该部分 View 。

@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,第一个和第三个 View 似乎是相同的...

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

我尝试使用

@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;
    }
}

然后您将能够提供自己的缓存 key :

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

编辑:

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

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();
    });

关于asp.net-mvc - 使用 Umbraco CachedPartial 为不同模型缓存部分 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41260172/

相关文章:

asp.net-mvc - 让 System.Web.Optimization 在类库中的 Razor View 中工作

javascript - 如何从 View 重新加载部分 View ?

asp.net - 有什么方法可以清除/刷新/删除OutputCache?

javascript - Switch 语句触发所有 @html.partials 而不是选定的值

c# - 相同路径上的“以下方法或属性之间的调用不明确”

asp.net-mvc - ASP.NET MVC 中 HTML 元素的 ID 生成

asp.net - 使用 SmtpClient 发送邮件

asp.net-mvc - 如何在Asp.net Core中将缓存更新为并发请求?

javascript - 如何要求浏览器刷新(刷新)我网站的缓存?

umbraco - 如何在 Umbraco 7 中以编程方式取消发布内容