Skip to content

etcd support for easycaching #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/buildandtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ jobs:
image: bitnami/memcached
ports:
- 11212:11211

etcd:
image: quay.io/coreos/etcd:v3.5.18
ports:
- 2379:2379
env:
ETCD_LISTEN_CLIENT_URLS: "http://0.0.0.0:2379"
ETCD_ADVERTISE_CLIENT_URLS: "http://127.0.0.1:2379"
ETCDCTL_API: 3
ALLOW_NONE_AUTHENTICATION: "yes"
steps:
- uses: actions/checkout@v4
- name: Setup .NET SDK 8.0.x
Expand Down
17 changes: 12 additions & 5 deletions EasyCaching.sln
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Serialization.M
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Demo.Locks", "sample\EasyCaching.Demo.Locks\EasyCaching.Demo.Locks.csproj", "{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Etcd", "src\EasyCaching.Etcd\EasyCaching.Etcd.csproj", "{BA59F594-423A-4667-B6A0-980619AED44E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -209,18 +211,22 @@ Global
{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
{EEF22C21-F380-4980-B72C-F14488369333}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EEF22C21-F380-4980-B72C-F14488369333}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EEF22C21-F380-4980-B72C-F14488369333}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEF22C21-F380-4980-B72C-F14488369333}.Release|Any CPU.Build.0 = Release|Any CPU
{3C9D5E40-B3A5-4649-8B40-08094644B0FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C9D5E40-B3A5-4649-8B40-08094644B0FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C9D5E40-B3A5-4649-8B40-08094644B0FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C9D5E40-B3A5-4649-8B40-08094644B0FB}.Release|Any CPU.Build.0 = Release|Any CPU
{EEF22C21-F380-4980-B72C-F14488369333}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EEF22C21-F380-4980-B72C-F14488369333}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EEF22C21-F380-4980-B72C-F14488369333}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEF22C21-F380-4980-B72C-F14488369333}.Release|Any CPU.Build.0 = Release|Any CPU
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}.Release|Any CPU.Build.0 = Release|Any CPU
{BA59F594-423A-4667-B6A0-980619AED44E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BA59F594-423A-4667-B6A0-980619AED44E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA59F594-423A-4667-B6A0-980619AED44E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA59F594-423A-4667-B6A0-980619AED44E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -256,9 +262,10 @@ Global
{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}
{EEF22C21-F380-4980-B72C-F14488369333} = {15070C49-A507-4844-BCFE-D319CFBC9A63}
{3C9D5E40-B3A5-4649-8B40-08094644B0FB} = {B337509B-75F9-4851-821F-9BBE87C4E4BC}
{EEF22C21-F380-4980-B72C-F14488369333} = {15070C49-A507-4844-BCFE-D319CFBC9A63}
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98} = {F88D727A-9F9C-43D9-90B1-D4A02BF8BC98}
{BA59F594-423A-4667-B6A0-980619AED44E} = {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 @@ -14,6 +14,7 @@
<ProjectReference Include="..\..\src\EasyCaching.Core\EasyCaching.Core.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.CSRedis\EasyCaching.CSRedis.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.Disk\EasyCaching.Disk.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.Etcd\EasyCaching.Etcd.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.HybridCache\EasyCaching.HybridCache.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.InMemory\EasyCaching.InMemory.csproj" />
<ProjectReference Include="..\..\interceptor\EasyCaching.Interceptor.AspectCore\EasyCaching.Interceptor.AspectCore.csproj" />
Expand Down
26 changes: 26 additions & 0 deletions sample/EasyCaching.Demo.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace EasyCaching.Demo.ConsoleApp
using MemoryPack;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;

class Program
Expand Down Expand Up @@ -56,6 +58,17 @@ static void Main(string[] args)
.WithJson("json")
.WithSystemTextJson("sysjson")
.WithMessagePack("msgpack");

option.UseEtcd(options =>
{
options.Address = "http://127.0.0.1:2379";
options.Timeout = 30000;
options.SerializerName = "json";
}, "e1").WithJson(jsonSerializerSettingsConfigure: x =>
{
x.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.None;
x.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
}, "json");
});

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

