Skip to content

Commit

Permalink
Support FasterKv options (#518)
Browse files Browse the repository at this point in the history
* Support FasterKv options:
1. pre allocate file
2. delete file on close
3. try recover latest

* Pass-through options
  • Loading branch information
InCerryGit authored Feb 25, 2024
1 parent 446e99a commit 0a26115
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
20 changes: 17 additions & 3 deletions src/EasyCaching.FasterKv/Configurations/FasterKvCachingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,21 @@ public class FasterKvCachingOptions : BaseProviderOptions
#else
Path.Combine(Environment.CurrentDirectory, $"EasyCaching-FasterKv-{System.Diagnostics.Process.GetCurrentProcess().Id}");
#endif


/// <summary>
/// Preallocate file
/// </summary>
public bool PreallocateFile { get; set; } = false;

/// <summary>
/// Delete file on close
/// </summary>
public bool DeleteFileOnClose { get; set; } = true;

/// <summary>
/// Try recover latest
/// </summary>
public bool TryRecoverLatest { get; set; } = false;

/// <summary>
/// Set Custom Store
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
22 changes: 18 additions & 4 deletions src/EasyCaching.FasterKv/DefaultFasterKvCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,16 @@ public DefaultFasterKvCachingProvider(
{
var logSetting = options.GetLogSettings(name);
_logDevice = logSetting.LogDevice;
_fasterKv = new FasterKV<SpanByte, SpanByte>(_options.IndexCount, logSetting,
loggerFactory: loggerFactory);
_fasterKv = new FasterKV<SpanByte, SpanByte>(
_options.IndexCount,
logSetting,
loggerFactory: loggerFactory,
tryRecoverLatest: _options.TryRecoverLatest,
checkpointSettings: new CheckpointSettings()
{
CheckpointDir = options.LogPath + ".checkpoint"
}
);
}
else
{
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 0a26115

Please sign in to comment.