Skip to content

Commit 467375a

Browse files
committed
feat:add EasyCaching.Etcd and modify EasyCaching.Demo.ConsoleApp for add test
1 parent 0ce6bd0 commit 467375a

File tree

13 files changed

+1461
-6
lines changed

13 files changed

+1461
-6
lines changed

EasyCaching.sln

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Serialization.M
8383
EndProject
8484
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Demo.Locks", "sample\EasyCaching.Demo.Locks\EasyCaching.Demo.Locks.csproj", "{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}"
8585
EndProject
86+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Etcd", "src\EasyCaching.Etcd\EasyCaching.Etcd.csproj", "{BA59F594-423A-4667-B6A0-980619AED44E}"
87+
EndProject
8688
Global
8789
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8890
Debug|Any CPU = Debug|Any CPU
@@ -209,18 +211,22 @@ Global
209211
{7191E567-38DF-4879-82E1-73EC618AFCAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
210212
{7191E567-38DF-4879-82E1-73EC618AFCAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
211213
{7191E567-38DF-4879-82E1-73EC618AFCAC}.Release|Any CPU.Build.0 = Release|Any CPU
212-
{EEF22C21-F380-4980-B72C-F14488369333}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
213-
{EEF22C21-F380-4980-B72C-F14488369333}.Debug|Any CPU.Build.0 = Debug|Any CPU
214-
{EEF22C21-F380-4980-B72C-F14488369333}.Release|Any CPU.ActiveCfg = Release|Any CPU
215-
{EEF22C21-F380-4980-B72C-F14488369333}.Release|Any CPU.Build.0 = Release|Any CPU
216214
{3C9D5E40-B3A5-4649-8B40-08094644B0FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
217215
{3C9D5E40-B3A5-4649-8B40-08094644B0FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
218216
{3C9D5E40-B3A5-4649-8B40-08094644B0FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
219217
{3C9D5E40-B3A5-4649-8B40-08094644B0FB}.Release|Any CPU.Build.0 = Release|Any CPU
218+
{EEF22C21-F380-4980-B72C-F14488369333}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
219+
{EEF22C21-F380-4980-B72C-F14488369333}.Debug|Any CPU.Build.0 = Debug|Any CPU
220+
{EEF22C21-F380-4980-B72C-F14488369333}.Release|Any CPU.ActiveCfg = Release|Any CPU
221+
{EEF22C21-F380-4980-B72C-F14488369333}.Release|Any CPU.Build.0 = Release|Any CPU
220222
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
221223
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}.Debug|Any CPU.Build.0 = Debug|Any CPU
222224
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}.Release|Any CPU.ActiveCfg = Release|Any CPU
223225
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}.Release|Any CPU.Build.0 = Release|Any CPU
226+
{BA59F594-423A-4667-B6A0-980619AED44E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
227+
{BA59F594-423A-4667-B6A0-980619AED44E}.Debug|Any CPU.Build.0 = Debug|Any CPU
228+
{BA59F594-423A-4667-B6A0-980619AED44E}.Release|Any CPU.ActiveCfg = Release|Any CPU
229+
{BA59F594-423A-4667-B6A0-980619AED44E}.Release|Any CPU.Build.0 = Release|Any CPU
224230
EndGlobalSection
225231
GlobalSection(SolutionProperties) = preSolution
226232
HideSolutionNode = FALSE
@@ -256,9 +262,10 @@ Global
256262
{F7FBADEB-D766-4595-949A-07104B52692C} = {B337509B-75F9-4851-821F-9BBE87C4E4BC}
257263
{5E488583-391E-4E15-83C1-7301B4FE79AE} = {B337509B-75F9-4851-821F-9BBE87C4E4BC}
258264
{7191E567-38DF-4879-82E1-73EC618AFCAC} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
259-
{EEF22C21-F380-4980-B72C-F14488369333} = {15070C49-A507-4844-BCFE-D319CFBC9A63}
260265
{3C9D5E40-B3A5-4649-8B40-08094644B0FB} = {B337509B-75F9-4851-821F-9BBE87C4E4BC}
266+
{EEF22C21-F380-4980-B72C-F14488369333} = {15070C49-A507-4844-BCFE-D319CFBC9A63}
261267
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98} = {F88D727A-9F9C-43D9-90B1-D4A02BF8BC98}
268+
{BA59F594-423A-4667-B6A0-980619AED44E} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
262269
EndGlobalSection
263270
GlobalSection(ExtensibilityGlobals) = postSolution
264271
SolutionGuid = {63A57886-054B-476C-AAE1-8D7C8917682E}

sample/EasyCaching.Demo.ConsoleApp/EasyCaching.Demo.ConsoleApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<ProjectReference Include="..\..\src\EasyCaching.Core\EasyCaching.Core.csproj" />
1515
<ProjectReference Include="..\..\src\EasyCaching.CSRedis\EasyCaching.CSRedis.csproj" />
1616
<ProjectReference Include="..\..\src\EasyCaching.Disk\EasyCaching.Disk.csproj" />
17+
<ProjectReference Include="..\..\src\EasyCaching.Etcd\EasyCaching.Etcd.csproj" />
1718
<ProjectReference Include="..\..\src\EasyCaching.HybridCache\EasyCaching.HybridCache.csproj" />
1819
<ProjectReference Include="..\..\src\EasyCaching.InMemory\EasyCaching.InMemory.csproj" />
1920
<ProjectReference Include="..\..\interceptor\EasyCaching.Interceptor.AspectCore\EasyCaching.Interceptor.AspectCore.csproj" />

sample/EasyCaching.Demo.ConsoleApp/Program.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ namespace EasyCaching.Demo.ConsoleApp
99
using MemoryPack;
1010
using Microsoft.Extensions.DependencyInjection;
1111
using Microsoft.Extensions.Options;
12+
using Newtonsoft.Json;
1213
using System;
14+
using System.Collections.Generic;
1315
using System.IO;
1416

1517
class Program
@@ -56,6 +58,17 @@ static void Main(string[] args)
5658
.WithJson("json")
5759
.WithSystemTextJson("sysjson")
5860
.WithMessagePack("msgpack");
61+
62+
option.UseEtcd(options =>
63+
{
64+
options.Address = "http://127.0.0.1:2379";
65+
options.Timeout = 30000;
66+
options.SerializerName = "json";
67+
}, "e1").WithJson(jsonSerializerSettingsConfigure: x =>
68+
{
69+
x.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.None;
70+
x.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
71+
}, "json");
5972
});
6073

