Skip to content

Commit

Permalink
Flush the cache data on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
kubagdynia committed Dec 5, 2023
1 parent 9ccc66a commit c2d9230
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CacheDrive.ExampleConsoleApp/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"CacheExpirationType": "Minutes",
"CacheExpiration": 60,
"CacheType": "MemoryAndFile",
"InitializeOnStartup": true
"InitializeOnStartup": true,
"FlushOnExit": true
}
}
6 changes: 6 additions & 0 deletions CacheDrive/Configuration/CacheSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ public class CacheSettings
/// Default value is true.
/// </summary>
public bool InitializeOnStartup { get; set; } = true;

/// <summary>
/// Before exit, flush the cache data to a files if necessary.
/// Default value is true.
/// </summary>
public bool FlushOnExit { get; set; } = true;
}
6 changes: 6 additions & 0 deletions CacheDrive/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ public static void RegisterCacheDrive(this IServiceCollection services, CacheSet
services.Configure<CacheSettings>(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
Expand All @@ -48,9 +51,12 @@ public static void RegisterCacheDrive(this IServiceCollection services, CacheSet
services.Configure<CacheSettings>(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
Expand Down
5 changes: 4 additions & 1 deletion CacheDrive/Services/MemoryCacheFileStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ private void DisposeManageResource()

private void DisposeUnManageResource()
{
Flush();
if (CacheSettings.FlushOnExit)
{
Flush();
}
}
}

0 comments on commit c2d9230

Please sign in to comment.