The problem
As per diagram above, we have 2 content types, Content_Type_A and Content_Type_B.
Content_Type_A has an open API endpoint to allow data pushed from alternate source.
Content_Type_B references Content_Type_A for some business logic.
Both content type are served in different page.
The Page Cache has turned on, both pages are cached.
We noticed that when API pushed data to Content_Type_A, the page (blue line) will get updated data display.
While the page uses Content_Type_B (red line) will not get updated.
The Solution
Then we found that Sitefinity do provide a way to manually notify the cache to invalidate (in reference).
We have our code listen to Content_Type_A update event, and notify the invalidation.
Code snippets:
public void Update()
{
if (Content_Type_A_Updated)
{
NotifyCache(TypeResolutionService.ResolveType("Content_Type_B"));
}
}
private static void NotifyCache(Type type)
{
CacheDependency.Notify(GetCacheDependencyKeys(type, type.FullName));
}
private static List<CacheDependencyKey> GetCacheDependencyKeys(Type type, string key)
{
var cacheDependencyKeys = new List<CacheDependencyKey>();
var manager = DynamicModuleManager.GetManager(string.Empty);
string applicationName = manager != null && manager.Provider != null ? manager.Provider.ApplicationName : string.Empty;
cacheDependencyKeys.AddRange(OutputCacheDependencyHelper.GetPublishedContentCacheDependencyKeys(type, applicationName));
var itemIds = manager.GetDataItems(type).Where(x => x.Status == ContentLifecycleStatus.Live && x.Visible).Select(x => x.Id).ToList();
foreach (var itemId in itemIds)
{
cacheDependencyKeys.AddRange(OutputCacheDependencyHelper.GetPublishedContentCacheDependencyKeys(type, itemId));
}
cacheDependencyKeys.Add(new CacheDependencyKey { Key = key, Type = type });
return cacheDependencyKeys;
}
Code can be further enhance to locate and select the right Content_Type_B that has dependency from Content_Type_A for invalidation.
References
This article first appeared on hawjeh blog.