internal static async Task ClearRelatedCacheAsync(this ContentType contentType, CancellationToken cancellationToken, string correlationID = null, bool clearDataCache = true, bool clearHtmlCache = true, bool doRefresh = true) { // data cache keys var sort = Sorts <ContentType> .Ascending("Title"); var dataCacheKeys = clearDataCache ? Extensions.GetRelatedCacheKeys(Filters <ContentType> .And(), sort) .Concat(Extensions.GetRelatedCacheKeys(ContentTypeProcessor.GetContentTypesFilter(contentType.SystemID), sort)) .Concat(Extensions.GetRelatedCacheKeys(ContentTypeProcessor.GetContentTypesFilter(contentType.SystemID, contentType.RepositoryID, contentType.ContentTypeDefinitionID), sort)) .Concat(Extensions.GetRelatedCacheKeys(ContentTypeProcessor.GetContentTypesFilter(contentType.SystemID, contentType.RepositoryID, null), sort)) .Concat(Extensions.GetRelatedCacheKeys(ContentTypeProcessor.GetContentTypesFilter(contentType.SystemID, null, contentType.ContentTypeDefinitionID), sort)) .Concat(await Utility.Cache.GetSetMembersAsync(contentType.GetSetCacheKey(), cancellationToken).ConfigureAwait(false)) .Concat(new[] { contentType.GetSetCacheKey() }) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList() : new List <string>(); // html cache keys (desktop HTMLs) var htmlCacheKeys = new List <string>(); if (clearHtmlCache) { htmlCacheKeys = contentType.Organization?.GetDesktopCacheKey() ?? new List <string>(); await new[] { contentType.Desktop?.GetSetCacheKey() } .Concat(await contentType.GetSetCacheKeysAsync(cancellationToken).ConfigureAwait(false) ?? new List <string>()) .Where(id => !string.IsNullOrWhiteSpace(id)) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList() .ForEachAsync(async desktopSetCacheKey => { var cacheKeys = await Utility.Cache.GetSetMembersAsync(desktopSetCacheKey, cancellationToken).ConfigureAwait(false); if (cacheKeys != null && cacheKeys.Count > 0) { htmlCacheKeys = htmlCacheKeys.Concat(cacheKeys).Concat(new[] { desktopSetCacheKey }).ToList(); } }, true, false).ConfigureAwait(false); } htmlCacheKeys = htmlCacheKeys.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); // clear related cache await Utility.Cache.RemoveAsync(htmlCacheKeys.Concat(dataCacheKeys).Distinct(StringComparer.OrdinalIgnoreCase).ToList(), cancellationToken).ConfigureAwait(false); await Task.WhenAll ( Utility.WriteCacheLogs?Utility.WriteLogAsync(correlationID, $"Clear related cache of a content-type [{contentType.Title} - ID: {contentType.ID}]\r\n- {dataCacheKeys.Count} data keys => {dataCacheKeys.Join(", ")}\r\n- {htmlCacheKeys.Count} html keys => {htmlCacheKeys.Join(", ")}", ServiceBase.ServiceComponent.CancellationToken, "Caches") : Task.CompletedTask, doRefresh?$"{Utility.PortalsHttpURI}/~{contentType.Organization.Alias}/".RefreshWebPageAsync(1, correlationID, $"Refresh desktop when related cache of a content-type was clean [{contentType.Title} - ID: {contentType.ID}]") : Task.CompletedTask ).ConfigureAwait(false); }
public static List <ContentType> FindContentTypes(this string systemID, string repositoryID = null, string definitionID = null, bool updateCache = true) { if (string.IsNullOrWhiteSpace(systemID)) { return(new List <ContentType>()); } var filter = ContentTypeProcessor.GetContentTypesFilter(systemID, repositoryID, definitionID); var sort = Sorts <ContentType> .Ascending("Title"); var contentTypes = ContentType.Find(filter, sort, 0, 1, Extensions.GetCacheKey(filter, sort, 0, 1)); contentTypes.ForEach(contentType => { if (contentType.ID.GetContentTypeByID(false, false) == null) { contentType.Set(updateCache); } }); return(contentTypes); }
public static async Task <List <ContentType> > FindContentTypesAsync(this string systemID, string repositoryID = null, string definitionID = null, CancellationToken cancellationToken = default, bool updateCache = true) { if (string.IsNullOrWhiteSpace(systemID)) { return(new List <ContentType>()); } var filter = ContentTypeProcessor.GetContentTypesFilter(systemID, repositoryID, definitionID); var sort = Sorts <ContentType> .Ascending("Title"); var contentTypes = await ContentType.FindAsync(filter, sort, 0, 1, Extensions.GetCacheKey(filter, sort, 0, 1), cancellationToken).ConfigureAwait(false); contentTypes.ForEach(contentType => { if (contentType.ID.GetContentTypeByID(false, false) == null) { contentType.Set(updateCache); } }); return(contentTypes); }