Obtaining a cache dependency key
Mari Jørgensen
2019-10-11 17:13:01
Normally we then want to invalidate the cached model if the current content has changed, i.e. a new version is published.
To achieve this we need to obtain a version cache key using the IContentCacheKeyCreator interface:
IContentCacheKeyCreator.CreateVersionCacheKey(contentLink)
However, this will only work if the contentLink provided contains the necessary version info, meaning both the published version and a work id.
The code sample below show how that can be done for catalog node content:
var currentNodeContentLink = _referenceConverter.GetContentLink(catalogNodeId,
CatalogContentType.CatalogNode, 0); // 0 = published version
var versionCacheKey = GetVersionKey(currentNodeContentLink);
....
public string GetVersionKey(ContentReference contentLink)
{
var versionCacheKey = string.Empty;
var latestPublishedVersion = _contentVersionRepository.List(contentLink).ToList()
.FirstOrDefault(v => v.Status == VersionStatus.Published);
if (latestPublishedVersion != null)
{
// adding current published version to cache key
versionCacheKey = _cacheKeyCreator.CreateVersionCacheKey(latestPublishedVersion.ContentLink);
}
return versionCacheKey;
}