//etcd cache
var etcdCache = factory.GetCachingProvider("e1");
var re11 = etcdCache.GetAllKeysByPrefix("emk");
var re12 = etcdCache.GetByPrefix<Product>("emk");
etcdCache.Set<Product>("emkey3", prod, TimeSpan.FromSeconds(2000));
var re13 = etcdCache.Get<Product>("emkey3");
var re14 = etcdCache.GetAll<Product>(new List<string>()
{
"emkey3"
});
etcdCache.Remove("emkey3");
Console.WriteLine($"etcd cache get value, {re13.HasValue} {re13.IsNull} {re13.Value} ");

Console.ReadKey();
}
}
Expand Down
29 changes: 29 additions & 0 deletions sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,33 @@ public async Task MemoryLockingOperation(int millisecondsTimeout)
await memoryLock.ReleaseAsync();
}
}

[HttpPost("etcd-locking")]
public async Task EtcdLockingOperation(int millisecondsTimeout)
{
using var distributedLock = _distributedLockFactory.CreateLock("DefaultEtcd", "YourKey");

try
{
if (await distributedLock.LockAsync(millisecondsTimeout))
{
// Simulate operation
Thread.Sleep(2000);
}
else
{
// Proper error
}
}
catch (Exception ex)
{
// log error
throw new Exception("Exception", ex);
}
finally
{
// release lock at the end
await distributedLock.ReleaseAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ProjectReference Include="..\..\bus\EasyCaching.Bus.Redis\EasyCaching.Bus.Redis.csproj" />
<ProjectReference Include="..\..\interceptor\EasyCaching.Interceptor.AspectCore\EasyCaching.Interceptor.AspectCore.csproj" />
<ProjectReference Include="..\..\serialization\EasyCaching.Serialization.Json\EasyCaching.Serialization.Json.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.Etcd\EasyCaching.Etcd.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.HybridCache\EasyCaching.HybridCache.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.InMemory\EasyCaching.InMemory.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.Redis\EasyCaching.Redis.csproj" />
Expand Down
3 changes: 2 additions & 1 deletion sample/EasyCaching.Demo.Locks/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
global using EasyCaching.Core.Configurations;
global using EasyCaching.Core.DistributedLock;
global using Microsoft.AspNetCore.Mvc;
global using EasyCaching.Redis.DistributedLock;
global using EasyCaching.Redis.DistributedLock;
global using EasyCaching.Etcd.DistributedLock;
18 changes: 18 additions & 0 deletions sample/EasyCaching.Demo.Locks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Newtonsoft.Json;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Expand All @@ -23,6 +25,19 @@
})
.WithJson()//with josn serialization
.UseRedisLock(); // use distributed lock

// use etcd cache
option.UseEtcd(options =>
{
options.Address = "http://127.0.0.1:2379";
options.Timeout = 3000;
options.LockMs = 10000;
options.SerializerName = "json";
}).WithJson(jsonSerializerSettingsConfigure: x =>
{
x.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.None;
x.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
}, "json").UseEtcdLock();
});

#region How Inject Distributed and Memory lock
Expand All @@ -33,6 +48,9 @@
// inject to use memory lock
builder.Services.AddSingleton<IDistributedLockFactory, MemoryLockFactory>();

// inject to use memory lock
builder.Services.AddSingleton<IDistributedLockFactory, EtcdLockFactory>();

#endregion

var app = builder.Build();
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,FasterKv
//1. InMemory,Memcached,Redis,SQLite,FasterKv,Etcd
private readonly IEasyCachingProvider _provider;

public ValuesController(IEasyCachingProvider provider)
Expand Down Expand Up @@ -38,6 +38,9 @@ public string Get(string str)
case "set" :
_provider.Set("demo", "123", TimeSpan.FromMinutes(1));
return "seted";
case "getexpiretime":
var timeSpanData = _provider.GetExpiration("demo");
return $"{timeSpanData.TotalSeconds}";
case "remove" :
_provider.Remove("demo");
return "removed";
Expand All @@ -64,6 +67,9 @@ public async Task<string> GetAsync(string str)
case "set":
await _provider.SetAsync("demo", "123", TimeSpan.FromMinutes(1));
return "seted";
case "getexpiretime":
var timeSpanData = _provider.GetExpiration("demo");
return $"{timeSpanData.TotalSeconds}";
case "remove":
await _provider.RemoveAsync("demo");
return "removed";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<ItemGroup>
<ProjectReference Include="..\..\bus\EasyCaching.Bus.ConfluentKafka\EasyCaching.Bus.ConfluentKafka.csproj" />
<ProjectReference Include="..\..\bus\EasyCaching.Bus.Zookeeper\EasyCaching.Bus.Zookeeper.csproj" />
<ProjectReference Include="..\..\serialization\EasyCaching.Serialization.Json\EasyCaching.Serialization.Json.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.Etcd\EasyCaching.Etcd.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" />
Expand Down
13 changes: 13 additions & 0 deletions sample/EasyCaching.Demo.Providers/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

