internal static async Task ClearRelatedCacheAsync(this Module module, CancellationToken cancellationToken = default, string correlationID = null, bool clearDataCache = true, bool clearHtmlCache = true, bool doRefresh = true) { // data cache keys var sort = Sorts <Module> .Ascending("Title"); var dataCacheKeys = clearDataCache ? Extensions.GetRelatedCacheKeys(Filters <Module> .And(), sort) .Concat(Extensions.GetRelatedCacheKeys(ModuleProcessor.GetModulesFilter(module.SystemID), sort)) .Concat(Extensions.GetRelatedCacheKeys(ModuleProcessor.GetModulesFilter(module.SystemID, module.ModuleDefinitionID), sort)) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList() : new List <string>(); // html cache keys (desktop HTMLs) var htmlCacheKeys = new List <string>(); if (clearHtmlCache) { htmlCacheKeys = new[] { module.Desktop?.GetSetCacheKey() }.Concat(module.Organization?.GetDesktopCacheKey() ?? new List <string>()).ToList(); var desktopSetCacheKeys = new List <string>(); await module.ContentTypes.ForEachAsync(async contentType => { desktopSetCacheKeys = desktopSetCacheKeys.Concat(await contentType.GetSetCacheKeysAsync(cancellationToken).ConfigureAwait(false) ?? new List <string>()).ToList(); }, true, false).ConfigureAwait(false); await desktopSetCacheKeys.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 module [{module.Title} - ID: {module.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}/~{module.Organization.Alias}/".RefreshWebPageAsync(1, correlationID, $"Refresh desktop when related cache of a module was clean [{module.Title} - ID: {module.ID}]") : Task.CompletedTask ).ConfigureAwait(false); }
public static List <Module> FindModules(this string systemID, string definitionID = null, bool updateCache = true) { if (string.IsNullOrWhiteSpace(systemID)) { return(new List <Module>()); } var filter = ModuleProcessor.GetModulesFilter(systemID, definitionID); var sort = Sorts <Module> .Ascending("Title"); var modules = Module.Find(filter, sort, 0, 1, Extensions.GetCacheKey(filter, sort, 0, 1)); modules.ForEach(module => { if (module.ID.GetModuleByID(false, false) == null) { module.Set(updateCache); } }); return(modules); }
public static async Task <List <Module> > FindModulesAsync(this string systemID, string definitionID = null, CancellationToken cancellationToken = default, bool updateCache = true) { if (string.IsNullOrWhiteSpace(systemID)) { return(new List <Module>()); } var filter = ModuleProcessor.GetModulesFilter(systemID, definitionID); var sort = Sorts <Module> .Ascending("Title"); var modules = await Module.FindAsync(filter, sort, 0, 1, Extensions.GetCacheKey(filter, sort, 0, 1), cancellationToken).ConfigureAwait(false); modules.ForEach(module => { if (module.ID.GetModuleByID(false, false) == null) { module.Set(updateCache); } }); return(modules); }