rebuild nucache if serializer changes

This commit is contained in:
nzdev
2021-02-12 15:41:46 +13:00
parent 40184c0c3c
commit d425fbe05e
4 changed files with 111 additions and 2 deletions

View File

@@ -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
{
/// <summary>
/// Rebuilds the database cache if required when the serializer changes
/// </summary>
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<NuCacheSerializerComponent>($"Database NuCache was serialized using {currentSerializer}. Currently configured NuCache serializer {serializer}. Rebuilding Nucache");
using (_profilingLogger.TraceDuration<NuCacheSerializerComponent>($"Rebuilding NuCache database with {currentSerializer} serializer"))
{
_service.Rebuild();
_keyValueService.SetValue(Nucache_Serializer_Key, serializer);
}
}
}
public void Terminate()
{ }
}
}

View File

@@ -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<NuCacheSerializerComponent>();
}
}
}