Skip to content

Commit 5b093dc

Browse files
authored
feat(FasterKv): Add FasterKv support (#418)
* feat(FasterKv): add FasterKv caching provider support. * fix wrong summary
1 parent 83b8ec8 commit 5b093dc

21 files changed

+1245
-23
lines changed

EasyCaching.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.Bus.ConfluentKa
7474
EndProject
7575
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Bus.Zookeeper", "bus\EasyCaching.Bus.Zookeeper\EasyCaching.Bus.Zookeeper.csproj", "{5E488583-391E-4E15-83C1-7301B4FE79AE}"
7676
EndProject
77+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.FasterKv", "src\EasyCaching.FasterKv\EasyCaching.FasterKv.csproj", "{7191E567-38DF-4879-82E1-73EC618AFCAC}"
78+
EndProject
7779
Global
7880
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7981
Debug|Any CPU = Debug|Any CPU
@@ -196,6 +198,10 @@ Global
196198
{5E488583-391E-4E15-83C1-7301B4FE79AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
197199
{5E488583-391E-4E15-83C1-7301B4FE79AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
198200
{5E488583-391E-4E15-83C1-7301B4FE79AE}.Release|Any CPU.Build.0 = Release|Any CPU
201+
{7191E567-38DF-4879-82E1-73EC618AFCAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
202+
{7191E567-38DF-4879-82E1-73EC618AFCAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
203+
{7191E567-38DF-4879-82E1-73EC618AFCAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
204+
{7191E567-38DF-4879-82E1-73EC618AFCAC}.Release|Any CPU.Build.0 = Release|Any CPU
199205
EndGlobalSection
200206
GlobalSection(SolutionProperties) = preSolution
201207
HideSolutionNode = FALSE
@@ -230,6 +236,7 @@ Global
230236
{4FCF16BF-5E21-4B74-AB45-3C121ADF1485} = {15070C49-A507-4844-BCFE-D319CFBC9A63}
231237
{F7FBADEB-D766-4595-949A-07104B52692C} = {B337509B-75F9-4851-821F-9BBE87C4E4BC}
232238
{5E488583-391E-4E15-83C1-7301B4FE79AE} = {B337509B-75F9-4851-821F-9BBE87C4E4BC}
239+
{7191E567-38DF-4879-82E1-73EC618AFCAC} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
233240
EndGlobalSection
234241
GlobalSection(ExtensibilityGlobals) = postSolution
235242
SolutionGuid = {63A57886-054B-476C-AAE1-8D7C8917682E}

sample/EasyCaching.Demo.Providers/Controllers/ValuesController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[Route("api/[controller]")]
99
public class ValuesController : Controller
1010
{
11-
//1. InMemory,Memcached,Redis,SQLite
11+
//1. InMemory,Memcached,Redis,SQLite,FasterKv
1212
private readonly IEasyCachingProvider _provider;
1313

1414
public ValuesController(IEasyCachingProvider provider)

sample/EasyCaching.Demo.Providers/EasyCaching.Demo.Providers.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<ItemGroup>
88
<ProjectReference Include="..\..\bus\EasyCaching.Bus.ConfluentKafka\EasyCaching.Bus.ConfluentKafka.csproj" />
99
<ProjectReference Include="..\..\bus\EasyCaching.Bus.Zookeeper\EasyCaching.Bus.Zookeeper.csproj" />
10+
<ProjectReference Include="..\..\src\EasyCaching.FasterKv\EasyCaching.FasterKv.csproj" />
1011
<ProjectReference Include="..\..\src\EasyCaching.Redis\EasyCaching.Redis.csproj" />
1112
<ProjectReference Include="..\..\src\EasyCaching.Memcached\EasyCaching.Memcached.csproj" />
1213
<ProjectReference Include="..\..\src\EasyCaching.SQLite\EasyCaching.SQLite.csproj" />

sample/EasyCaching.Demo.Providers/Startup.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ public void ConfigureServices(IServiceCollection services)
6161
});
6262

6363
option.UseMemcached(Configuration);
64+
65+
//use fasterKv
66+
option.UseFasterKv(config =>
67+
{
68+
// fasterKv must be set SerializerName
69+
config.SerializerName = "msg";
70+
})
71+
.WithMessagePack("msg");
6472
});
6573
}
6674

@@ -84,4 +92,4 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
8492
});
8593
}
8694
}
87-
}
95+
}

sample/EasyCaching.Demo.Providers/appsettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
"GroupId": "MyGroupId"
7777
},
7878
"ConsumerCount":2
79+
},
80+
"fasterKv": {
81+
"MemorySizeBit": 16,
82+
"PageSizeBit": 15
7983
}
8084
}
8185
}

src/EasyCaching.Core/Internal/CachingProviderType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ public enum CachingProviderType
1313
Ext1,
1414
Ext2,
1515
LiteDB,
16+
FasterKv,
1617
}
1718
}

