From d2a26bf4da986e712a8b18fadf4e142ac768b082 Mon Sep 17 00:00:00 2001 From: Elitsa Marinovska Date: Thu, 25 Mar 2021 11:49:38 +0100 Subject: [PATCH] Caching RecycleBinSmells --- src/Umbraco.Core/Cache/CacheKeys.cs | 3 +++ .../Cache/ContentCacheRefresher.cs | 1 + src/Umbraco.Web/Cache/MediaCacheRefresher.cs | 1 + .../Trees/ContentTreeController.cs | 2 ++ .../Trees/ContentTreeControllerBase.cs | 25 +++++++++++++++++-- src/Umbraco.Web/Trees/MediaTreeController.cs | 1 + 6 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Core/Cache/CacheKeys.cs b/src/Umbraco.Core/Cache/CacheKeys.cs index 0e9a9a3862..642bef6d0c 100644 --- a/src/Umbraco.Core/Cache/CacheKeys.cs +++ b/src/Umbraco.Core/Cache/CacheKeys.cs @@ -17,5 +17,8 @@ public const string UserAllMediaStartNodesPrefix = "AllMediaStartNodes"; public const string UserMediaStartNodePathsPrefix = "MediaStartNodePaths"; public const string UserContentStartNodePathsPrefix = "ContentStartNodePaths"; + + public const string ContentRecycleBinCacheKey = "recycleBin_content"; + public const string MediaRecycleBinCacheKey = "recycleBin_media"; } } diff --git a/src/Umbraco.Web/Cache/ContentCacheRefresher.cs b/src/Umbraco.Web/Cache/ContentCacheRefresher.cs index 6abad820c9..5e8bd83c5d 100644 --- a/src/Umbraco.Web/Cache/ContentCacheRefresher.cs +++ b/src/Umbraco.Web/Cache/ContentCacheRefresher.cs @@ -46,6 +46,7 @@ namespace Umbraco.Web.Cache public override void Refresh(JsonPayload[] payloads) { AppCaches.RuntimeCache.ClearOfType(); + AppCaches.RuntimeCache.ClearByKey(CacheKeys.ContentRecycleBinCacheKey); var idsRemoved = new HashSet(); var isolatedCache = AppCaches.IsolatedCaches.GetOrCreate(); diff --git a/src/Umbraco.Web/Cache/MediaCacheRefresher.cs b/src/Umbraco.Web/Cache/MediaCacheRefresher.cs index 1f54b62c5b..b0845f2a9a 100644 --- a/src/Umbraco.Web/Cache/MediaCacheRefresher.cs +++ b/src/Umbraco.Web/Cache/MediaCacheRefresher.cs @@ -48,6 +48,7 @@ namespace Umbraco.Web.Cache if (anythingChanged) { Current.AppCaches.ClearPartialViewCache(); + AppCaches.RuntimeCache.ClearByKey(CacheKeys.MediaRecycleBinCacheKey); var mediaCache = AppCaches.IsolatedCaches.Get(); diff --git a/src/Umbraco.Web/Trees/ContentTreeController.cs b/src/Umbraco.Web/Trees/ContentTreeController.cs index d82166b9a3..3a4033e724 100644 --- a/src/Umbraco.Web/Trees/ContentTreeController.cs +++ b/src/Umbraco.Web/Trees/ContentTreeController.cs @@ -44,6 +44,8 @@ namespace Umbraco.Web.Trees protected override bool RecycleBinSmells => Services.ContentService.RecycleBinSmells(); + public override string RecycleBinSmellsCacheKey => CacheKeys.ContentRecycleBinCacheKey; + private int[] _userStartNodes; protected override int[] UserStartNodes diff --git a/src/Umbraco.Web/Trees/ContentTreeControllerBase.cs b/src/Umbraco.Web/Trees/ContentTreeControllerBase.cs index 57c7027598..c2a9019544 100644 --- a/src/Umbraco.Web/Trees/ContentTreeControllerBase.cs +++ b/src/Umbraco.Web/Trees/ContentTreeControllerBase.cs @@ -26,9 +26,11 @@ namespace Umbraco.Web.Trees { public abstract class ContentTreeControllerBase : TreeController { + private readonly AppCaches _appCaches; protected ContentTreeControllerBase(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper) : base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, umbracoHelper) { + _appCaches = appCaches; } protected ContentTreeControllerBase() @@ -148,6 +150,11 @@ namespace Umbraco.Web.Trees /// protected abstract bool RecycleBinSmells { get; } + /// + /// Gets the name of the recycle bin cache key. + /// + public abstract string RecycleBinSmellsCacheKey { get; } + /// /// Returns the user's start node for this tree /// @@ -327,15 +334,29 @@ namespace Umbraco.Web.Trees //and for some reason when there are no dashboards, this parameter is missing if (IsDialog(queryStrings) == false && id == Constants.System.RootString && queryStrings.HasKey("application")) { + var cache = _appCaches.RuntimeCache; + + var hasChildren = cache.GetCacheItem(RecycleBinSmellsCacheKey); + bool recycleBinSmells; + + if (!(hasChildren is null)) + { + recycleBinSmells = (bool) hasChildren; + } + else + { + recycleBinSmells = RecycleBinSmells; + cache.InsertCacheItem(RecycleBinSmellsCacheKey, () => recycleBinSmells); + } + nodes.Add(CreateTreeNode( RecycleBinId.ToInvariantString(), id, queryStrings, Services.TextService.Localize("general/recycleBin"), "icon-trash", - RecycleBinSmells, + recycleBinSmells, queryStrings.GetRequiredValue("application") + TreeAlias.EnsureStartsWith('/') + "/recyclebin")); - } return nodes; diff --git a/src/Umbraco.Web/Trees/MediaTreeController.cs b/src/Umbraco.Web/Trees/MediaTreeController.cs index 43b5a83282..93e1fc22b6 100644 --- a/src/Umbraco.Web/Trees/MediaTreeController.cs +++ b/src/Umbraco.Web/Trees/MediaTreeController.cs @@ -47,6 +47,7 @@ namespace Umbraco.Web.Trees protected override int RecycleBinId => Constants.System.RecycleBinMedia; protected override bool RecycleBinSmells => Services.MediaService.RecycleBinSmells(); + public override string RecycleBinSmellsCacheKey => CacheKeys.MediaRecycleBinCacheKey; private int[] _userStartNodes; protected override int[] UserStartNodes