Files
Umbraco-CMS/src/Umbraco.Web/Cache/ContentCacheRefresher.cs

173 lines
6.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence.Repositories;
2017-12-07 16:45:25 +01:00
using Umbraco.Core.Persistence.Repositories.Implement;
Merge remote-tracking branch 'origin/dev-v7' into dev-v8 # Conflicts: # build/Modules/Umbraco.Build/Umbraco.Build.psm1 # src/SolutionInfo.cs # src/Umbraco.Core/Configuration/UmbracoVersion.cs # src/Umbraco.Core/DatabaseContext.cs # src/Umbraco.Core/IO/FileSystemProviderManager.cs # src/Umbraco.Core/ObjectExtensions.cs # src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenSevenZero/AddUserGroupTables.cs # src/Umbraco.Core/PropertyEditors/ValueConverters/IntegerValueConverter.cs # src/Umbraco.Core/Services/IdkMap.cs # src/Umbraco.Core/Services/UserService.cs # src/Umbraco.Core/packages.config # src/Umbraco.Tests.Benchmarks/app.config # src/Umbraco.Tests/UI/LegacyDialogTests.cs # src/Umbraco.Web/Cache/MediaCacheRefresher.cs # src/Umbraco.Web/Cache/UnpublishedPageCacheRefresher.cs # src/Umbraco.Web/Features/DisabledFeatures.cs # src/Umbraco.Web/Mvc/RenderRouteHandler.cs # src/Umbraco.Web/Mvc/UmbracoAuthorizeAttribute.cs # src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs # src/Umbraco.Web/PublishedCache/ContextualPublishedContentCache.cs # src/Umbraco.Web/PublishedCache/ContextualPublishedMediaCache.cs # src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs # src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs # src/Umbraco.Web/PublishedContentQuery.cs # src/Umbraco.Web/WebApi/Filters/FeatureAuthorizeAttribute.cs # src/Umbraco.Web/packages.config # src/Umbraco.Web/umbraco.presentation/umbraco/create/MemberGroupTasks.cs
2018-03-29 20:01:14 +11:00
using Umbraco.Core.Services;
using Umbraco.Core.Services.Changes;
2017-05-30 18:13:11 +02:00
using Umbraco.Web.Composing;
using Umbraco.Web.PublishedCache;
namespace Umbraco.Web.Cache
{
public sealed class ContentCacheRefresher : PayloadCacheRefresherBase<ContentCacheRefresher, ContentCacheRefresher.JsonPayload>
{
2017-10-31 12:48:24 +01:00
private readonly IPublishedSnapshotService _publishedSnapshotService;
Merge remote-tracking branch 'origin/dev-v7' into dev-v8 # Conflicts: # build/Modules/Umbraco.Build/Umbraco.Build.psm1 # src/SolutionInfo.cs # src/Umbraco.Core/Configuration/UmbracoVersion.cs # src/Umbraco.Core/DatabaseContext.cs # src/Umbraco.Core/IO/FileSystemProviderManager.cs # src/Umbraco.Core/ObjectExtensions.cs # src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenSevenZero/AddUserGroupTables.cs # src/Umbraco.Core/PropertyEditors/ValueConverters/IntegerValueConverter.cs # src/Umbraco.Core/Services/IdkMap.cs # src/Umbraco.Core/Services/UserService.cs # src/Umbraco.Core/packages.config # src/Umbraco.Tests.Benchmarks/app.config # src/Umbraco.Tests/UI/LegacyDialogTests.cs # src/Umbraco.Web/Cache/MediaCacheRefresher.cs # src/Umbraco.Web/Cache/UnpublishedPageCacheRefresher.cs # src/Umbraco.Web/Features/DisabledFeatures.cs # src/Umbraco.Web/Mvc/RenderRouteHandler.cs # src/Umbraco.Web/Mvc/UmbracoAuthorizeAttribute.cs # src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs # src/Umbraco.Web/PublishedCache/ContextualPublishedContentCache.cs # src/Umbraco.Web/PublishedCache/ContextualPublishedMediaCache.cs # src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs # src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs # src/Umbraco.Web/PublishedContentQuery.cs # src/Umbraco.Web/WebApi/Filters/FeatureAuthorizeAttribute.cs # src/Umbraco.Web/packages.config # src/Umbraco.Web/umbraco.presentation/umbraco/create/MemberGroupTasks.cs
2018-03-29 20:01:14 +11:00
private readonly IdkMap _idkMap;
private readonly IDomainService _domainService;
2019-01-17 08:34:29 +01:00
public ContentCacheRefresher(AppCaches appCaches, IPublishedSnapshotService publishedSnapshotService, IdkMap idkMap, IDomainService domainService)
: base(appCaches)
{
2017-10-31 12:48:24 +01:00
_publishedSnapshotService = publishedSnapshotService;
Merge remote-tracking branch 'origin/dev-v7' into dev-v8 # Conflicts: # build/Modules/Umbraco.Build/Umbraco.Build.psm1 # src/SolutionInfo.cs # src/Umbraco.Core/Configuration/UmbracoVersion.cs # src/Umbraco.Core/DatabaseContext.cs # src/Umbraco.Core/IO/FileSystemProviderManager.cs # src/Umbraco.Core/ObjectExtensions.cs # src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenSevenZero/AddUserGroupTables.cs # src/Umbraco.Core/PropertyEditors/ValueConverters/IntegerValueConverter.cs # src/Umbraco.Core/Services/IdkMap.cs # src/Umbraco.Core/Services/UserService.cs # src/Umbraco.Core/packages.config # src/Umbraco.Tests.Benchmarks/app.config # src/Umbraco.Tests/UI/LegacyDialogTests.cs # src/Umbraco.Web/Cache/MediaCacheRefresher.cs # src/Umbraco.Web/Cache/UnpublishedPageCacheRefresher.cs # src/Umbraco.Web/Features/DisabledFeatures.cs # src/Umbraco.Web/Mvc/RenderRouteHandler.cs # src/Umbraco.Web/Mvc/UmbracoAuthorizeAttribute.cs # src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs # src/Umbraco.Web/PublishedCache/ContextualPublishedContentCache.cs # src/Umbraco.Web/PublishedCache/ContextualPublishedMediaCache.cs # src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs # src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs # src/Umbraco.Web/PublishedContentQuery.cs # src/Umbraco.Web/WebApi/Filters/FeatureAuthorizeAttribute.cs # src/Umbraco.Web/packages.config # src/Umbraco.Web/umbraco.presentation/umbraco/create/MemberGroupTasks.cs
2018-03-29 20:01:14 +11:00
_idkMap = idkMap;
_domainService = domainService;
}
#region Define
2017-07-11 19:21:13 +02:00
protected override ContentCacheRefresher This => this;
public static readonly Guid UniqueId = Guid.Parse("900A4FBE-DF3C-41E6-BB77-BE896CD158EA");
public override Guid RefresherUniqueId => UniqueId;
public override string Name => "ContentCacheRefresher";
#endregion
#region Refresher
public override void Refresh(JsonPayload[] payloads)
{
2019-01-17 11:01:23 +01:00
AppCaches.RuntimeCache.ClearOfType<PublicAccessEntry>();
var idsRemoved = new HashSet<int>();
2019-01-17 11:01:23 +01:00
var isolatedCache = AppCaches.IsolatedCaches.GetOrCreate<IContent>();
foreach (var payload in payloads)
{
2019-01-17 11:01:23 +01:00
isolatedCache.Clear(RepositoryCacheKeys.GetKey<IContent>(payload.Id));
Merge remote-tracking branch 'origin/dev-v7' into dev-v8 # Conflicts: # build/Modules/Umbraco.Build/Umbraco.Build.psm1 # src/SolutionInfo.cs # src/Umbraco.Core/Configuration/UmbracoVersion.cs # src/Umbraco.Core/DatabaseContext.cs # src/Umbraco.Core/IO/FileSystemProviderManager.cs # src/Umbraco.Core/ObjectExtensions.cs # src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenSevenZero/AddUserGroupTables.cs # src/Umbraco.Core/PropertyEditors/ValueConverters/IntegerValueConverter.cs # src/Umbraco.Core/Services/IdkMap.cs # src/Umbraco.Core/Services/UserService.cs # src/Umbraco.Core/packages.config # src/Umbraco.Tests.Benchmarks/app.config # src/Umbraco.Tests/UI/LegacyDialogTests.cs # src/Umbraco.Web/Cache/MediaCacheRefresher.cs # src/Umbraco.Web/Cache/UnpublishedPageCacheRefresher.cs # src/Umbraco.Web/Features/DisabledFeatures.cs # src/Umbraco.Web/Mvc/RenderRouteHandler.cs # src/Umbraco.Web/Mvc/UmbracoAuthorizeAttribute.cs # src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs # src/Umbraco.Web/PublishedCache/ContextualPublishedContentCache.cs # src/Umbraco.Web/PublishedCache/ContextualPublishedMediaCache.cs # src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs # src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs # src/Umbraco.Web/PublishedContentQuery.cs # src/Umbraco.Web/WebApi/Filters/FeatureAuthorizeAttribute.cs # src/Umbraco.Web/packages.config # src/Umbraco.Web/umbraco.presentation/umbraco/create/MemberGroupTasks.cs
2018-03-29 20:01:14 +11:00
_idkMap.ClearCache(payload.Id);
// remove those that are in the branch
if (payload.ChangeTypes.HasTypesAny(TreeChangeTypes.RefreshBranch | TreeChangeTypes.Remove))
{
var pathid = "," + payload.Id + ",";
2019-01-17 11:01:23 +01:00
isolatedCache.ClearOfType<IContent>((k, v) => v.Path.Contains(pathid));
}
//if the item is being completely removed, we need to refresh the domains cache if any domain was assigned to the content
if (payload.ChangeTypes.HasTypesAny(TreeChangeTypes.Remove))
{
idsRemoved.Add(payload.Id);
}
}
if (idsRemoved.Count > 0)
{
var assignedDomains = _domainService.GetAll(true).Where(x => x.RootContentId.HasValue && idsRemoved.Contains(x.RootContentId.Value)).ToList();
if (assignedDomains.Count > 0)
{
2019-01-21 15:57:48 +01:00
// todo - this is duplicating the logic in DomainCacheRefresher BUT we cannot inject that into this because it it not registered explicitly in the container,
// and we cannot inject the CacheRefresherCollection since that would be a circular reference, so what is the best way to call directly in to the
// DomainCacheRefresher?
ClearAllIsolatedCacheByEntityType<IDomain>();
// note: must do what's above FIRST else the repositories still have the old cached
// content and when the PublishedCachesService is notified of changes it does not see
// the new content...
// notify
_publishedSnapshotService.Notify(assignedDomains.Select(x => new DomainCacheRefresher.JsonPayload(x.Id, DomainChangeTypes.Remove)).ToArray());
}
}
// note: must do what's above FIRST else the repositories still have the old cached
// content and when the PublishedCachesService is notified of changes it does not see
// the new content...
2019-01-21 15:57:48 +01:00
// todo - what about this?
2017-09-19 15:51:47 +02:00
// should rename it, and then, this is only for Deploy, and then, ???
//if (Suspendable.PageCacheRefresher.CanUpdateDocumentCache)
// ...
2017-10-31 12:48:24 +01:00
_publishedSnapshotService.Notify(payloads, out _, out var publishedChanged);
if (payloads.Any(x => x.ChangeTypes.HasType(TreeChangeTypes.RefreshAll)) || publishedChanged)
{
// when a public version changes
2019-01-18 08:14:08 +01:00
Current.AppCaches.ClearPartialViewCache();
2019-01-17 08:34:29 +01:00
MacroCacheRefresher.ClearMacroContentCache(AppCaches); // just the content
}
base.Refresh(payloads);
}
// these events should never trigger
// everything should be PAYLOAD/JSON
public override void RefreshAll()
{
throw new NotSupportedException();
}
public override void Refresh(int id)
{
throw new NotSupportedException();
}
public override void Refresh(Guid id)
{
throw new NotSupportedException();
}
public override void Remove(int id)
{
throw new NotSupportedException();
}
#endregion
#region Json
public class JsonPayload
{
public JsonPayload(int id, TreeChangeTypes changeTypes)
{
Id = id;
ChangeTypes = changeTypes;
}
public int Id { get; }
public TreeChangeTypes ChangeTypes { get; }
}
#endregion
#region Indirect
2019-01-17 08:34:29 +01:00
public static void RefreshContentTypes(AppCaches appCaches)
{
// we could try to have a mechanism to notify the PublishedCachesService
// and figure out whether published items were modified or not... keep it
// simple for now, just clear the whole thing
2019-01-17 08:34:29 +01:00
appCaches.ClearPartialViewCache();
MacroCacheRefresher.ClearMacroContentCache(appCaches); // just the content
2019-01-17 11:01:23 +01:00
appCaches.IsolatedCaches.ClearCache<PublicAccessEntry>();
appCaches.IsolatedCaches.ClearCache<IContent>();
}
#endregion
}
}