From d425fbe05e97db92f9ea33a2e800f95e4bfa2d27 Mon Sep 17 00:00:00 2001
From: nzdev <834725+nzdev@users.noreply.github.com>
Date: Fri, 12 Feb 2021 15:41:46 +1300
Subject: [PATCH] rebuild nucache if serializer changes
---
.../NuCache/NuCacheSerializerComponent.cs | 66 +++++++++++++++++++
.../NuCache/NuCacheSerializerComposer.cs | 21 ++++++
src/Umbraco.Web/Umbraco.Web.csproj | 2 +
src/umbraco.sln | 24 ++++++-
4 files changed, 111 insertions(+), 2 deletions(-)
create mode 100644 src/Umbraco.Web/PublishedCache/NuCache/NuCacheSerializerComponent.cs
create mode 100644 src/Umbraco.Web/PublishedCache/NuCache/NuCacheSerializerComposer.cs
diff --git a/src/Umbraco.Web/PublishedCache/NuCache/NuCacheSerializerComponent.cs b/src/Umbraco.Web/PublishedCache/NuCache/NuCacheSerializerComponent.cs
new file mode 100644
index 0000000000..4b4ccd295a
--- /dev/null
+++ b/src/Umbraco.Web/PublishedCache/NuCache/NuCacheSerializerComponent.cs
@@ -0,0 +1,66 @@
+using System;
+using System.Collections.Generic;
+using System.Configuration;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Umbraco.Core.Composing;
+using Umbraco.Core.Logging;
+using Umbraco.Core.Services;
+
+namespace Umbraco.Web.PublishedCache.NuCache
+{
+ ///
+ /// Rebuilds the database cache if required when the serializer changes
+ ///
+ public class NuCacheSerializerComponent : IComponent
+ {
+ private const string Nucache_Serializer_Key = "Umbraco.Web.PublishedCache.NuCache.Serializer";
+ private const string JSON_SERIALIZER_VALUE = "JSON";
+ private readonly IPublishedSnapshotService _service;
+ private readonly IKeyValueService _keyValueService;
+ private readonly IProfilingLogger _profilingLogger;
+
+ public NuCacheSerializerComponent(IPublishedSnapshotService service, IKeyValueService keyValueService,IProfilingLogger profilingLogger)
+ {
+ // service: nothing - this just ensures that the service is created at boot time
+ _service = service;
+ _keyValueService = keyValueService;
+ _profilingLogger = profilingLogger;
+ }
+
+ public void Initialize()
+ {
+ RebuildDatabaseCacheIfSerializerChanged();
+ }
+
+ private void RebuildDatabaseCacheIfSerializerChanged()
+ {
+ var serializer = ConfigurationManager.AppSettings[Nucache_Serializer_Key];
+ var currentSerializer = _keyValueService.GetValue(Nucache_Serializer_Key);
+
+ if (currentSerializer == null)
+ {
+ currentSerializer = JSON_SERIALIZER_VALUE;
+ }
+ if (serializer == null)
+ {
+ serializer = JSON_SERIALIZER_VALUE;
+ }
+
+ if (serializer != currentSerializer)
+ {
+ _profilingLogger.Info($"Database NuCache was serialized using {currentSerializer}. Currently configured NuCache serializer {serializer}. Rebuilding Nucache");
+ using (_profilingLogger.TraceDuration($"Rebuilding NuCache database with {currentSerializer} serializer"))
+ {
+
+ _service.Rebuild();
+ _keyValueService.SetValue(Nucache_Serializer_Key, serializer);
+ }
+ }
+ }
+
+ public void Terminate()
+ { }
+ }
+}
diff --git a/src/Umbraco.Web/PublishedCache/NuCache/NuCacheSerializerComposer.cs b/src/Umbraco.Web/PublishedCache/NuCache/NuCacheSerializerComposer.cs
new file mode 100644
index 0000000000..59a206bc47
--- /dev/null
+++ b/src/Umbraco.Web/PublishedCache/NuCache/NuCacheSerializerComposer.cs
@@ -0,0 +1,21 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Umbraco.Core;
+using Umbraco.Core.Composing;
+
+namespace Umbraco.Web.PublishedCache.NuCache
+{
+
+ [ComposeAfter(typeof(NuCacheComposer))]
+ [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
+ public class NuCacheSerializerComposer : ICoreComposer
+ {
+ public void Compose(Composition composition)
+ {
+ composition.Components().Append();
+ }
+ }
+}
diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj
index 088fb0eeb3..0878867162 100755
--- a/src/Umbraco.Web/Umbraco.Web.csproj
+++ b/src/Umbraco.Web/Umbraco.Web.csproj
@@ -251,6 +251,8 @@
+
+
diff --git a/src/umbraco.sln b/src/umbraco.sln
index 63fb856b5d..72b06a517a 100644
--- a/src/umbraco.sln
+++ b/src/umbraco.sln
@@ -58,8 +58,24 @@ Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "Umbraco.Web.UI.Client", "ht
StartServerOnDebug = "false"
EndProjectSection
EndProject
-Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "Umbraco.Tests.AcceptanceTest\", "Umbraco.Tests.AcceptanceTest\", "{9E4C8A12-FBE0-4673-8CE2-DF99D5D57817}"
+Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "Umbraco.Tests.AcceptanceTest(1)", "Umbraco.Tests.AcceptanceTest\", "{9E4C8A12-FBE0-4673-8CE2-DF99D5D57817}"
ProjectSection(WebsiteProperties) = preProject
+ TargetFrameworkMoniker = ".NETFramework,Version%3Dv4.0"
+ Debug.AspNetCompiler.VirtualPath = "/localhost_62926"
+ Debug.AspNetCompiler.PhysicalPath = "Umbraco.Tests.AcceptanceTest\"
+ Debug.AspNetCompiler.TargetPath = "PrecompiledWeb\localhost_62926\"
+ Debug.AspNetCompiler.Updateable = "true"
+ Debug.AspNetCompiler.ForceOverwrite = "true"
+ Debug.AspNetCompiler.FixedNames = "false"
+ Debug.AspNetCompiler.Debug = "True"
+ Release.AspNetCompiler.VirtualPath = "/localhost_62926"
+ Release.AspNetCompiler.PhysicalPath = "Umbraco.Tests.AcceptanceTest\"
+ Release.AspNetCompiler.TargetPath = "PrecompiledWeb\localhost_62926\"
+ Release.AspNetCompiler.Updateable = "true"
+ Release.AspNetCompiler.ForceOverwrite = "true"
+ Release.AspNetCompiler.FixedNames = "false"
+ Release.AspNetCompiler.Debug = "False"
+ VWDPort = "62926"
SlnRelativePath = "Umbraco.Tests.AcceptanceTest\"
EndProjectSection
EndProject
@@ -123,6 +139,10 @@ Global
{4C4C194C-B5E4-4991-8F87-4373E24CC19F}.Release|Any CPU.Build.0 = Release|Any CPU
{3819A550-DCEC-4153-91B4-8BA9F7F0B9B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3819A550-DCEC-4153-91B4-8BA9F7F0B9B4}.Release|Any CPU.ActiveCfg = Debug|Any CPU
+ {9E4C8A12-FBE0-4673-8CE2-DF99D5D57817}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {9E4C8A12-FBE0-4673-8CE2-DF99D5D57817}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {9E4C8A12-FBE0-4673-8CE2-DF99D5D57817}.Release|Any CPU.ActiveCfg = Debug|Any CPU
+ {9E4C8A12-FBE0-4673-8CE2-DF99D5D57817}.Release|Any CPU.Build.0 = Debug|Any CPU
{651E1350-91B6-44B7-BD60-7207006D7003}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{651E1350-91B6-44B7-BD60-7207006D7003}.Debug|Any CPU.Build.0 = Debug|Any CPU
{651E1350-91B6-44B7-BD60-7207006D7003}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -157,6 +177,7 @@ Global
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{227C3B55-80E5-4E7E-A802-BE16C5128B9D} = {2849E9D4-3B4E-40A3-A309-F3CB4F0E125F}
+ {9E4C8A12-FBE0-4673-8CE2-DF99D5D57817} = {B5BD12C1-A454-435E-8A46-FF4A364C0382}
{5D3B8245-ADA6-453F-A008-50ED04BFE770} = {B5BD12C1-A454-435E-8A46-FF4A364C0382}
{E3F9F378-AFE1-40A5-90BD-82833375DBFE} = {227C3B55-80E5-4E7E-A802-BE16C5128B9D}
{5B03EF4E-E0AC-4905-861B-8C3EC1A0D458} = {227C3B55-80E5-4E7E-A802-BE16C5128B9D}
@@ -164,7 +185,6 @@ Global
{3A33ADC9-C6C0-4DB1-A613-A9AF0210DF3D} = {B5BD12C1-A454-435E-8A46-FF4A364C0382}
{C7311C00-2184-409B-B506-52A5FAEA8736} = {FD962632-184C-4005-A5F3-E705D92FC645}
{FB5676ED-7A69-492C-B802-E7B24144C0FC} = {B5BD12C1-A454-435E-8A46-FF4A364C0382}
- {9E4C8A12-FBE0-4673-8CE2-DF99D5D57817} = {B5BD12C1-A454-435E-8A46-FF4A364C0382}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7A0F2E34-D2AF-4DAB-86A0-7D7764B3D0EC}