From f3aa932b3e00ad73804453b30d88b22f3a993116 Mon Sep 17 00:00:00 2001 From: malik masis Date: Thu, 10 Mar 2022 14:17:12 +0300 Subject: [PATCH] Made changes on the document It will be continued enhancement --- ...ributed-Lock.md => Distributed-Locking.md} | 85 +++++++++---------- docs/en/docs-nav.json | 2 +- 2 files changed, 41 insertions(+), 46 deletions(-) rename docs/en/{Distributed-Lock.md => Distributed-Locking.md} (58%) diff --git a/docs/en/Distributed-Lock.md b/docs/en/Distributed-Locking.md similarity index 58% rename from docs/en/Distributed-Lock.md rename to docs/en/Distributed-Locking.md index ae1d5b9232..d79dff8674 100644 --- a/docs/en/Distributed-Lock.md +++ b/docs/en/Distributed-Locking.md @@ -1,23 +1,47 @@ -# Distributed Lock -Distributed locks are very useful to manage many processes that request the same object. -In a normal case, accessing the same object from different threads or services can corrupt the value of objects. +# Distributed Locking +Distributed locking is very useful to manage many processes that request the same object. +In a normal case, accessing the same object from different applications can corrupt the value of resources. In terms of accuracy, we may need to protect the value that's why a lock will be necessary. -On another hand, it will protect from more times triggered hence you will provide more efficiency. -To summarize, It's used to handle conflict between these requests. Once obtaining anyone it, the others need to wait till give up free. -# Providers -Distributed locks system provides an abstraction that can be implemented by any vendor/provider. +> To use in ABP, you should download the below NuGet package or can open any command apps and run the below command. +````powershell +abp add-package Volo.Abp.DistributedLocking +```` - * `MedallionAbpDistributedLock` -ABP depends on [DistributedLock.Core](https://www.nuget.org/packages/DistributedLock.Core) library which provides a distributed locking system for concurrency control in a distributed environment. There are [many distributed lock providers](https://github.com/madelson/DistributedLock#implementations) including Redis, SqlServer and ZooKeeper. You may use the one you want. Here, I will show the Redis provider. +````csharp +using Volo.Abp.DistributedLocking; +namespace AbpDemo +{ + public class MyService : ITransientDependency + { + private readonly IAbpDistributedLock _distributedLock; + public MyService(IAbpDistributedLock distributedLock) + { + _distributedLock = distributedLock; + } + + public async Task MyMethodAsync() + { + await using (var handle = await _distributedLock.TryAcquireAsync("NameOfLock")) + { + if (handle != null) + { + //your code + } + } + } + } +} +```` This provider contains a method named `TryAcquireAsync` and this method returns null if the lock could not be handled. -> `Name` is a mandatory field. It keeps the locked provider name. -> `Timeout` is set as default. If it fells deadlock and doesn't work properly you can use define the time to kill it. -> `CancellationToken` is set as default. It enables cooperative cancellation between threads, thread pool work items, or Task objects +`name` is mandatory. It keeps the locked provider name. This name should be unique, otherwise it will be seen as a different lock. +`timeout` is set as default. If it fells deadlock and doesn't work properly you can use define the time to kill it. +`cancellationToken` is set as default. It enables cooperative cancellation between threads, thread pool work items, or Task objects +ABP depends on [DistributedLock.Core](https://www.nuget.org/packages/DistributedLock.Core) library which provides a distributed locking system for concurrency control in a distributed environment. There are [many distributed lock providers](https://github.com/madelson/DistributedLock#implementations) including Redis, SqlServer and ZooKeeper. You may use the one you want. ABP implements the Redis provider for you and you may customize the others yourselves. -Also, you should add [DistributedLock.Redis](https://www.nuget.org/packages/DistributedLock.Redis) NuGet package to your project, then add the following code into the ConfigureService method of your ABP module class. +Firstly, you should add [DistributedLock.Redis](https://www.nuget.org/packages/DistributedLock.Redis) NuGet package to your project, then add the following code into the ConfigureService method of your ABP module class. ````csharp using Medallion.Threading; @@ -41,41 +65,12 @@ namespace AbpDemo } ```` -Also, you should add your Redis configuration in appsetting.json +Also, you should add your Redis configuration in appsetting.json. +You may change the structure of the JSON file but it must match with the above configuration code. ````json "Redis": { "Configuration": "127.0.0.1" } ```` -> To use in ABP, you should download the below NuGet package. -It already contains DistributedLocking.Abstractions and no need to download as well. -````powershell -abp add-package Volo.Abp.DistributedLocking -```` - -````csharp -using Volo.Abp.DistributedLocking; -namespace AbpDemo -{ - public class MyService : ITransientDependency - { - private readonly IAbpDistributedLock _distributedLock; - public MyService(IAbpDistributedLock distributedLock) - { - _distributedLock = distributedLock; - } - - public async Task MyMethodAsync() - { - await using (var handle = await _distributedLock.TryAcquireAsync("NameOfLock")) - { - if (handle != null) - { - //your code - } - } - } - } -} -```` \ No newline at end of file +As mentioned the above, this implemantition is for Redis and for more detail you can visit [the official site](https://github.com/madelson/DistributedLock#implementations). \ No newline at end of file diff --git a/docs/en/docs-nav.json b/docs/en/docs-nav.json index f039d02f1b..16edb1a7cc 100644 --- a/docs/en/docs-nav.json +++ b/docs/en/docs-nav.json @@ -302,7 +302,7 @@ }, { "text": "Distributed Lock", - "path": "Distributed-Lock.md" + "path": "Distributed-Locking.md" }, { "text": "Email Sending",