src/EasyCaching.Core/Internal/EasyCachingConstValue.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public class EasyCachingConstValue
5353
/// <summary>
5454
/// The rabbitMQ Bus section.
5555
/// </summary>
56-
public const string RabbitMQBusSection = "easycaching:rabbitmqbus";
57-
56+
public const string RabbitMQBusSection = "easycaching:rabbitmqbus";
57+
5858
/// <summary>
5959
/// The kafka bus section.
6060
/// </summary>
61-
public const string KafkaBusSection = "easycaching:kafkabus";
62-
61+
public const string KafkaBusSection = "easycaching:kafkabus";
62+
6363
/// <summary>
6464
/// The zookeeper bus section.
6565
/// </summary>
@@ -104,19 +104,29 @@ public class EasyCachingConstValue
104104
/// <summary>
105105
/// The default name of the serializer.
106106
/// </summary>
107-
public const string DefaultSerializerName = "binary";
108-
107+
public const string DefaultSerializerName = "binary";
108+
109109
/// <summary>
110110
/// The default name of the LiteDB.
111111
/// </summary>
112-
public const string DefaultLiteDBName = "DefaultLiteDB";
112+
public const string DefaultLiteDBName = "DefaultLiteDB";
113113
/// <summary>
114114
/// The LiteDB Bus section.
115-
/// </summary>
116-
public const string LiteDBSection= "easycaching:litedb";
117-
118-
public const string NotFoundCliExceptionMessage = "Can not find the matched client instance, client name is {0}";
119-
120-
public const string NotFoundSerExceptionMessage = "Can not find the matched serializer instance, serializer name is {0}";
115+
/// </summary>
116+
public const string LiteDBSection= "easycaching:litedb";
117+
118+
/// <summary>
119+
/// The default name of the FasterKv
120+
/// </summary>
121+
public const string DefaultFasterKvName = "DefaultFasterKvName";
122+
123+
/// <summary>
124+
/// The FasterKv section.
125+
/// </summary>
126+
public const string FasterKvSection= "easycaching:fasterKv";
127+
128+
public const string NotFoundCliExceptionMessage = "Can not find the matched client instance, client name is {0}";
129+
130+
public const string NotFoundSerExceptionMessage = "Can not find the matched serializer instance, serializer name is {0}";
121131
}
122132
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using FASTER.core;
4+
5+
namespace EasyCaching.FasterKv
6+
{
7+
public class ClientSessionWrap : IDisposable
8+
{
9+
public ClientSession<SpanByte, SpanByte, SpanByte, SpanByteAndMemory, StoreContext, StoreFunctions> Session { get; }
10+
11+
private readonly ConcurrentQueue<ClientSession<SpanByte, SpanByte, SpanByte, SpanByteAndMemory, StoreContext, StoreFunctions>> _innerPool;
12+
13+
public ClientSessionWrap(
14+
ClientSession<SpanByte, SpanByte, SpanByte, SpanByteAndMemory, StoreContext, StoreFunctions> clientSession,
15+
ConcurrentQueue<ClientSession<SpanByte, SpanByte, SpanByte, SpanByteAndMemory, StoreContext, StoreFunctions>> innerPool)
16+
{
17+
Session = clientSession;
18+
_innerPool = innerPool;
19+
}
20+
21+
public void Dispose()
22+
{
23+
Session.CompletePending(true);
24+
_innerPool.Enqueue(Session);
25+
}
26+
}
27+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.IO;
3+
using EasyCaching.Core.Configurations;
4+
using FASTER.core;
5+
6+
namespace EasyCaching.FasterKv.Configurations
7+
{
8+
/// <summary>
9+
/// FasterKvCachingOptions
10+
/// for details, see https://microsoft.github.io/FASTER/docs/fasterkv-basics/#fasterkvsettings
11+
/// </summary>
12+
public class FasterKvCachingOptions : BaseProviderOptions
13+
{
14+
/// <summary>
15+
/// FasterKv index count, Must be power of 2
16+
/// </summary>
17+
/// <para>For example: 1024(2^10) 2048(2^11) 65536(2^16) 131072(2^17)</para>
18+
/// <para>Each index is 64 bits. So this define 131072 keys. Used 1024Kb memory</para>
19+
public long IndexCount { get; set; } = 131072;
20+
21+
/// <summary>
22+
/// FasterKv used memory size (default: 16MB)
23+
/// </summary>
24+
public int MemorySizeBit { get; set; } = 24;
25+
26+
/// <summary>
27+
/// FasterKv page size (default: 1MB)
28+
/// </summary>
29+
public int PageSizeBit { get; set; } = 20;
30+
31+
/// <summary>
32+
/// FasterKv read cache used memory size (default: 16MB)
33+
/// </summary>
34+
public int ReadCacheMemorySizeBit { get; set; } = 24;
35+
36+
/// <summary>
37+
/// FasterKv read cache page size (default: 16MB)
38+
/// </summary>
39+
public int ReadCachePageSizeBit { get; set; } = 20;
40+
41+
/// <summary>
42+
/// FasterKv commit logs path
43+
/// </summary>
44+
public string LogPath { get; set; } =
45+
#if (NET6_0 || NET7_0)
46+
Path.Combine(Environment.CurrentDirectory, $"EasyCaching-FasterKv-{Environment.ProcessId}");
47+
#else
48+
Path.Combine(Environment.CurrentDirectory, $"EasyCaching-FasterKv-{System.Diagnostics.Process.GetCurrentProcess().Id}");
49+
#endif
50+
51+
52+
/// <summary>
53+
/// Set Custom Store
54+
/// </summary>
55+
public FasterKV<SpanByte, SpanByte>? CustomStore { get; set; }
56+
57+
internal LogSettings GetLogSettings(string name)
58+
{
59+
return new LogSettings
60+
{
61+
LogDevice = Devices.CreateLogDevice(Path.Combine(LogPath, name),
62+
preallocateFile: true,
63+
deleteOnClose: true),
64+
PageSizeBits = PageSizeBit,
65+
MemorySizeBits = MemorySizeBit,
66+
ReadCacheSettings = new ReadCacheSettings
67+
{
68+
MemorySizeBits = ReadCacheMemorySizeBit,
69+
PageSizeBits = ReadCachePageSizeBit,
70+
}
71+
};
72+
}
73+
}
74+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using EasyCaching.Core;
3+
using EasyCaching.Core.Configurations;
4+
using EasyCaching.FasterKv;
5+
using EasyCaching.FasterKv.Configurations;
6+
using Microsoft.Extensions.Configuration;
7+
// ReSharper disable CheckNamespace
8+
9+
namespace Microsoft.Extensions.DependencyInjection;
10+
11+
public static class FasterKvCachingOptionsExtensions
12+
{
13+
/// <summary>
14+
/// Uses the FasterKv provider (specify the config via hard code).
15+
/// </summary>
16+
/// <param name="options">Options.</param>
17+
/// <param name="configure">Configure provider settings.</param>
18+
/// <param name="name">The name of this provider instance.</param>
19+
public static EasyCachingOptions UseFasterKv(
20+
this EasyCachingOptions options,
21+
Action<FasterKvCachingOptions> configure,
22+
string name = EasyCachingConstValue.DefaultFasterKvName
23+
)
24+
{
25+
ArgumentCheck.NotNull(configure, nameof(configure));
26+
27+
options.RegisterExtension(new FasterKvOptionsExtension(name, configure));
28+
return options;
29+
}
30+
31+
/// <summary>
32+
/// Uses the FasterKv provider (read config from configuration file).
33+
/// </summary>
34+
/// <param name="options">Options.</param>
35+
/// <param name="configuration">The configuration.</param>
36+
/// <param name="name">The name of this provider instance.</param>
37+
/// <param name="sectionName">The section name in the configuration file.</param>
38+
public static EasyCachingOptions UseFasterKv(
39+
this EasyCachingOptions options,
40+
IConfiguration configuration,
41+
string name = EasyCachingConstValue.DefaultFasterKvName,
42+
string sectionName = EasyCachingConstValue.FasterKvSection
43+
)
44+
{
45+
var dbConfig = configuration.GetSection(sectionName);
46+
var fasterKvOptions = new FasterKvCachingOptions();
47+
dbConfig.Bind(fasterKvOptions);
48+
49+
void Configure(FasterKvCachingOptions x)
50+
{
51+
x.EnableLogging = fasterKvOptions.EnableLogging;
52+
x.MaxRdSecond = fasterKvOptions.MaxRdSecond;
53+
x.LockMs = fasterKvOptions.LockMs;
54+
x.SleepMs = fasterKvOptions.SleepMs;
55+
x.SerializerName = fasterKvOptions.SerializerName;
56+
x.CacheNulls = fasterKvOptions.CacheNulls;
57+
x.IndexCount = fasterKvOptions.IndexCount;
58+
x.MemorySizeBit = fasterKvOptions.MemorySizeBit;
59+
x.PageSizeBit = fasterKvOptions.PageSizeBit;
60+
x.ReadCacheMemorySizeBit = fasterKvOptions.ReadCacheMemorySizeBit;
61+
x.ReadCachePageSizeBit = fasterKvOptions.ReadCachePageSizeBit;
62+
x.LogPath = fasterKvOptions.LogPath;
63+
}
64+
65+
options.RegisterExtension(new FasterKvOptionsExtension(name, Configure));
66+
return options;
67+
}
68+
}

0 commit comments

Comments
 (0)