From 02c5166f0fe680b1e1b40b52d5900f519368d3aa Mon Sep 17 00:00:00 2001 From: "erfanmahmoudi98@outlook.com" Date: Sun, 17 Sep 2023 11:30:16 +0330 Subject: [PATCH] Add locks sample --- EasyCaching.sln | 16 +++-- .../Controllers/LocksController.cs | 70 +++++++++++++++++++ .../EasyCaching.Demo.Locks.csproj | 20 ++++++ sample/EasyCaching.Demo.Locks/GlobalUsings.cs | 4 ++ sample/EasyCaching.Demo.Locks/Program.cs | 53 ++++++++++++++ .../appsettings.Development.json | 8 +++ .../EasyCaching.Demo.Locks/appsettings.json | 9 +++ 7 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs create mode 100644 sample/EasyCaching.Demo.Locks/EasyCaching.Demo.Locks.csproj create mode 100644 sample/EasyCaching.Demo.Locks/GlobalUsings.cs create mode 100644 sample/EasyCaching.Demo.Locks/Program.cs create mode 100644 sample/EasyCaching.Demo.Locks/appsettings.Development.json create mode 100644 sample/EasyCaching.Demo.Locks/appsettings.json diff --git a/EasyCaching.sln b/EasyCaching.sln index 7d07ad0a..a2d217c4 100644 --- a/EasyCaching.sln +++ b/EasyCaching.sln @@ -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 @@ -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 @@ -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 @@ -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} diff --git a/sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs b/sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs new file mode 100644 index 00000000..c7cf7530 --- /dev/null +++ b/sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs @@ -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(); + } + } +} diff --git a/sample/EasyCaching.Demo.Locks/EasyCaching.Demo.Locks.csproj b/sample/EasyCaching.Demo.Locks/EasyCaching.Demo.Locks.csproj new file mode 100644 index 00000000..dc06abc5 --- /dev/null +++ b/sample/EasyCaching.Demo.Locks/EasyCaching.Demo.Locks.csproj @@ -0,0 +1,20 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + + diff --git a/sample/EasyCaching.Demo.Locks/GlobalUsings.cs b/sample/EasyCaching.Demo.Locks/GlobalUsings.cs new file mode 100644 index 00000000..838d0c76 --- /dev/null +++ b/sample/EasyCaching.Demo.Locks/GlobalUsings.cs @@ -0,0 +1,4 @@ +global using EasyCaching.Core.Configurations; +global using EasyCaching.Core.DistributedLock; +global using Microsoft.AspNetCore.Mvc; +global using EasyCaching.Redis.DistributedLock; \ No newline at end of file diff --git a/sample/EasyCaching.Demo.Locks/Program.cs b/sample/EasyCaching.Demo.Locks/Program.cs new file mode 100644 index 00000000..c2d00cdb --- /dev/null +++ b/sample/EasyCaching.Demo.Locks/Program.cs @@ -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(); + +// inject to use memory lock +builder.Services.AddSingleton(); + +#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(); diff --git a/sample/EasyCaching.Demo.Locks/appsettings.Development.json b/sample/EasyCaching.Demo.Locks/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/sample/EasyCaching.Demo.Locks/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/sample/EasyCaching.Demo.Locks/appsettings.json b/sample/EasyCaching.Demo.Locks/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/sample/EasyCaching.Demo.Locks/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}