Skip to content

Commit

Permalink
Merge pull request #489 from ErfanMahmoudi/feat/add-locks-sample
Browse files Browse the repository at this point in the history
Add locks sample
  • Loading branch information
Memoyu committed Sep 19, 2023
2 parents c74ad32 + 02c5166 commit 930d9f0
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 4 deletions.
16 changes: 12 additions & 4 deletions EasyCaching.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Microsoft Visual Studio Solution File, Format Version 12.00

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32616.157
MinimumVisualStudioVersion = 10.0.40219.1
Expand Down Expand Up @@ -72,11 +73,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.Serialization.S
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.Bus.ConfluentKafka", "bus\EasyCaching.Bus.ConfluentKafka\EasyCaching.Bus.ConfluentKafka.csproj", "{F7FBADEB-D766-4595-949A-07104B52692C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Bus.Zookeeper", "bus\EasyCaching.Bus.Zookeeper\EasyCaching.Bus.Zookeeper.csproj", "{5E488583-391E-4E15-83C1-7301B4FE79AE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "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}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.FasterKv", "src\EasyCaching.FasterKv\EasyCaching.FasterKv.csproj", "{7191E567-38DF-4879-82E1-73EC618AFCAC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Serialization.MemoryPack", "serialization\EasyCaching.Serialization.MemoryPack\EasyCaching.Serialization.MemoryPack.csproj", "{EEF22C21-F380-4980-B72C-F14488369333}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.Serialization.MemoryPack", "serialization\EasyCaching.Serialization.MemoryPack\EasyCaching.Serialization.MemoryPack.csproj", "{EEF22C21-F380-4980-B72C-F14488369333}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Demo.Locks", "sample\EasyCaching.Demo.Locks\EasyCaching.Demo.Locks.csproj", "{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -208,6 +211,10 @@ Global
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -244,6 +251,7 @@ Global
{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}
{9B15A0A0-BD6B-40B0-90D4-848BC3E4AF98} = {F88D727A-9F9C-43D9-90B1-D4A02BF8BC98}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {63A57886-054B-476C-AAE1-8D7C8917682E}
Expand Down
70 changes: 70 additions & 0 deletions sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
namespace EasyCaching.Demo.Locks.Controllers;

[Route("api/[controller]")]
public class LocksController : Controller
{
private readonly IDistributedLockFactory _distributedLockFactory;

public LocksController(IDistributedLockFactory distributedLockFactory)
{
_distributedLockFactory = distributedLockFactory;
}

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

try
{
if (await distributedLock.LockAsync(millisecondsTimeout))
{
// Simulate operation
Thread.Sleep(2000);
}
else
{
// Proper error
}
}
catch (Exception ex)

Check warning on line 30 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs

View workflow job for this annotation

GitHub Actions / build and test on ubuntu-latest

The variable 'ex' is declared but never used

Check warning on line 30 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs

View workflow job for this annotation

GitHub Actions / build and test on ubuntu-latest

The variable 'ex' is declared but never used

Check warning on line 30 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs

View workflow job for this annotation

GitHub Actions / build on windows-latest

The variable 'ex' is declared but never used

Check warning on line 30 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs

View workflow job for this annotation

GitHub Actions / build on windows-latest

The variable 'ex' is declared but never used

Check warning on line 30 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs

View workflow job for this annotation

GitHub Actions / Build and upload artifact

The variable 'ex' is declared but never used

Check warning on line 30 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs

View workflow job for this annotation

GitHub Actions / Build and upload artifact

The variable 'ex' is declared but never used
{
// log error
throw;
}
finally
{
// release lock at the end
await distributedLock.ReleaseAsync();
}
}

[HttpPost("memory-locking")]
public async Task MemoryLockingOperation(int millisecondsTimeout)
{
using var memoryLock = _distributedLockFactory.CreateLock("DefaultInMemory", "YourKey");

try
{
if (await memoryLock.LockAsync(millisecondsTimeout))
{
// Simulate operation
Thread.Sleep(2000);
}
else
{
// Proper error
}
}
catch (Exception ex)

Check warning on line 59 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs

View workflow job for this annotation

GitHub Actions / build and test on ubuntu-latest

The variable 'ex' is declared but never used

Check warning on line 59 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs

View workflow job for this annotation

GitHub Actions / build and test on ubuntu-latest

The variable 'ex' is declared but never used

Check warning on line 59 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs

View workflow job for this annotation

GitHub Actions / build on windows-latest

The variable 'ex' is declared but never used

Check warning on line 59 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs

View workflow job for this annotation

GitHub Actions / build on windows-latest

The variable 'ex' is declared but never used

Check warning on line 59 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs

View workflow job for this annotation

GitHub Actions / Build and upload artifact

The variable 'ex' is declared but never used

Check warning on line 59 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs

View workflow job for this annotation

GitHub Actions / Build and upload artifact

The variable 'ex' is declared but never used
{
// log error
throw;
}
finally
{
// release lock at the end
await memoryLock.ReleaseAsync();
}
}
}
20 changes: 20 additions & 0 deletions sample/EasyCaching.Demo.Locks/EasyCaching.Demo.Locks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />

<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.HybridCache\EasyCaching.HybridCache.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.InMemory\EasyCaching.InMemory.csproj" />
<ProjectReference Include="..\..\src\EasyCaching.Redis\EasyCaching.Redis.csproj" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions sample/EasyCaching.Demo.Locks/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global using EasyCaching.Core.Configurations;
global using EasyCaching.Core.DistributedLock;
global using Microsoft.AspNetCore.Mvc;
global using EasyCaching.Redis.DistributedLock;
53 changes: 53 additions & 0 deletions sample/EasyCaching.Demo.Locks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddEasyCaching(option =>
{
//use memory cache
option.UseInMemory()
.UseMemoryLock(); // use memory lock
//use redis cache
option.UseRedis(config =>
{
config.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6379));
config.DBConfig.SyncTimeout = 10000;
config.DBConfig.AsyncTimeout = 10000;
config.SerializerName = "NewtonsoftJson";
})
.WithJson()//with josn serialization
.UseRedisLock(); // use distributed lock
});

#region How Inject Distributed and Memory lock

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

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

#endregion

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
8 changes: 8 additions & 0 deletions sample/EasyCaching.Demo.Locks/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions sample/EasyCaching.Demo.Locks/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

0 comments on commit 930d9f0

Please sign in to comment.