From 96d5bdd7b20c3b45c05f2ac177b25be78bdb2b14 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Tue, 2 Jul 2019 15:26:54 +0200 Subject: [PATCH] https://github.com/umbraco/Umbraco-CMS/issues/5671 - If modelsbuilder is in live mode, we need to clear cache for all document types when at least one is cleared. This is due to modelsbuilder updating all the models to different versions. --- .../Cache/ContentTypeCacheRefresher.cs | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Web/Cache/ContentTypeCacheRefresher.cs b/src/Umbraco.Web/Cache/ContentTypeCacheRefresher.cs index 86ff709541..a5134426c2 100644 --- a/src/Umbraco.Web/Cache/ContentTypeCacheRefresher.cs +++ b/src/Umbraco.Web/Cache/ContentTypeCacheRefresher.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using Umbraco.Core; using Umbraco.Core.Cache; @@ -17,15 +18,17 @@ namespace Umbraco.Web.Cache private readonly IPublishedSnapshotService _publishedSnapshotService; private readonly IPublishedModelFactory _publishedModelFactory; private readonly IContentTypeCommonRepository _contentTypeCommonRepository; + private readonly IContentTypeService _contentTypeService; private readonly IdkMap _idkMap; - public ContentTypeCacheRefresher(AppCaches appCaches, IPublishedSnapshotService publishedSnapshotService, IPublishedModelFactory publishedModelFactory, IdkMap idkMap, IContentTypeCommonRepository contentTypeCommonRepository) + public ContentTypeCacheRefresher(AppCaches appCaches, IPublishedSnapshotService publishedSnapshotService, IPublishedModelFactory publishedModelFactory, IdkMap idkMap, IContentTypeCommonRepository contentTypeCommonRepository, IContentTypeService contentTypeService) : base(appCaches) { _publishedSnapshotService = publishedSnapshotService; _publishedModelFactory = publishedModelFactory; _idkMap = idkMap; _contentTypeCommonRepository = contentTypeCommonRepository; + _contentTypeService = contentTypeService; } #region Define @@ -50,6 +53,16 @@ namespace Umbraco.Web.Cache _contentTypeCommonRepository.ClearCache(); // always + // We need to special handle the IContentType if modelsbuilder is in live mode, because all models are updated when a IContentType is changed, we need to clear all from cache also. + if (_publishedModelFactory is ILivePublishedModelFactory && payloads.Any(x => x.ItemType == typeof(IContentType).Name)) + { + //This is super nasty, and we need to figure out a better way to to this + //Ensure all doc type ids is part of the payload + var missingPayloads = GetMissingContentTypePayloads(payloads); + + payloads = payloads.Union(missingPayloads).ToArray(); + } + if (payloads.Any(x => x.ItemType == typeof(IContentType).Name)) { ClearAllIsolatedCacheByEntityType(); @@ -96,6 +109,20 @@ namespace Umbraco.Web.Cache base.Refresh(payloads); } + private IEnumerable GetMissingContentTypePayloads(JsonPayload[] payloads) + { + var existingPayloadIds = new HashSet(payloads.Select(x => x.Id)); + var contentTypeIds = _contentTypeService.GetAll().Select(x => x.Id).ToArray(); + + foreach (var contentTypeId in contentTypeIds) + { + if (!existingPayloadIds.Contains(contentTypeId)) + { + yield return new JsonPayload(typeof(IContentType).Name, contentTypeId, ContentTypeChangeTypes.RefreshOther); + } + } + } + public override void RefreshAll() { throw new NotSupportedException();