public class Startup
{
Expand Down Expand Up @@ -69,6 +70,18 @@ public void ConfigureServices(IServiceCollection services)
config.SerializerName = "msg";
})
.WithMessagePack("msg");

// use etcd cache
option.UseEtcd(options =>
{
options.Address = "http://127.0.0.1:2379";
options.Timeout = 30000;
options.SerializerName = "json";
}, "e1").WithJson(jsonSerializerSettingsConfigure: x =>
{
x.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.None;
x.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
}, "json");
});
}

Expand Down
1 change: 1 addition & 0 deletions src/EasyCaching.Core/Internal/CachingProviderType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public enum CachingProviderType
Ext2,
LiteDB,
FasterKv,
Etcd,
}
}
12 changes: 11 additions & 1 deletion src/EasyCaching.Core/Internal/EasyCachingConstValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,17 @@ public class EasyCachingConstValue
/// The default name of the FasterKv
/// </summary>
public const string DefaultFasterKvName = "DefaultFasterKvName";


/// <summary>
/// The default name of the etcd.
/// </summary>
public const string DefaultEtcdName = "DefaultEtcd";

/// <summary>
/// The etcd section.
/// </summary>
public const string EtcdSection = "easycaching:etcd";

/// <summary>
/// The FasterKv section.
/// </summary>
Expand Down
31 changes: 31 additions & 0 deletions src/EasyCaching.Etcd/Configurations/EtcdCachingOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using EasyCaching.Core.Configurations;

namespace EasyCaching.Etcd
{
/// <summary>
/// EasyCaching options extensions of Etcd.
/// </summary>
public class EtcdCachingOptions : BaseProviderOptions
{
/// <summary>
/// Etcd address
/// cluster:like "http://localhost:23790,http://localhost:23791,http://localhost:23792"
/// </summary>
public string Address { get; set; }

Check warning on line 14 in src/EasyCaching.Etcd/Configurations/EtcdCachingOptions.cs

View workflow job for this annotation

GitHub Actions / build on windows-latest

Non-nullable property 'Address' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 14 in src/EasyCaching.Etcd/Configurations/EtcdCachingOptions.cs

View workflow job for this annotation

GitHub Actions / build and test on ubuntu-latest

Non-nullable property 'Address' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 14 in src/EasyCaching.Etcd/Configurations/EtcdCachingOptions.cs

View workflow job for this annotation

GitHub Actions / build and test on ubuntu-latest

Non-nullable property 'Address' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

/// <summary>
/// Etcd access UserName
/// </summary>
public string UserName { get; set; }

Check warning on line 19 in src/EasyCaching.Etcd/Configurations/EtcdCachingOptions.cs

View workflow job for this annotation

GitHub Actions / build on windows-latest

Non-nullable property 'UserName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 19 in src/EasyCaching.Etcd/Configurations/EtcdCachingOptions.cs

View workflow job for this annotation

GitHub Actions / build and test on ubuntu-latest

Non-nullable property 'UserName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 19 in src/EasyCaching.Etcd/Configurations/EtcdCachingOptions.cs

View workflow job for this annotation

GitHub Actions / build and test on ubuntu-latest

Non-nullable property 'UserName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

/// <summary>
/// Etcd access Pwd
/// </summary>
public string Password { get; set; }

Check warning on line 24 in src/EasyCaching.Etcd/Configurations/EtcdCachingOptions.cs

View workflow job for this annotation

GitHub Actions / build on windows-latest

Non-nullable property 'Password' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 24 in src/EasyCaching.Etcd/Configurations/EtcdCachingOptions.cs

View workflow job for this annotation

GitHub Actions / build and test on ubuntu-latest

Non-nullable property 'Password' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 24 in src/EasyCaching.Etcd/Configurations/EtcdCachingOptions.cs

View workflow job for this annotation

GitHub Actions / build and test on ubuntu-latest

Non-nullable property 'Password' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

/// <summary>
/// Etcd timeout with Milliseconds
/// </summary>
public long Timeout { get; set; } = 3000;
}
}
Loading