From cdf5ddf539bd20633e30987da9b59917d45d28d1 Mon Sep 17 00:00:00 2001 From: Marc Brooks Date: Mon, 30 Sep 2013 18:23:56 -0500 Subject: [PATCH] Added cacheKeyBuilder optional argument to allow the key used for caching to be contextual. when building cached partials that work with many different instances of the same partial view (e.g. using the same partial more than once on a page with different ViewDataDictionary or different model), it is necessary to either name each instance differently (ugh), or not cache. By adding an optional argument to the CachedPartial Html helper method, we can vary the cache-key depending on the context of insertion. For maximum flexibility, pass the model and the viewData to the function so it can use that data to format a cacheKey suffix. A typical use would be something like: @Html.CachedPartial( "EventListings" , FeedRepository.getFeed(new CalendarEventsModel { StartingDate = DateTime.UtcNow.Date , Count = 10 }) , 0 , contextualKeyBuilder: (model, viewData) => { return (model as CalendarEventsModel).StartingDate.ToString(); } ) --- src/Umbraco.Web/HtmlHelperRenderExtensions.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Umbraco.Web/HtmlHelperRenderExtensions.cs b/src/Umbraco.Web/HtmlHelperRenderExtensions.cs index 2a009dfaa2..d8af4eecf0 100644 --- a/src/Umbraco.Web/HtmlHelperRenderExtensions.cs +++ b/src/Umbraco.Web/HtmlHelperRenderExtensions.cs @@ -82,7 +82,8 @@ namespace Umbraco.Web int cachedSeconds, bool cacheByPage = false, bool cacheByMember = false, - ViewDataDictionary viewData = null) + ViewDataDictionary viewData = null, + Func contextualKeyBuilder = null) { var cacheKey = new StringBuilder(partialViewName); if (cacheByPage) @@ -97,7 +98,12 @@ namespace Umbraco.Web { var currentMember = Member.GetCurrentMember(); cacheKey.AppendFormat("m{0}-", currentMember == null ? 0 : currentMember.Id); - } + } + if (contextualKeyBuilder != null) + { + var contextualKey = contextualKeyBuilder(model, viewData); + cacheKey.Append("c{0}-", contextualKey); + } return ApplicationContext.Current.ApplicationCache.CachedPartialView(htmlHelper, partialViewName, model, cachedSeconds, cacheKey.ToString(), viewData); } @@ -813,4 +819,4 @@ namespace Umbraco.Web #endregion } -} \ No newline at end of file +}