diff --git a/src/Umbraco.Core/PublishedCache/IPublishedSnapshotService.cs b/src/Umbraco.Core/PublishedCache/IPublishedSnapshotService.cs index 5bd5ff23cc..8e661aa758 100644 --- a/src/Umbraco.Core/PublishedCache/IPublishedSnapshotService.cs +++ b/src/Umbraco.Core/PublishedCache/IPublishedSnapshotService.cs @@ -125,4 +125,8 @@ public interface IPublishedSnapshotService : IDisposable /// Cleans up unused snapshots /// Task CollectAsync(); + + void ResetLocalDb() + { + } } diff --git a/src/Umbraco.Infrastructure/Migrations/Upgrade/V_12_0_0/ResetCache.cs b/src/Umbraco.Infrastructure/Migrations/Upgrade/V_12_0_0/ResetCache.cs index b55b4c4ca7..0e41ad89ca 100644 --- a/src/Umbraco.Infrastructure/Migrations/Upgrade/V_12_0_0/ResetCache.cs +++ b/src/Umbraco.Infrastructure/Migrations/Upgrade/V_12_0_0/ResetCache.cs @@ -1,22 +1,34 @@ -using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment; +using Microsoft.Extensions.DependencyInjection; +using Umbraco.Cms.Core.DependencyInjection; +using Umbraco.Cms.Core.PublishedCache; +using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment; namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_12_0_0; public class ResetCache : MigrationBase { private readonly IHostingEnvironment _hostingEnvironment; + private readonly IPublishedSnapshotService _publishedSnapshotService; + [Obsolete("Use ctor with all params - This will be removed in Umbraco 14.")] public ResetCache(IMigrationContext context, IHostingEnvironment hostingEnvironment) - : base(context) => + : this(context, hostingEnvironment, StaticServiceProvider.Instance.GetRequiredService()) + { + } + + public ResetCache(IMigrationContext context, IHostingEnvironment hostingEnvironment, IPublishedSnapshotService publishedSnapshotService) + : base(context) + { _hostingEnvironment = hostingEnvironment; + _publishedSnapshotService = publishedSnapshotService; + } protected override void Migrate() { RebuildCache = true; var distCacheFolderAbsolutePath = Path.Combine(_hostingEnvironment.LocalTempPath, "DistCache"); - var nuCacheFolderAbsolutePath = Path.Combine(_hostingEnvironment.LocalTempPath, "NuCache"); DeleteAllFilesInFolder(distCacheFolderAbsolutePath); - DeleteAllFilesInFolder(nuCacheFolderAbsolutePath); + _publishedSnapshotService.ResetLocalDb(); } private void DeleteAllFilesInFolder(string path) diff --git a/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotService.cs b/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotService.cs index 68b50cfd91..a7f8c42823 100644 --- a/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotService.cs +++ b/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotService.cs @@ -70,6 +70,8 @@ internal class PublishedSnapshotService : IPublishedSnapshotService private long _mediaGen; private ContentStore _mediaStore = null!; + private string LocalFilePath => Path.Combine(_hostingEnvironment.LocalTempPath, "NuCache"); + public PublishedSnapshotService( PublishedSnapshotServiceOptions options, ISyncBootStateAccessor syncBootStateAccessor, @@ -475,6 +477,22 @@ internal class PublishedSnapshotService : IPublishedSnapshotService return GetUid(_mediaStore, id); } + + public void ResetLocalDb() + { + _logger.LogInformation( + "Resetting NuCache local db"); + var path = LocalFilePath; + if (Directory.Exists(path) is false) + { + return; + } + + MainDomRelease(); + Directory.Delete(path, true); + MainDomRegister(); + } + /// /// Lazily populates the stores only when they are first requested /// @@ -603,7 +621,7 @@ internal class PublishedSnapshotService : IPublishedSnapshotService /// private void MainDomRegister() { - var path = GetLocalFilesPath(); + var path = GetAndEnsureLocalFilesPathExists(); var localContentDbPath = Path.Combine(path, "NuCache.Content.db"); var localMediaDbPath = Path.Combine(path, "NuCache.Media.db"); @@ -652,9 +670,9 @@ internal class PublishedSnapshotService : IPublishedSnapshotService } } - private string GetLocalFilesPath() + private string GetAndEnsureLocalFilesPathExists() { - var path = Path.Combine(_hostingEnvironment.LocalTempPath, "NuCache"); + var path = LocalFilePath; if (!Directory.Exists(path)) {