From 0a26115f498e185c8f223c04909005a2e3a123f1 Mon Sep 17 00:00:00 2001
From: InCerryGit <36690780+InCerryGit@users.noreply.github.com>
Date: Sun, 25 Feb 2024 12:16:24 +0800
Subject: [PATCH] Support FasterKv options (#518)
* Support FasterKv options:
1. pre allocate file
2. delete file on close
3. try recover latest
* Pass-through options
---
.../Configurations/FasterKvCachingOptions.cs | 20 ++++++++++++++---
.../FasterKvCachingOptionsExtensions.cs | 3 +++
.../DefaultFasterKvCachingProvider.cs | 22 +++++++++++++++----
3 files changed, 38 insertions(+), 7 deletions(-)
diff --git a/src/EasyCaching.FasterKv/Configurations/FasterKvCachingOptions.cs b/src/EasyCaching.FasterKv/Configurations/FasterKvCachingOptions.cs
index 325ff15f..95f25ca8 100644
--- a/src/EasyCaching.FasterKv/Configurations/FasterKvCachingOptions.cs
+++ b/src/EasyCaching.FasterKv/Configurations/FasterKvCachingOptions.cs
@@ -47,7 +47,21 @@ public class FasterKvCachingOptions : BaseProviderOptions
#else
Path.Combine(Environment.CurrentDirectory, $"EasyCaching-FasterKv-{System.Diagnostics.Process.GetCurrentProcess().Id}");
#endif
-
+
+ ///
+ /// Preallocate file
+ ///
+ public bool PreallocateFile { get; set; } = false;
+
+ ///
+ /// Delete file on close
+ ///
+ public bool DeleteFileOnClose { get; set; } = true;
+
+ ///
+ /// Try recover latest
+ ///
+ public bool TryRecoverLatest { get; set; } = false;
///
/// Set Custom Store
@@ -59,8 +73,8 @@ internal LogSettings GetLogSettings(string name)
return new LogSettings
{
LogDevice = Devices.CreateLogDevice(Path.Combine(LogPath, name),
- preallocateFile: true,
- deleteOnClose: true),
+ preallocateFile: PreallocateFile,
+ deleteOnClose: DeleteFileOnClose),
PageSizeBits = PageSizeBit,
MemorySizeBits = MemorySizeBit,
ReadCacheSettings = new ReadCacheSettings
diff --git a/src/EasyCaching.FasterKv/Configurations/FasterKvCachingOptionsExtensions.cs b/src/EasyCaching.FasterKv/Configurations/FasterKvCachingOptionsExtensions.cs
index d1d770da..e9db89e8 100644
--- a/src/EasyCaching.FasterKv/Configurations/FasterKvCachingOptionsExtensions.cs
+++ b/src/EasyCaching.FasterKv/Configurations/FasterKvCachingOptionsExtensions.cs
@@ -60,6 +60,9 @@ void Configure(FasterKvCachingOptions x)
x.ReadCacheMemorySizeBit = fasterKvOptions.ReadCacheMemorySizeBit;
x.ReadCachePageSizeBit = fasterKvOptions.ReadCachePageSizeBit;
x.LogPath = fasterKvOptions.LogPath;
+ x.PreallocateFile = fasterKvOptions.PreallocateFile;
+ x.DeleteFileOnClose = fasterKvOptions.DeleteFileOnClose;
+ x.TryRecoverLatest = fasterKvOptions.TryRecoverLatest;
}
options.RegisterExtension(new FasterKvOptionsExtension(name, Configure));
diff --git a/src/EasyCaching.FasterKv/DefaultFasterKvCachingProvider.cs b/src/EasyCaching.FasterKv/DefaultFasterKvCachingProvider.cs
index 5a2643dd..47e5f593 100644
--- a/src/EasyCaching.FasterKv/DefaultFasterKvCachingProvider.cs
+++ b/src/EasyCaching.FasterKv/DefaultFasterKvCachingProvider.cs
@@ -59,8 +59,16 @@ public DefaultFasterKvCachingProvider(
{
var logSetting = options.GetLogSettings(name);
_logDevice = logSetting.LogDevice;
- _fasterKv = new FasterKV(_options.IndexCount, logSetting,
- loggerFactory: loggerFactory);
+ _fasterKv = new FasterKV(
+ _options.IndexCount,
+ logSetting,
+ loggerFactory: loggerFactory,
+ tryRecoverLatest: _options.TryRecoverLatest,
+ checkpointSettings: new CheckpointSettings()
+ {
+ CheckpointDir = options.LogPath + ".checkpoint"
+ }
+ );
}
else
{
@@ -364,12 +372,18 @@ private void Dispose(bool _)
{
session.Dispose();
}
-
- _logDevice?.Dispose();
+
if (_options.CustomStore != _fasterKv)
{
+ if (_options.DeleteFileOnClose == false)
+ {
+ _fasterKv.TakeFullCheckpointAsync(CheckpointType.FoldOver).AsTask().GetAwaiter().GetResult();
+ }
_fasterKv.Dispose();
}
+
+ _logDevice?.Dispose();
+
_disposed = true;
}