forked from dotnetcore/EasyCaching
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(FasterKv): Add FasterKv support (dotnetcore#418)
* feat(FasterKv): add FasterKv caching provider support. * fix wrong summary
- Loading branch information
1 parent
83b8ec8
commit 5b093dc
Showing
21 changed files
with
1,245 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,10 @@ | |
"GroupId": "MyGroupId" | ||
}, | ||
"ConsumerCount":2 | ||
}, | ||
"fasterKv": { | ||
"MemorySizeBit": 16, | ||
"PageSizeBit": 15 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,6 @@ public enum CachingProviderType | |
Ext1, | ||
Ext2, | ||
LiteDB, | ||
FasterKv, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/EasyCaching.FasterKv/ClientSessionPooledObjectPolicy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using FASTER.core; | ||
|
||
namespace EasyCaching.FasterKv | ||
{ | ||
public class ClientSessionWrap : IDisposable | ||
{ | ||
public ClientSession<SpanByte, SpanByte, SpanByte, SpanByteAndMemory, StoreContext, StoreFunctions> Session { get; } | ||
|
||
private readonly ConcurrentQueue<ClientSession<SpanByte, SpanByte, SpanByte, SpanByteAndMemory, StoreContext, StoreFunctions>> _innerPool; | ||
|
||
public ClientSessionWrap( | ||
ClientSession<SpanByte, SpanByte, SpanByte, SpanByteAndMemory, StoreContext, StoreFunctions> clientSession, | ||
ConcurrentQueue<ClientSession<SpanByte, SpanByte, SpanByte, SpanByteAndMemory, StoreContext, StoreFunctions>> innerPool) | ||
{ | ||
Session = clientSession; | ||
_innerPool = innerPool; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Session.CompletePending(true); | ||
_innerPool.Enqueue(Session); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/EasyCaching.FasterKv/Configurations/FasterKvCachingOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.IO; | ||
using EasyCaching.Core.Configurations; | ||
using FASTER.core; | ||
|
||
namespace EasyCaching.FasterKv.Configurations | ||
{ | ||
/// <summary> | ||
/// FasterKvCachingOptions | ||
/// for details, see https://microsoft.github.io/FASTER/docs/fasterkv-basics/#fasterkvsettings | ||
/// </summary> | ||
public class FasterKvCachingOptions : BaseProviderOptions | ||
{ | ||
/// <summary> | ||
/// FasterKv index count, Must be power of 2 | ||
/// </summary> | ||
/// <para>For example: 1024(2^10) 2048(2^11) 65536(2^16) 131072(2^17)</para> | ||
/// <para>Each index is 64 bits. So this define 131072 keys. Used 1024Kb memory</para> | ||
public long IndexCount { get; set; } = 131072; | ||
|
||
/// <summary> | ||
/// FasterKv used memory size (default: 16MB) | ||
/// </summary> | ||
public int MemorySizeBit { get; set; } = 24; | ||
|
||
/// <summary> | ||
/// FasterKv page size (default: 1MB) | ||
/// </summary> | ||
public int PageSizeBit { get; set; } = 20; | ||
|
||
/// <summary> | ||
/// FasterKv read cache used memory size (default: 16MB) | ||
/// </summary> | ||
public int ReadCacheMemorySizeBit { get; set; } = 24; | ||
|
||
/// <summary> | ||
/// FasterKv read cache page size (default: 16MB) | ||
/// </summary> | ||
public int ReadCachePageSizeBit { get; set; } = 20; | ||
|
||
/// <summary> | ||
/// FasterKv commit logs path | ||
/// </summary> | ||
public string LogPath { get; set; } = | ||
#if (NET6_0 || NET7_0) | ||
Path.Combine(Environment.CurrentDirectory, $"EasyCaching-FasterKv-{Environment.ProcessId}"); | ||
#else | ||
Path.Combine(Environment.CurrentDirectory, $"EasyCaching-FasterKv-{System.Diagnostics.Process.GetCurrentProcess().Id}"); | ||
#endif | ||
|
||
|
||
/// <summary> | ||
/// Set Custom Store | ||
/// </summary> | ||
public FasterKV<SpanByte, SpanByte>? CustomStore { get; set; } | ||
|
||
internal LogSettings GetLogSettings(string name) | ||
{ | ||
return new LogSettings | ||
{ | ||
LogDevice = Devices.CreateLogDevice(Path.Combine(LogPath, name), | ||
preallocateFile: true, | ||
deleteOnClose: true), | ||
PageSizeBits = PageSizeBit, | ||
MemorySizeBits = MemorySizeBit, | ||
ReadCacheSettings = new ReadCacheSettings | ||
{ | ||
MemorySizeBits = ReadCacheMemorySizeBit, | ||
PageSizeBits = ReadCachePageSizeBit, | ||
} | ||
}; | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/EasyCaching.FasterKv/Configurations/FasterKvCachingOptionsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using EasyCaching.Core; | ||
using EasyCaching.Core.Configurations; | ||
using EasyCaching.FasterKv; | ||
using EasyCaching.FasterKv.Configurations; | ||
using Microsoft.Extensions.Configuration; | ||
// ReSharper disable CheckNamespace | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static class FasterKvCachingOptionsExtensions | ||
{ | ||
/// <summary> | ||
/// Uses the FasterKv provider (specify the config via hard code). | ||
/// </summary> | ||
/// <param name="options">Options.</param> | ||
/// <param name="configure">Configure provider settings.</param> | ||
/// <param name="name">The name of this provider instance.</param> | ||
public static EasyCachingOptions UseFasterKv( | ||
this EasyCachingOptions options, | ||
Action<FasterKvCachingOptions> configure, | ||
string name = EasyCachingConstValue.DefaultFasterKvName | ||
) | ||
{ | ||
ArgumentCheck.NotNull(configure, nameof(configure)); | ||
|
||
options.RegisterExtension(new FasterKvOptionsExtension(name, configure)); | ||
return options; | ||
} | ||
|
||
/// <summary> | ||
/// Uses the FasterKv provider (read config from configuration file). | ||
/// </summary> | ||
/// <param name="options">Options.</param> | ||
/// <param name="configuration">The configuration.</param> | ||
/// <param name="name">The name of this provider instance.</param> | ||
/// <param name="sectionName">The section name in the configuration file.</param> | ||
public static EasyCachingOptions UseFasterKv( | ||
this EasyCachingOptions options, | ||
IConfiguration configuration, | ||
string name = EasyCachingConstValue.DefaultFasterKvName, | ||
string sectionName = EasyCachingConstValue.FasterKvSection | ||
) | ||
{ | ||
var dbConfig = configuration.GetSection(sectionName); | ||
var fasterKvOptions = new FasterKvCachingOptions(); | ||
dbConfig.Bind(fasterKvOptions); | ||
|
||
void Configure(FasterKvCachingOptions x) | ||
{ | ||
x.EnableLogging = fasterKvOptions.EnableLogging; | ||
x.MaxRdSecond = fasterKvOptions.MaxRdSecond; | ||
x.LockMs = fasterKvOptions.LockMs; | ||
x.SleepMs = fasterKvOptions.SleepMs; | ||
x.SerializerName = fasterKvOptions.SerializerName; | ||
x.CacheNulls = fasterKvOptions.CacheNulls; | ||
x.IndexCount = fasterKvOptions.IndexCount; | ||
x.MemorySizeBit = fasterKvOptions.MemorySizeBit; | ||
x.PageSizeBit = fasterKvOptions.PageSizeBit; | ||
x.ReadCacheMemorySizeBit = fasterKvOptions.ReadCacheMemorySizeBit; | ||
x.ReadCachePageSizeBit = fasterKvOptions.ReadCachePageSizeBit; | ||
x.LogPath = fasterKvOptions.LogPath; | ||
} | ||
|
||
options.RegisterExtension(new FasterKvOptionsExtension(name, Configure)); | ||
return options; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/EasyCaching.FasterKv/Configurations/FasterKvOptionsExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using EasyCaching.Core; | ||
using EasyCaching.Core.Configurations; | ||
using EasyCaching.Core.Serialization; | ||
using EasyCaching.FasterKv.Configurations; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace EasyCaching.FasterKv | ||
{ | ||
/// <summary> | ||
/// FasterKv options extension. | ||
/// </summary> | ||
internal sealed class FasterKvOptionsExtension : IEasyCachingOptionsExtension | ||
{ | ||
/// <summary> | ||
/// The name. | ||
/// </summary> | ||
private readonly string _name; | ||
|
||
/// <summary> | ||
/// The configure. | ||
/// </summary> | ||
private readonly Action<FasterKvCachingOptions> _configure; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:EasyCaching.FasterKv.FasterKvOptionsExtension"/> class. | ||
/// </summary> | ||
/// <param name="name">Name.</param> | ||
/// <param name="configure">Configure.</param> | ||
public FasterKvOptionsExtension(string name, Action<FasterKvCachingOptions> configure) | ||
{ | ||
_name = name; | ||
_configure = configure; | ||
} | ||
|
||
/// <summary> | ||
/// Adds the services. | ||
/// </summary> | ||
/// <param name="services">Services.</param> | ||
public void AddServices(IServiceCollection services) | ||
{ | ||
services.AddOptions(); | ||
|
||
services.Configure(_name, _configure); | ||
|
||
services.TryAddSingleton<IEasyCachingProviderFactory, DefaultEasyCachingProviderFactory>(); | ||
|
||
services.AddSingleton<IEasyCachingProvider, DefaultFasterKvCachingProvider>(x => | ||
{ | ||
var optionsMon = x.GetRequiredService<IOptionsMonitor<FasterKvCachingOptions>>(); | ||
var options = optionsMon.Get(_name); | ||
var factory = x.GetService<ILoggerFactory>(); | ||
var serializers = x.GetServices<IEasyCachingSerializer>(); | ||
return new DefaultFasterKvCachingProvider(_name, options, serializers, factory); | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.