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 753 2/10/2021
0.7.3 601 12/14/2020
0.7.2 692 8/8/2020
0.7.1 713 5/31/2020
0.7.0 783 5/30/2020
0.6.5 710 5/21/2020
0.6.4 712 4/30/2020
0.6.3 685 4/30/2020
0.6.2 680 4/27/2020
0.6.1 672 4/27/2020
0.6.0 645 4/25/2020
0.5.5 731 3/29/2020
0.5.4 818 3/29/2020
0.5.3 856 3/28/2020
0.5.2 754 3/28/2020
0.5.1 758 3/27/2020
0.5.0 708 3/27/2020
0.4.1 822 2/4/2020
0.4.0 758 2/4/2020
0.3.0 752 2/1/2020
0.2.9 1,096 11/6/2019
0.2.8 861 10/17/2019
0.2.7 813 10/10/2019
0.2.6 767 10/10/2019
0.2.5 845 9/28/2019
0.2.4 778 9/28/2019
0.2.3 834 9/28/2019
0.2.2 780 9/28/2019
0.2.1 828 9/8/2019
0.2.0 819 9/7/2019
0.1.3 825 8/9/2019
0.1.2 813 7/14/2019
0.1.1 882 6/23/2019
0.1.0 747 6/22/2019
0.0.19 829 6/22/2019
0.0.18 600 6/18/2019
0.0.17 571 6/13/2019
0.0.16 624 6/11/2019
0.0.15 615 6/11/2019
0.0.14 612 6/8/2019
0.0.13 635 6/7/2019
0.0.12 638 6/7/2019