Skip to content

Commit 930d9f0

Browse files
authored
Merge pull request #489 from ErfanMahmoudi/feat/add-locks-sample
Add locks sample
2 parents c74ad32 + 02c5166 commit 930d9f0

File tree

7 files changed

+176
-4
lines changed

7 files changed

+176
-4
lines changed

EasyCaching.sln

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
Microsoft Visual Studio Solution File, Format Version 12.00
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
23
# Visual Studio Version 17
34
VisualStudioVersion = 17.2.32616.157
45
MinimumVisualStudioVersion = 10.0.40219.1
@@ -72,11 +73,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.Serialization.S
7273
EndProject
7374
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.Bus.ConfluentKafka", "bus\EasyCaching.Bus.ConfluentKafka\EasyCaching.Bus.ConfluentKafka.csproj", "{F7FBADEB-D766-4595-949A-07104B52692C}"
7475
EndProject
75-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Bus.Zookeeper", "bus\EasyCaching.Bus.Zookeeper\EasyCaching.Bus.Zookeeper.csproj", "{5E488583-391E-4E15-83C1-7301B4FE79AE}"
76+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.Bus.Zookeeper", "bus\EasyCaching.Bus.Zookeeper\EasyCaching.Bus.Zookeeper.csproj", "{5E488583-391E-4E15-83C1-7301B4FE79AE}"
7677
EndProject
77-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.FasterKv", "src\EasyCaching.FasterKv\EasyCaching.FasterKv.csproj", "{7191E567-38DF-4879-82E1-73EC618AFCAC}"
78+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.FasterKv", "src\EasyCaching.FasterKv\EasyCaching.FasterKv.csproj", "{7191E567-38DF-4879-82E1-73EC618AFCAC}"
7879
EndProject
79-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Serialization.MemoryPack", "serialization\EasyCaching.Serialization.MemoryPack\EasyCaching.Serialization.MemoryPack.csproj", "{EEF22C21-F380-4980-B72C-F14488369333}"
80+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.Serialization.MemoryPack", "serialization\EasyCaching.Serialization.MemoryPack\EasyCaching.Serialization.MemoryPack.csproj", "{EEF22C21-F380-4980-B72C-F14488369333}"
81+
EndProject
82+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Demo.Locks", "sample\EasyCaching.Demo.Locks\EasyCaching.Demo.Locks.csproj", "{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}"
8083
EndProject
8184
Global
8285
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -208,6 +211,10 @@ Global
208211
{EEF22C21-F380-4980-B72C-F14488369333}.Debug|Any CPU.Build.0 = Debug|Any CPU
209212
{EEF22C21-F380-4980-B72C-F14488369333}.Release|Any CPU.ActiveCfg = Release|Any CPU
210213
{EEF22C21-F380-4980-B72C-F14488369333}.Release|Any CPU.Build.0 = Release|Any CPU
214+
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
215+
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}.Debug|Any CPU.Build.0 = Debug|Any CPU
216+
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}.Release|Any CPU.ActiveCfg = Release|Any CPU
217+
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}.Release|Any CPU.Build.0 = Release|Any CPU
211218
EndGlobalSection
212219
GlobalSection(SolutionProperties) = preSolution
213220
HideSolutionNode = FALSE
@@ -244,6 +251,7 @@ Global
244251
{5E488583-391E-4E15-83C1-7301B4FE79AE} = {B337509B-75F9-4851-821F-9BBE87C4E4BC}
245252
{7191E567-38DF-4879-82E1-73EC618AFCAC} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
246253
{EEF22C21-F380-4980-B72C-F14488369333} = {15070C49-A507-4844-BCFE-D319CFBC9A63}
254+
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98} = {F88D727A-9F9C-43D9-90B1-D4A02BF8BC98}
247255
EndGlobalSection
248256
GlobalSection(ExtensibilityGlobals) = postSolution
249257
SolutionGuid = {63A57886-054B-476C-AAE1-8D7C8917682E}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
namespace EasyCaching.Demo.Locks.Controllers;
2+
3+
[Route("api/[controller]")]
4+
public class LocksController : Controller
5+
{
6+
private readonly IDistributedLockFactory _distributedLockFactory;
7+
8+
public LocksController(IDistributedLockFactory distributedLockFactory)
9+
{
10+
_distributedLockFactory = distributedLockFactory;
11+
}
12+
13+
[HttpPost("distributed-locking")]
14+
public async Task DistributedLockingOperation(int millisecondsTimeout)
15+
{
16+
using var distributedLock = _distributedLockFactory.CreateLock("DefaultRedis", "YourKey");
17+
18+
try
19+
{
20+
if (await distributedLock.LockAsync(millisecondsTimeout))
21+
{
22+
// Simulate operation
23+
Thread.Sleep(2000);
24+
}
25+
else
26+
{
27+
// Proper error
28+
}
29+
}
30+
catch (Exception ex)
31+
{
32+
// log error
33+
throw;
34+
}
35+
finally
36+
{
37+
// release lock at the end
38+
await distributedLock.ReleaseAsync();
39+
}
40+
}
41+
42+
[HttpPost("memory-locking")]
43+
public async Task MemoryLockingOperation(int millisecondsTimeout)
44+
{
45+
using var memoryLock = _distributedLockFactory.CreateLock("DefaultInMemory", "YourKey");
46+
47+
try
48+
{
49+
if (await memoryLock.LockAsync(millisecondsTimeout))
50+
{
51+
// Simulate operation
52+
Thread.Sleep(2000);
53+
}
54+
else
55+
{
56+
// Proper error
57+
}
58+
}
59+
catch (Exception ex)
60+
{
61+
// log error
62+
throw;
63+
}
64+
finally
65+
{
66+
// release lock at the end
67+
await memoryLock.ReleaseAsync();
68+
}
69+
}
70+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
11+
12+
<ProjectReference Include="..\..\bus\EasyCaching.Bus.Redis\EasyCaching.Bus.Redis.csproj" />
13+
<ProjectReference Include="..\..\interceptor\EasyCaching.Interceptor.AspectCore\EasyCaching.Interceptor.AspectCore.csproj" />
14+
<ProjectReference Include="..\..\serialization\EasyCaching.Serialization.Json\EasyCaching.Serialization.Json.csproj" />
15+
<ProjectReference Include="..\..\src\EasyCaching.HybridCache\EasyCaching.HybridCache.csproj" />
16+
<ProjectReference Include="..\..\src\EasyCaching.InMemory\EasyCaching.InMemory.csproj" />
17+
<ProjectReference Include="..\..\src\EasyCaching.Redis\EasyCaching.Redis.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
global using EasyCaching.Core.Configurations;
2+
global using EasyCaching.Core.DistributedLock;
3+
global using Microsoft.AspNetCore.Mvc;
4+
global using EasyCaching.Redis.DistributedLock;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
// Add services to the container.
4+
5+
builder.Services.AddControllers();
6+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
7+
builder.Services.AddEndpointsApiExplorer();
8+
builder.Services.AddSwaggerGen();
9+
10+
builder.Services.AddEasyCaching(option =>
11+
{
12+
//use memory cache
13+
option.UseInMemory()
14+
.UseMemoryLock(); // use memory lock
15+
16+
//use redis cache
17+
option.UseRedis(config =>
18+
{
19+
config.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6379));
20+
config.DBConfig.SyncTimeout = 10000;
21+
config.DBConfig.AsyncTimeout = 10000;
22+
config.SerializerName = "NewtonsoftJson";
23+
})
24+
.WithJson()//with josn serialization
25+
.UseRedisLock(); // use distributed lock
26+
});
27+
28+
#region How Inject Distributed and Memory lock
29+
30+
// inject to use distributed lock
31+
builder.Services.AddSingleton<IDistributedLockFactory, RedisLockFactory>();
32+
33+
// inject to use memory lock
34+
builder.Services.AddSingleton<IDistributedLockFactory, MemoryLockFactory>();
35+
36+
#endregion
37+
38+
var app = builder.Build();
39+
40+
// Configure the HTTP request pipeline.
41+
if (app.Environment.IsDevelopment())
42+
{
43+
app.UseSwagger();
44+
app.UseSwaggerUI();
45+
}
46+
47+
app.UseHttpsRedirection();
48+
49+
app.UseAuthorization();
50+
51+
app.MapControllers();
52+
53+
app.Run();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)