-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c74ad32
commit 02c5166
Showing
7 changed files
with
176 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
// 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) | ||
{ | ||
// log error | ||
throw; | ||
} | ||
finally | ||
{ | ||
// release lock at the end | ||
await memoryLock.ReleaseAsync(); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
sample/EasyCaching.Demo.Locks/EasyCaching.Demo.Locks.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |