EasilyNET.MongoDistributedLock 2.2024.427.1128

There is a newer version of this package available.
See the version list below for details.
dotnet add package EasilyNET.MongoDistributedLock --version 2.2024.427.1128
                    
NuGet\Install-Package EasilyNET.MongoDistributedLock -Version 2.2024.427.1128
                    
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="EasilyNET.MongoDistributedLock" Version="2.2024.427.1128" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EasilyNET.MongoDistributedLock" Version="2.2024.427.1128" />
                    
Directory.Packages.props
<PackageReference Include="EasilyNET.MongoDistributedLock" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EasilyNET.MongoDistributedLock --version 2.2024.427.1128
                    
#r "nuget: EasilyNET.MongoDistributedLock, 2.2024.427.1128"
                    
#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.
#addin nuget:?package=EasilyNET.MongoDistributedLock&version=2.2024.427.1128
                    
Install EasilyNET.MongoDistributedLock as a Cake Addin
#tool nuget:?package=EasilyNET.MongoDistributedLock&version=2.2024.427.1128
                    
Install EasilyNET.MongoDistributedLock as a Cake Tool
EasilyNET.MongoDistributedLock
使用方法
// 使用MongoDB驱动创建一个链接
var setting = new MongoClientSettings
{
    Servers = new List<MongoServerAddress> { new("127.0.0.1", 27018) },
    Credential = MongoCredential.CreateCredential("admin", "guest", "guest"),
    LinqProvider = LinqProvider.V3
};
var client = new MongoClient(setting);
var db = client.GetDatabase("locks");
try
{
    // 由于使用到一些特性.需要将这个集合设置成 上限集合
    db.CreateCollection("release.signal", new()
    {
        // 这个数量理论上可以决定同时系统能有多少个锁.
        MaxDocuments = 100,
        MaxSize = 4096,
        Capped = true
    });
}
catch
{
    // ignored
}
IMongoCollection<LockAcquire> _locks = db.GetCollection<LockAcquire>("lock.acquire");
IMongoCollection<ReleaseSignal> _signals = db.GetCollection<ReleaseSignal>("release.signal");

// 获取锁
// 这里使用一个随机的ID作为锁ID,相当于其他锁中的Key.用来区分不同的业务的锁,也可以将不同的业务类型放到MongoDB中存起来,然后再使用的时候再取获取这个id
const string lockId = "64d44afde4473b85a177084c";

var mongoLock = DistributedLock.GenerateNew(_locks, _signals, ObjectId.Parse(lockId));
var acq = await mongoLock.AcquireAsync(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(0));

// 释放锁 也可以等超时释放
await mongoLock.ReleaseAsync(acq1);
  • 实际用的时候大概是这样.
try
{
    if (acq.Acquired)
    {
        // 关键部分,它不能一次由任何服务器上的多个线程执行
        // ...
        // ...
    }
    else
    {
        // 超时!也许另一个线程没有释放锁...我们可以再试一次或抛出例外
    }
}
finally
{
    // 如果(acq.Acquired)无需手动操作
    await mongoLock.ReleaseAsync(acq);
}
  • 注意事项和工作原理
  1. 当您尝试获取锁时,具有指定 lockId 的文档将添加到锁集合中,或者更新(如果存在).
  2. 释放锁时,将更新文档,并将新文档添加到信号上限集合中

当锁定正在等待时,将使用服务器端等待的可尾游标.详细信息

  1. 生存期是锁有效的时间段.在此时间之后,锁将自动“释放”,并且可以再次获取.它可以防止死锁.
  2. 不要使用长时间的超时,这可能会引发 MongoDB 驱动程序的异常.正常超时不超过 1-2 分钟!
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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 EasilyNET.MongoDistributedLock:

Package Downloads
EasilyNET.MongoDistributedLock.AspNetCore

