I have a multi site install with about 7 different languages, the templates etc are common across all sites so the navigation etc needs to be unique across the different sites. This has historically been a problem with the wrong navigation being cached and displayed on the wrong sites sometimes. I've just completed an upgrade to 7.4.3 and the problem has reared it's ugly head again.
Could someone please clarify that the following should cache the FooterSectionLinks.cshtml partial uniquely per hostname because it doesn't always seem to?
I can't answer you question, but an alternative approach would be to use Donut Caching. If you've not used, this essentially just allows you to cache Actions. If you can pass the Action the correct info as parameters, then you could easily then just VaryByParam on the DonutOutputCacheAttribute. If you can't then you may need to do VaryByCustom.
Are the keys built with the contextualKeyBuilder appended to the Umbraco generated keys or do they need to be a complete key and for instance include the node id if we want them to vary by page?
I've not really found any documentation on using contextualKeyBuilder.
I don't know the answer, but I've assumed that they don't include Umbraco's default ones. Adding keys other than the ones you add could prevent you from caching as you'd wish.
I thought I'd check the source and it seems it is appended. See the following implementation:
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)
{
var cacheKey = new StringBuilder(partialViewName);
if (cacheByPage)
{
if (UmbracoContext.Current == null)
{
throw new InvalidOperationException("Cannot cache by page if the UmbracoContext has not been initialized, this parameter can only be used in the context of an Umbraco request");
}
cacheKey.AppendFormat("{0}-", UmbracoContext.Current.PageId);
}
if (cacheByMember)
{
var currentMember = Member.GetCurrentMember();
cacheKey.AppendFormat("m{0}-", currentMember == null ? 0 : currentMember.Id);
}
if (contextualKeyBuilder != null)
{
var contextualKey = contextualKeyBuilder(model, viewData);
cacheKey.AppendFormat("c{0}-", contextualKey);
}
return ApplicationContext.Current.ApplicationCache.CachedPartialView(htmlHelper, partialViewName, model, cachedSeconds, cacheKey.ToString(), viewData);
}
Whilst this does confirm what I suspected it unfortunately doesn't explain the recurring issue we keep having.
I use this approach on a multi-lingual site for caching partials once per culture (which is effectively once per site since each site has it's own culture settings).
CachedPartial Contextual Keys
I have a multi site install with about 7 different languages, the templates etc are common across all sites so the navigation etc needs to be unique across the different sites. This has historically been a problem with the wrong navigation being cached and displayed on the wrong sites sometimes. I've just completed an upgrade to 7.4.3 and the problem has reared it's ugly head again.
Could someone please clarify that the following should cache the
FooterSectionLinks.cshtml
partial uniquely per hostname because it doesn't always seem to?I can't answer you question, but an alternative approach would be to use Donut Caching. If you've not used, this essentially just allows you to cache Actions. If you can pass the Action the correct info as parameters, then you could easily then just VaryByParam on the DonutOutputCacheAttribute. If you can't then you may need to do VaryByCustom.
Thanks but that's not an option in this current implementation right now but certainly a consideration for the future.
Hi Simon,
Does each language have a different host name ?
Dave
It does yes.
Hmm..
Your code seems okay. Can you reproduce it on a clean 7.4.3 install ?
Dave
I've not tried no but then I can't even replicate it successfully on the site having the issues either, it seems pretty random.
Are the keys built with the contextualKeyBuilder appended to the Umbraco generated keys or do they need to be a complete key and for instance include the node id if we want them to vary by page?
I've not really found any documentation on using contextualKeyBuilder.
Thanks, Simon
I don't know the answer, but I've assumed that they don't include Umbraco's default ones. Adding keys other than the ones you add could prevent you from caching as you'd wish.
I thought I'd check the source and it seems it is appended. See the following implementation:
Whilst this does confirm what I suspected it unfortunately doesn't explain the recurring issue we keep having.
I use this approach on a multi-lingual site for caching partials once per culture (which is effectively once per site since each site has it's own culture settings).
Seems to work well and caches the partial once per site.
is working on a reply...