6174
IServiceProvider serviceProvider = services.BuildServiceProvider();
@@ -104,6 +117,19 @@ static void Main(string[] args)
104117
var diskVal = diskCache.Get<string>("diskkey");
105118
Console.WriteLine($"disk cache get value, {diskVal.HasValue} {diskVal.IsNull} {diskVal.Value} ");
106119

120+
//etcd cache
121+
var etcdCache = factory.GetCachingProvider("e1");
122+
var re11 = etcdCache.GetAllKeysByPrefix("emk");
123+
var re12 = etcdCache.GetByPrefix<Product>("emk");
124+
etcdCache.Set<Product>("emkey3", prod, TimeSpan.FromSeconds(2000));
125+
var re13 = etcdCache.Get<Product>("emkey3");
126+
var re14 = etcdCache.GetAll<Product>(new List<string>()
127+
{
128+
"emkey3"
129+
});
130+
etcdCache.Remove("emkey3");
131+
Console.WriteLine($"etcd cache get value, {re13.HasValue} {re13.IsNull} {re13.Value} ");
132+
107133
Console.ReadKey();
108134
}
109135
}

src/EasyCaching.Core/Internal/CachingProviderType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public enum CachingProviderType
1414
Ext2,
1515
LiteDB,
1616
FasterKv,
17+
Etcd,
1718
}
1819
}

