Heily.Abp.DistributedLock 0.0.14

There is a newer version of this package available.
See the version list below for details.
dotnet add package Heily.Abp.DistributedLock --version 0.0.14
NuGet\Install-Package Heily.Abp.DistributedLock -Version 0.0.14
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Heily.Abp.DistributedLock" Version="0.0.14" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Heily.Abp.DistributedLock --version 0.0.14
#r "nuget: Heily.Abp.DistributedLock, 0.0.14"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Heily.Abp.DistributedLock as a Cake Addin
#addin nuget:?package=Heily.Abp.DistributedLock&version=0.0.14

// Install Heily.Abp.DistributedLock as a Cake Tool
#tool nuget:?package=Heily.Abp.DistributedLock&version=0.0.14

公司项目需要在现有abp框架的基础上自行扩展一套分布式锁模块出来,github没翻到类似的项目,自己照着abp的模块基于RedLockNet 封了一套,基本满足我目前所在项目的需求,这里简单记录下用法

1.在Host项目Module上加入模块依赖特性

[DependsOn(typeof(RedLockModule))]

2.StartUp初始化

在StartUp 的ConfigureServices方法中对redlock做初始化操作

services.AddRedLock(config =>
            {
                config.AddEndPoint(new RedLockEndPoint(new List<EndPoint>
                {
                    new DnsEndPoint("192.168.23.101", 6379),
                    new DnsEndPoint("192.168.23.101", 6380),
                }));
            });

3.Module初始化

也可以直接在Host项目Module的模块方法PreInitialize中对Redlock做初始化操作

if (!Configuration.Modules.RedLockConfiguration().IsInit)
            {
                Configuration.Modules.RedLockConfiguration().AddEndPoint(new RedLockEndPoint(new List<EndPoint>
                {
                    new DnsEndPoint("192.168.23.101", 6379),
                    new DnsEndPoint("192.168.23.101", 6380),
                }));

                Configuration.Modules.RedLockConfiguration().Expiry = 130;
                Configuration.Modules.RedLockConfiguration().Disabled = true;
                Configuration.Modules.RedLockConfiguration().Wait = 50;
            }

注意,2,3两条只能任选其一进行初始化,否则应用将无法启动

4.全方法加锁

基于winsor拦截器实现 该用法只需要在要加锁的方法前标记特性 Lock 即可生效,默认锁定的资源名称为当前方法名,支持自定义锁定资源key的名称和过期时间,支持自定义未获取锁的错误提示信息,默认未获取锁不会执行方法

// 领域层方法
public class MyCustomerDomainService : DomainService
    {
        private readonly ILockManager _lockManager;

        public MyCustomerDomainService(ILockManager lockManager)
        {
            _lockManager = lockManager;
        }

        [Lock]
        public virtual void Lock_DomainService_Test()
        {
            using (var varLock = _lockManager.CreateLock().Lock(nameof(Lock_DomainService_Test), null))
            {
                Assert.False(varLock.IsAcquired);
            }
        }
    }
// 应用层方法
public class MyCustomerAppService : ApplicationService
    {
        private readonly ILockManager _lockManager;

        public MyCustomerAppService(ILockManager lockManager)
        {
            _lockManager = lockManager;
        }

        [Lock]
        public virtual void Lock_ApplicationService_Test()
        {

            using (var varLock = _lockManager.CreateLock().Lock(nameof(Lock_ApplicationService_Test), null))
            {
                Assert.False(varLock.IsAcquired);
            }
        }
    }

5.自定义加锁逻辑

自定义加锁,只需要注入加锁服务对象ILockManager,使用对象的CreateLock方法或者CreateLockAsync创建加锁对象,调用其Lock方法,即可完成加锁过程

       private readonly ILockManager _redLockManager;
       public RedLockTest(ILockManager lockManager)
       {
           _redLockManager = lockManager;
       }
       public void LockTest()
       {
           using (var redLock = _redLockManager.CreateLock().Lock(nameof(LockTest), null))
               {
                   if(redLock.IsAcquired){
                       // 这里就是你的业务代码
                   }
               }
       }
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Heily.Abp.DistributedLock:

Package Downloads
Heily.Abp.AspNetCore

abp,aspnetcore 模块

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.7.4 666 2/10/2021
0.7.3 555 12/14/2020
0.7.2 640 8/8/2020
0.7.1 671 5/31/2020
0.7.0 730 5/30/2020
0.6.5 649 5/21/2020
0.6.4 676 4/30/2020
0.6.3 642 4/30/2020
0.6.2 635 4/27/2020
0.6.1 628 4/27/2020
0.6.0 599 4/25/2020
0.5.5 691 3/29/2020
0.5.4 759 3/29/2020
0.5.3 798 3/28/2020
0.5.2 716 3/28/2020
0.5.1 718 3/27/2020
0.5.0 666 3/27/2020
0.4.1 765 2/4/2020
0.4.0 712 2/4/2020
0.3.0 702 2/1/2020
0.2.9 1,041 11/6/2019
0.2.8 828 10/17/2019
0.2.7 759 10/10/2019
0.2.6 697 10/10/2019
0.2.5 777 9/28/2019
0.2.4 726 9/28/2019
0.2.3 782 9/28/2019
0.2.2 730 9/28/2019
0.2.1 783 9/8/2019
0.2.0 784 9/7/2019
0.1.3 770 8/9/2019
0.1.2 787 7/14/2019
0.1.1 833 6/23/2019
0.1.0 699 6/22/2019
0.0.19 769 6/22/2019
0.0.18 564 6/18/2019
0.0.17 539 6/13/2019
0.0.16 594 6/11/2019
0.0.15 583 6/11/2019
0.0.14 581 6/8/2019
0.0.13 614 6/7/2019
0.0.12 606 6/7/2019