2016-12-21 11 views
3

私は部分ビューをキャッシュするCachedPartial htmlヘルパーを使用しています。私の見解では異なるモデルにUmbraco CachedPartialを使用したキャッシュの部分表示

@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 

は、第1および第3のビューは、同一であることを思え

ための 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 

答えて

3

の作品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) 

EDIT:キーが

に渡すことができます CachedPartialためのオーバーロードがあり、バージョン7から

このため

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

アイデアをお寄せいただきありがとうございます。私の編集した投稿をご覧ください:) –

+1

@SnakeEyes great - バージョン7以上で動作します。あなたがそのアプローチを使用したい場合は、拡張メソッドの必要はありません、それは直接書くことができます、私の編集された答えを参照してください - それを簡素化したい場合:) –

関連する問題