Skip to content

Commit

Permalink
feat(FasterKv): Add FasterKv support (#418)
Browse files Browse the repository at this point in the history
* feat(FasterKv): add FasterKv caching provider support.

* fix wrong summary
  • Loading branch information
InCerryGit authored Nov 17, 2022
1 parent 83b8ec8 commit 5b093dc
Show file tree
Hide file tree
Showing 21 changed files with 1,245 additions and 23 deletions.
7 changes: 7 additions & 0 deletions EasyCaching.sln
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.Bus.ConfluentKa
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Bus.Zookeeper", "bus\EasyCaching.Bus.Zookeeper\EasyCaching.Bus.Zookeeper.csproj", "{5E488583-391E-4E15-83C1-7301B4FE79AE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.FasterKv", "src\EasyCaching.FasterKv\EasyCaching.FasterKv.csproj", "{7191E567-38DF-4879-82E1-73EC618AFCAC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -196,6 +198,10 @@ Global
{5E488583-391E-4E15-83C1-7301B4FE79AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E488583-391E-4E15-83C1-7301B4FE79AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E488583-391E-4E15-83C1-7301B4FE79AE}.Release|Any CPU.Build.0 = Release|Any CPU
{7191E567-38DF-4879-82E1-73EC618AFCAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7191E567-38DF-4879-82E1-73EC618AFCAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7191E567-38DF-4879-82E1-73EC618AFCAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7191E567-38DF-4879-82E1-73EC618AFCAC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -230,6 +236,7 @@ Global
{4FCF16BF-5E21-4B74-AB45-3C121ADF1485} = {15070C49-A507-4844-BCFE-D319CFBC9A63}
{F7FBADEB-D766-4595-949A-07104B52692C} = {B337509B-75F9-4851-821F-9BBE87C4E4BC}
{5E488583-391E-4E15-83C1-7301B4FE79AE} = {B337509B-75F9-4851-821F-9BBE87C4E4BC}
{7191E567-38DF-4879-82E1-73EC618AFCAC} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {63A57886-054B-476C-AAE1-8D7C8917682E}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[Route("api/[controller]")]
public class ValuesController : Controller
{
//1. InMemory,Memcached,Redis,SQLite
//1. InMemory,Memcached,Redis,SQLite,FasterKv
private readonly IEasyCachingProvider _provider;

public ValuesController(IEasyCachingProvider provider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<ItemGroup>
<ProjectReference Include="..\..\bus\EasyCaching.Bus.ConfluentKafka\EasyCaching.Bus.ConfluentKafka.csproj" />
<ProjectReference Include="..\..\bus\EasyCaching.Bus.Zookeeper\EasyCaching.Bus.Zookeeper.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.FasterKv\EasyCaching.FasterKv.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.Redis\EasyCaching.Redis.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.Memcached\EasyCaching.Memcached.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.SQLite\EasyCaching.SQLite.csproj" />
Expand Down
10 changes: 9 additions & 1 deletion sample/EasyCaching.Demo.Providers/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public void ConfigureServices(IServiceCollection services)
});
option.UseMemcached(Configuration);
//use fasterKv
option.UseFasterKv(config =>
{
// fasterKv must be set SerializerName
config.SerializerName = "msg";
})
.WithMessagePack("msg");
});
}

Expand All @@ -84,4 +92,4 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
});
}
}
}
}
4 changes: 4 additions & 0 deletions sample/EasyCaching.Demo.Providers/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
"GroupId": "MyGroupId"
},
"ConsumerCount":2
},
"fasterKv": {
"MemorySizeBit": 16,
"PageSizeBit": 15
}
}
}
1 change: 1 addition & 0 deletions src/EasyCaching.Core/Internal/CachingProviderType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public enum CachingProviderType
Ext1,
Ext2,
LiteDB,
FasterKv,
}
}
36 changes: 23 additions & 13 deletions src/EasyCaching.Core/Internal/EasyCachingConstValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public class EasyCachingConstValue
/// <summary>
/// The rabbitMQ Bus section.
/// </summary>
public const string RabbitMQBusSection = "easycaching:rabbitmqbus";

public const string RabbitMQBusSection = "easycaching:rabbitmqbus";

/// <summary>
/// The kafka bus section.
/// </summary>
public const string KafkaBusSection = "easycaching:kafkabus";

public const string KafkaBusSection = "easycaching:kafkabus";

/// <summary>
/// The zookeeper bus section.
/// </summary>
Expand Down Expand Up @@ -104,19 +104,29 @@ public class EasyCachingConstValue
/// <summary>
/// The default name of the serializer.
/// </summary>
public const string DefaultSerializerName = "binary";

public const string DefaultSerializerName = "binary";

/// <summary>
/// The default name of the LiteDB.
/// </summary>
public const string DefaultLiteDBName = "DefaultLiteDB";
public const string DefaultLiteDBName = "DefaultLiteDB";
/// <summary>
/// The LiteDB Bus section.
/// </summary>
public const string LiteDBSection= "easycaching:litedb";

public const string NotFoundCliExceptionMessage = "Can not find the matched client instance, client name is {0}";

public const string NotFoundSerExceptionMessage = "Can not find the matched serializer instance, serializer name is {0}";
/// </summary>
public const string LiteDBSection= "easycaching:litedb";

/// <summary>
/// The default name of the FasterKv
/// </summary>
public const string DefaultFasterKvName = "DefaultFasterKvName";

/// <summary>
/// The FasterKv section.
/// </summary>
public const string FasterKvSection= "easycaching:fasterKv";

public const string NotFoundCliExceptionMessage = "Can not find the matched client instance, client name is {0}";

public const string NotFoundSerExceptionMessage = "Can not find the matched serializer instance, serializer name is {0}";
}
}
27 changes: 27 additions & 0 deletions src/EasyCaching.FasterKv/ClientSessionPooledObjectPolicy.cs
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 src/EasyCaching.FasterKv/Configurations/FasterKvCachingOptions.cs
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,
}
};
}
}
}
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;
}
}
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);
});
}
}
}
Loading

0 comments on commit 5b093dc

Please sign in to comment.