-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
Add locks sample
- Loading branch information
There are no files selected for viewing
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 GitHub Actions / build and test on ubuntu-latest
Check warning on line 30 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs GitHub Actions / build and test on ubuntu-latest
Check warning on line 30 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs GitHub Actions / build on windows-latest
Check warning on line 30 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs GitHub Actions / build on windows-latest
Check warning on line 30 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs GitHub Actions / Build and upload artifact
|
||
{ | ||
// 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 GitHub Actions / build and test on ubuntu-latest
Check warning on line 59 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs GitHub Actions / build and test on ubuntu-latest
Check warning on line 59 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs GitHub Actions / build on windows-latest
Check warning on line 59 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs GitHub Actions / build on windows-latest
Check warning on line 59 in sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs GitHub Actions / Build and upload artifact
|
||
{ | ||
// log error | ||
throw; | ||
} | ||
finally | ||
{ | ||
// release lock at the end | ||
await memoryLock.ReleaseAsync(); | ||
} | ||
} | ||
} |
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> |
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; |
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(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |