umbraco/Umbraco-CMS — pull request #143 by IDisposable (2013-09-30 23:24:19+00:00)
Title: Added cacheKeyBuilder optional argument to allow the key used for cachin...

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