src/EasyCaching.Core/Internal/EasyCachingConstValue.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,17 @@ public class EasyCachingConstValue
119119
/// The default name of the FasterKv
120120
/// </summary>
121121
public const string DefaultFasterKvName = "DefaultFasterKvName";
122-
122+
123+
/// <summary>
124+
/// The default name of the etcd.
125+
/// </summary>
126+
public const string DefaultEtcdName = "DefaultEtcd";
127+
128+
/// <summary>
129+
/// The etcd section.
130+
/// </summary>
131+
public const string EtcdSection = "easycaching:etcd";
132+
123133
/// <summary>
124134
/// The FasterKv section.
125135
/// </summary>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using EasyCaching.Core.Configurations;
2+
3+
namespace EasyCaching.Etcd
4+
{
5+
/// <summary>
6+
/// EasyCaching options extensions of Etcd.
7+
/// </summary>
8+
public class EtcdCachingOptions : BaseProviderOptions
9+
{
10+
/// <summary>
11+
/// Etcd address
12+
/// cluster:like "http://localhost:23790,http://localhost:23791,http://localhost:23792"
13+
/// </summary>
14+
public string Address { get; set; }
15+
16+
/// <summary>
17+
/// Etcd access UserName
18+
/// </summary>
19+
public string UserName { get; set; }
20+
21+
/// <summary>
22+
/// Etcd access Pwd
23+
/// </summary>
24+
public string Password { get; set; }
25+
26+
/// <summary>
27+
/// Etcd timeout with Milliseconds
28+
/// </summary>
29+
public long Timeout { get; set; } = 3000;
30+
}
31+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using EasyCaching.Core;
3+
using EasyCaching.Core.Configurations;
4+
using EasyCaching.Etcd;
5+
using Microsoft.Extensions.Configuration;
6+
// ReSharper disable CheckNamespace
7+
8+
namespace Microsoft.Extensions.DependencyInjection;
9+
10+
public static class EtcdCachingOptionsExtensions
11+
{
12+
/// <summary>
13+
/// Uses the Etcd provider (specify the config via hard code).
14+
/// </summary>
15+
/// <param name="options">Options.</param>
16+
/// <param name="configure">Configure provider settings.</param>
17+
/// <param name="name">The name of this provider instance.</param>
18+
public static EasyCachingOptions UseEtcd(
19+
this EasyCachingOptions options,
20+
Action<EtcdCachingOptions> configure,
21+
string name = EasyCachingConstValue.DefaultEtcdName
22+
)
23+
{
24+
ArgumentCheck.NotNull(configure, nameof(configure));
25+
26+
options.RegisterExtension(new EtcdOptionsExtension(name, configure));
27+
return options;
28+
}
29+
30+
/// <summary>
31+
/// Uses the Etcd provider (read config from configuration file).
32+
/// </summary>
33+
/// <param name="options">Options.</param>
34+
/// <param name="configuration">The configuration.</param>
35+
/// <param name="name">The name of this provider instance.</param>
36+
/// <param name="sectionName">The section name in the configuration file.</param>
37+
public static EasyCachingOptions UseEtcd(
38+
this EasyCachingOptions options,
39+
IConfiguration configuration,
40+
string name = EasyCachingConstValue.DefaultEtcdName,
41+
string sectionName = EasyCachingConstValue.EtcdSection
42+
)
43+
{
44+
var dbConfig = configuration.GetSection(sectionName);
45+
var EtcdOptions = new EtcdCachingOptions();
46+
dbConfig.Bind(EtcdOptions);
47+
48+
void Configure(EtcdCachingOptions x)
49+
{
50+
x.EnableLogging = EtcdOptions.EnableLogging;
51+
x.MaxRdSecond = EtcdOptions.MaxRdSecond;
52+
x.LockMs = EtcdOptions.LockMs;
53+
x.SleepMs = EtcdOptions.SleepMs;
54+
x.SerializerName = EtcdOptions.SerializerName;
55+
x.CacheNulls = EtcdOptions.CacheNulls;
56+
x.Address = EtcdOptions.Address;
57+
x.UserName = EtcdOptions.UserName;
58+
x.Password = EtcdOptions.Password;
59+
x.Timeout= EtcdOptions.Timeout;
60+
}
61+
62+
options.RegisterExtension(new EtcdOptionsExtension(name, Configure));
63+
return options;
64+
}
65+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using EasyCaching.Core;
3+
using EasyCaching.Core.Configurations;
4+
using EasyCaching.Core.Serialization;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.DependencyInjection.Extensions;
7+
using Microsoft.Extensions.Logging;
8+
using Microsoft.Extensions.Options;
9+
10+
namespace EasyCaching.Etcd
11+
{
12+
/// <summary>
13+
/// Etcd options extension.
14+
/// </summary>
15+
internal sealed class EtcdOptionsExtension : IEasyCachingOptionsExtension
16+
{
17+
/// <summary>
18+
/// The name.
19+
/// </summary>
20+
private readonly string _name;
21+
22+
/// <summary>
23+
/// The configure.
24+
/// </summary>
25+
private readonly Action<EtcdCachingOptions> _configure;
26+
27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="T:EasyCaching.Etcd.EtcdOptionsExtension"/> class.
29+
/// </summary>
30+
/// <param name="name">Name.</param>
31+
/// <param name="configure">Configure.</param>
32+
public EtcdOptionsExtension(string name, Action<EtcdCachingOptions> configure)
33+
{
34+
_name = name;
35+
_configure = configure;
36+
}
37+
38+
/// <summary>
39+
/// Adds the services.
40+
/// </summary>
41+
/// <param name="services">Services.</param>
42+
public void AddServices(IServiceCollection services)
43+
{
44+
services.AddOptions();
45+
46+
services.Configure(_name, _configure);
47+
48+
services.TryAddSingleton<IEasyCachingProviderFactory, DefaultEasyCachingProviderFactory>();
49+
50+
services.AddSingleton<IEtcdCaching, EtcdCaching>(x =>
51+
{
52+
var optionsMon = x.GetRequiredService<IOptionsMonitor<EtcdCachingOptions>>();
53+
var options = optionsMon.Get(_name);
54+
var factory = x.GetService<ILoggerFactory>();
55+
var serializers = x.GetServices<IEasyCachingSerializer>();
56+
return new EtcdCaching(_name, options,serializers,factory);
57+
});
58+
59+
services.AddSingleton<IEasyCachingProvider, DefaultEtcdCachingProvider>(x =>
60+
{
61+
var mCache = x.GetServices<IEtcdCaching>();
62+
var optionsMon = x.GetRequiredService<IOptionsMonitor<EtcdCachingOptions>>();
63+
var options = optionsMon.Get(_name);
64+
var factory = x.GetService<ILoggerFactory>();
65+
var serializers = x.GetServices<IEasyCachingSerializer>();
66+
return new DefaultEtcdCachingProvider(_name,mCache, options, serializers, factory);
67+
});
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)