Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added contextualKeyBuilder optional argument to allow the key used for cachin... #143

Merged
merged 1 commit into from Dec 19, 2013

Conversation

IDisposable
Copy link
Contributor

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

…hing 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();
  }
)
@Aaron-P
Copy link

Aaron-P commented Sep 30, 2013

This may also fix this feature request from several months ago: http://issues.umbraco.org/issue/U4-1994

@ghost ghost mentioned this pull request Nov 7, 2013
Shazwazza added a commit that referenced this pull request Dec 19, 2013
Added contextualKeyBuilder optional argument to allow the key used for cachin...
@Shazwazza Shazwazza merged commit de9ecf6 into umbraco:6.2.0 Dec 19, 2013
@Shazwazza
Copy link
Contributor

Thanks!!

if (contextualKeyBuilder != null)
{
var contextualKey = contextualKeyBuilder(model, viewData);
cacheKey.Append("c{0}-", contextualKey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had to fix this, this should read AppendFormat - please let me know if you think that is incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants