Files
Umbraco-CMS/src/Umbraco.Web/Cache/ContentCacheRefresher.cs
Shannon 881fbb7f76 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

168 lines
5.4 KiB
C#

using System;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence.Repositories;
using Umbraco.Core.Persistence.Repositories.Implement;
using Umbraco.Core.Services;
using Umbraco.Core.Services.Changes;
using Umbraco.Web.Composing;
using Umbraco.Web.PublishedCache;
namespace Umbraco.Web.Cache
{
public sealed class ContentCacheRefresher : PayloadCacheRefresherBase<ContentCacheRefresher, ContentCacheRefresher.JsonPayload>
{
private readonly IPublishedSnapshotService _publishedSnapshotService;
private readonly IdkMap _idkMap;
public ContentCacheRefresher(CacheHelper cacheHelper, IPublishedSnapshotService publishedSnapshotService, IdkMap idkMap)
: base(cacheHelper)
{
_publishedSnapshotService = publishedSnapshotService;
_idkMap = idkMap;
}
#region Define
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)
{
var runtimeCache = Current.ApplicationCache.RuntimeCache;
runtimeCache.ClearCacheObjectTypes<PublicAccessEntry>();
// fixme - this is for entity service, not sure why we do it here and nowhere else?
runtimeCache.ClearCacheByKeySearch(CacheKeys.IdToKeyCacheKey);
runtimeCache.ClearCacheByKeySearch(CacheKeys.KeyToIdCacheKey);
foreach (var payload in payloads)
{
// remove that one
runtimeCache.ClearCacheItem(RepositoryCacheKeys.GetKey<IContent>(payload.Id));
_idkMap.ClearCache(payload.Id);
// remove those that are in the branch
if (payload.ChangeTypes.HasTypesAny(TreeChangeTypes.RefreshBranch | TreeChangeTypes.Remove))
{
var pathid = "," + payload.Id + ",";
runtimeCache.ClearCacheObjectTypes<IContent>((k, v) => v.Path.Contains(pathid));
}
}
// 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...
// fixme - what about this?
// should rename it, and then, this is only for Deploy, and then, ???
//if (Suspendable.PageCacheRefresher.CanUpdateDocumentCache)
// ...
_publishedSnapshotService.Notify(payloads, out _, out var publishedChanged);
if (payloads.Any(x => x.ChangeTypes.HasType(TreeChangeTypes.RefreshAll)) || publishedChanged)
{
// when a public version changes
Current.ApplicationCache.ClearPartialViewCache();
MacroCacheRefresher.ClearMacroContentCache(CacheHelper); // just the content
ClearXsltCache();
Current.ApplicationCache.RuntimeCache.ClearCacheByKeySearch(CacheKeys.IdToKeyCacheKey);
Current.ApplicationCache.RuntimeCache.ClearCacheByKeySearch(CacheKeys.KeyToIdCacheKey);
}
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 Events
#endregion
#region Indirect
public static void RefreshContentTypes(CacheHelper cacheHelper)
{
// 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
cacheHelper.ClearPartialViewCache();
MacroCacheRefresher.ClearMacroContentCache(cacheHelper); // just the content
ClearXsltCache();
cacheHelper.IsolatedRuntimeCache.ClearCache<PublicAccessEntry>();
cacheHelper.IsolatedRuntimeCache.ClearCache<IContent>();
}
#endregion
#region Helpers
private static void ClearXsltCache()
{
// todo: document where this is coming from
if (UmbracoConfig.For.UmbracoSettings().Content.UmbracoLibraryCacheDuration <= 0) return;
Current.ApplicationCache.ClearCacheObjectTypes("MS.Internal.Xml.XPath.XPathSelectionIterator");
}
#endregion
}
}