From c2d9230a6d369f30a050597f450bd7d7979fb066 Mon Sep 17 00:00:00 2001 From: kubagdynia Date: Tue, 5 Dec 2023 21:30:32 +0100 Subject: [PATCH] Flush the cache data on exit --- CacheDrive.ExampleConsoleApp/appsettings.json | 3 ++- CacheDrive/Configuration/CacheSettings.cs | 6 ++++++ CacheDrive/Extensions/ServiceCollectionExtensions.cs | 6 ++++++ CacheDrive/Services/MemoryCacheFileStorageService.cs | 5 ++++- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CacheDrive.ExampleConsoleApp/appsettings.json b/CacheDrive.ExampleConsoleApp/appsettings.json index 2903618..edb3c0e 100644 --- a/CacheDrive.ExampleConsoleApp/appsettings.json +++ b/CacheDrive.ExampleConsoleApp/appsettings.json @@ -5,6 +5,7 @@ "CacheExpirationType": "Minutes", "CacheExpiration": 60, "CacheType": "MemoryAndFile", - "InitializeOnStartup": true + "InitializeOnStartup": true, + "FlushOnExit": true } } \ No newline at end of file diff --git a/CacheDrive/Configuration/CacheSettings.cs b/CacheDrive/Configuration/CacheSettings.cs index 8c80e07..d3e26a4 100644 --- a/CacheDrive/Configuration/CacheSettings.cs +++ b/CacheDrive/Configuration/CacheSettings.cs @@ -39,4 +39,10 @@ public class CacheSettings /// Default value is true. /// public bool InitializeOnStartup { get; set; } = true; + + /// + /// Before exit, flush the cache data to a files if necessary. + /// Default value is true. + /// + public bool FlushOnExit { get; set; } = true; } \ No newline at end of file diff --git a/CacheDrive/Extensions/ServiceCollectionExtensions.cs b/CacheDrive/Extensions/ServiceCollectionExtensions.cs index bebe88c..2749f8c 100644 --- a/CacheDrive/Extensions/ServiceCollectionExtensions.cs +++ b/CacheDrive/Extensions/ServiceCollectionExtensions.cs @@ -31,9 +31,12 @@ public static void RegisterCacheDrive(this IServiceCollection services, services.Configure(opt => { opt.CacheEnabled = settings.CacheEnabled; + opt.CacheFolderName = settings.CacheFolderName; opt.CacheExpiration = settings.CacheExpiration; opt.CacheExpirationType = settings.CacheExpirationType; opt.CacheType = settings.CacheType; + opt.InitializeOnStartup = settings.InitializeOnStartup; + opt.FlushOnExit = settings.FlushOnExit; }); } else @@ -48,9 +51,12 @@ public static void RegisterCacheDrive(this IServiceCollection services, services.Configure(opt => { opt.CacheEnabled = settings.CacheEnabled; + opt.CacheFolderName = settings.CacheFolderName; opt.CacheExpiration = settings.CacheExpiration; opt.CacheExpirationType = settings.CacheExpirationType; opt.CacheType = settings.CacheType; + opt.InitializeOnStartup = settings.InitializeOnStartup; + opt.FlushOnExit = settings.FlushOnExit; }); } else diff --git a/CacheDrive/Services/MemoryCacheFileStorageService.cs b/CacheDrive/Services/MemoryCacheFileStorageService.cs index 2cf81c2..dbc6c8e 100644 --- a/CacheDrive/Services/MemoryCacheFileStorageService.cs +++ b/CacheDrive/Services/MemoryCacheFileStorageService.cs @@ -175,6 +175,9 @@ private void DisposeManageResource() private void DisposeUnManageResource() { - Flush(); + if (CacheSettings.FlushOnExit) + { + Flush(); + } } } \ No newline at end of file