2024-10-15 12:02:48 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
|
|
|
|
|
using Umbraco.Cms.Core.Logging;
|
|
|
|
|
|
using Umbraco.Cms.Core.PublishedCache;
|
2024-10-02 08:43:44 +02:00
|
|
|
|
using Umbraco.Cms.Core.Scoping;
|
2024-10-15 12:02:48 +02:00
|
|
|
|
using Umbraco.Cms.Core.Services;
|
2024-10-01 15:03:02 +02:00
|
|
|
|
using Umbraco.Cms.Infrastructure.HybridCache.Persistence;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Cms.Infrastructure.HybridCache;
|
|
|
|
|
|
|
|
|
|
|
|
internal class DatabaseCacheRebuilder : IDatabaseCacheRebuilder
|
|
|
|
|
|
{
|
2024-10-15 12:02:48 +02:00
|
|
|
|
private const string NuCacheSerializerKey = "Umbraco.Web.PublishedCache.NuCache.Serializer";
|
2024-10-01 15:03:02 +02:00
|
|
|
|
private readonly IDatabaseCacheRepository _databaseCacheRepository;
|
2024-10-02 08:43:44 +02:00
|
|
|
|
private readonly ICoreScopeProvider _coreScopeProvider;
|
2024-10-15 12:02:48 +02:00
|
|
|
|
private readonly IOptions<NuCacheSettings> _nucacheSettings;
|
|
|
|
|
|
private readonly IKeyValueService _keyValueService;
|
|
|
|
|
|
private readonly ILogger<DatabaseCacheRebuilder> _logger;
|
|
|
|
|
|
private readonly IProfilingLogger _profilingLogger;
|
2024-10-01 15:03:02 +02:00
|
|
|
|
|
2024-10-15 12:02:48 +02:00
|
|
|
|
public DatabaseCacheRebuilder(
|
|
|
|
|
|
IDatabaseCacheRepository databaseCacheRepository,
|
|
|
|
|
|
ICoreScopeProvider coreScopeProvider,
|
|
|
|
|
|
IOptions<NuCacheSettings> nucacheSettings,
|
|
|
|
|
|
IKeyValueService keyValueService,
|
|
|
|
|
|
ILogger<DatabaseCacheRebuilder> logger, IProfilingLogger profilingLogger)
|
2024-10-01 15:03:02 +02:00
|
|
|
|
{
|
|
|
|
|
|
_databaseCacheRepository = databaseCacheRepository;
|
2024-10-02 08:43:44 +02:00
|
|
|
|
_coreScopeProvider = coreScopeProvider;
|
2024-10-15 12:02:48 +02:00
|
|
|
|
_nucacheSettings = nucacheSettings;
|
|
|
|
|
|
_keyValueService = keyValueService;
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_profilingLogger = profilingLogger;
|
2024-10-01 15:03:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-02 08:43:44 +02:00
|
|
|
|
public void Rebuild()
|
|
|
|
|
|
{
|
|
|
|
|
|
using ICoreScope scope = _coreScopeProvider.CreateCoreScope();
|
|
|
|
|
|
_databaseCacheRepository.Rebuild();
|
|
|
|
|
|
scope.Complete();
|
|
|
|
|
|
}
|
2024-10-15 12:02:48 +02:00
|
|
|
|
|
|
|
|
|
|
public void RebuildDatabaseCacheIfSerializerChanged()
|
|
|
|
|
|
{
|
|
|
|
|
|
using var scope = _coreScopeProvider.CreateCoreScope();
|
|
|
|
|
|
NuCacheSerializerType serializer = _nucacheSettings.Value.NuCacheSerializerType;
|
|
|
|
|
|
var currentSerializerValue = _keyValueService.GetValue(NuCacheSerializerKey);
|
|
|
|
|
|
|
|
|
|
|
|
if (Enum.TryParse(currentSerializerValue, out NuCacheSerializerType currentSerializer) && serializer == currentSerializer)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogWarning(
|
|
|
|
|
|
"Database cache was serialized using {CurrentSerializer}. Currently configured cache serializer {Serializer}. Rebuilding database cache.",
|
|
|
|
|
|
currentSerializer, serializer);
|
|
|
|
|
|
|
|
|
|
|
|
using (_profilingLogger.TraceDuration<DatabaseCacheRebuilder>($"Rebuilding database cache with {serializer} serializer"))
|
|
|
|
|
|
{
|
|
|
|
|
|
Rebuild();
|
|
|
|
|
|
_keyValueService.SetValue(NuCacheSerializerKey, serializer.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
scope.Complete();
|
|
|
|
|
|
}
|
2024-10-01 15:03:02 +02:00
|
|
|
|
}
|