MongoDB分布式锁方案

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.24.1113.100 128 7 months ago
3.24.1112.125 109 7 months ago
3.24.1107.140 107 7 months ago
3.24.1107.54 105 7 months ago
3.24.1107.34 116 7 months ago
3.24.1105.111 115 7 months ago
3.24.1103.31 121 7 months ago
3.24.1103 117 7 months ago
3.24.1031.135 111 7 months ago
3.24.1031.112 111 7 months ago
3.24.1031.104 109 7 months ago
3.24.1029.142 109 7 months ago
3.24.1025.30 116 7 months ago
3.24.1022.142 98 7 months ago
3.24.1018.204 163 8 months ago
3.24.1018.175 162 8 months ago
3.24.1018.166 153 8 months ago
3.24.1018.93 160 8 months ago
3.24.1017.42 112 8 months ago
3.24.1016.161 110 8 months ago
3.24.1015.231 106 8 months ago
3.24.1015.14 112 8 months ago
3.24.1012.114 109 8 months ago
3.24.1009.115 114 8 months ago
3.24.1008.160 104 8 months ago
3.24.1008.133 111 8 months ago
3.24.1007.185 107 8 months ago
3.24.1003.33 119 8 months ago
3.24.1002.162 122 8 months ago
3.24.929.143 120 8 months ago
3.24.929.141 115 8 months ago
3.24.929.131 114 8 months ago
3.24.929.122 103 8 months ago
3.24.926.184 119 8 months ago
3.24.926.182 118 8 months ago
3.24.926.175 116 8 months ago
3.24.924.160 108 8 months ago
3.24.924.133 112 8 months ago
3.24.924.124 107 8 months ago
3.24.924.10 120 8 months ago
3.24.924.1 113 8 months ago
3.24.923.234 102 8 months ago
3.24.923.232 111 8 months ago
3.24.923.155 119 8 months ago
3.24.919.92 113 9 months ago
3.24.914.125 142 9 months ago
3.24.914.115 128 9 months ago
3.24.914.111 136 9 months ago
3.24.911.95 127 9 months ago
3.24.908.215 117 9 months ago
3.24.904.200 122 9 months ago
3.24.828.163 148 9 months ago
3.24.820.173 143 10 months ago
3.24.814.92 146 10 months ago
3.24.812.115 144 10 months ago
3.24.802.100 108 8/2/2024
3.24.801.162 124 8/1/2024
3.24.801.160 119 8/1/2024
3.24.801.155 116 8/1/2024
3.24.730.164 86 7/30/2024
3.24.730.91 90 7/30/2024
3.24.724.91 110 7/24/2024
3.24.718.105 138 7/18/2024
3.24.716.95 119 7/16/2024
3.24.712.94 113 7/12/2024
3.24.710.14 135 7/9/2024
3.24.709.105 122 7/9/2024
3.24.704.94 140 7/4/2024
3.24.701.90 133 7/1/2024
3.24.628.114 131 6/28/2024
3.24.627.145 122 6/27/2024
3.24.620.160 129 6/20/2024
3.24.613.115 118 6/13/2024
3.24.612.95 120 6/12/2024
3.24.528.90 124 5/28/2024
3.24.522.84 131 5/22/2024
3.24.512.213 119 5/12/2024
3.24.508.112 146 5/8/2024
2.2024.428.71 146 4/28/2024
2.2024.427.1128 143 4/27/2024
2.2.72 140 4/14/2024
2.2.71 128 4/12/2024
2.2.8 127 4/26/2024
2.2.6 127 4/10/2024
2.2.5 125 3/26/2024
2.2.4 131 3/25/2024
2.2.3 127 3/24/2024
2.2.2 148 3/21/2024
2.2.1 162 3/20/2024
2.2.0 153 3/13/2024
2.1.9 154 2/21/2024
2.1.8 132 2/18/2024
2.1.7 155 2/16/2024
2.1.6 134 2/14/2024
2.1.5 137 2/14/2024
2.1.4 158 2/9/2024
2.1.3 161 2/8/2024
2.1.2 137 2/5/2024
2.1.1.2 208 12/26/2023
2.1.1.1 152 12/26/2023
2.1.1 156 12/25/2023
2.1.0 171 12/17/2023
2.0.11 165 12/6/2023
2.0.1 192 11/15/2023
2.0.0 175 11/14/2023
1.9.1 166 11/1/2023
1.9.0 149 10/19/2023
1.9.0-preview2 121 10/12/2023
1.9.0-preview1 141 10/12/2023
1.8.9 164 10/11/2023
1.8.8 172 10/11/2023
1.8.7-rc2 114 9/21/2023
1.8.7-rc1 125 9/12/2023
1.8.6 180 8/31/2023
1.8.5 171 8/25/2023
1.8.4 168 8/24/2023
1.8.3 200 8/23/2023
1.8.2 175 8/22/2023
1.8.1 169 8/18/2023
1.8.0 188 8/15/2023
1.7.9 185 8/11/2023
1.7.8 186 8/11/2023
1.7.7 183 8/10/2023
1.7.6 175 8/9/2023
1.7.5 189 8/9/2023
1.7.4 191 8/3/2023
1.7.3 193 8/1/2023
1.7.2 192 7/31/2023
1.7.1 184 7/27/2023
1.7.0 202 7/27/2023