EasilyNET.MongoDistributedLock
1.7.4
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package EasilyNET.MongoDistributedLock --version 1.7.4
NuGet\Install-Package EasilyNET.MongoDistributedLock -Version 1.7.4
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="1.7.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasilyNET.MongoDistributedLock --version 1.7.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EasilyNET.MongoDistributedLock, 1.7.4"
#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 EasilyNET.MongoDistributedLock as a Cake Addin #addin nuget:?package=EasilyNET.MongoDistributedLock&version=1.7.4 // Install EasilyNET.MongoDistributedLock as a Cake Tool #tool nuget:?package=EasilyNET.MongoDistributedLock&version=1.7.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
EasilyNET.MongoDistributedLock
- 基于 GitHub 开源项目
使用方法
// 使用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");
// 获取锁
var mongoLock = new DistributedLock(_locks, _signals, ObjectId.GenerateNewId());
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);
}
- 注意事项和工作原理
- 当您尝试获取锁时,具有指定 lockId 的文档将添加到锁集合中,或者更新(如果存在).
- 释放锁时,将更新文档,并将新文档添加到信号上限集合中
- 当锁定正在等待时,将使用服务器端等待的可尾游标.详细信息
- 生存期是锁有效的时间段.在此时间之后,锁将自动“释放”,并且可以再次获取.它可以防止死锁.
- 不要使用长时间的超时,这可能会引发 MongoDB 驱动程序的异常.正常超时不超过 1-2 分钟!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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 is compatible. 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 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- MongoDB.Driver (>= 2.20.0)
-
net7.0
- MongoDB.Driver (>= 2.20.0)
-
net8.0
- MongoDB.Driver (>= 2.20.0)
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.1107.140 | 32 | 11/7/2024 |
3.24.1107.54 | 30 | 11/7/2024 |
3.24.1107.34 | 31 | 11/7/2024 |
3.24.1105.111 | 77 | 11/5/2024 |
3.24.1103.31 | 79 | 11/2/2024 |
3.24.1103 | 75 | 11/2/2024 |
3.24.1031.135 | 74 | 10/31/2024 |
3.24.1031.112 | 72 | 10/31/2024 |
3.24.1031.104 | 71 | 10/31/2024 |
3.24.1029.142 | 68 | 10/29/2024 |
3.24.1025.30 | 76 | 10/24/2024 |
3.24.1022.142 | 56 | 10/22/2024 |
3.24.1018.204 | 131 | 10/18/2024 |
3.24.1018.175 | 129 | 10/18/2024 |
3.24.1018.166 | 124 | 10/18/2024 |
3.24.1018.93 | 127 | 10/18/2024 |
3.24.1017.42 | 81 | 10/16/2024 |
3.24.1016.161 | 81 | 10/16/2024 |
3.24.1015.231 | 76 | 10/15/2024 |
3.24.1015.14 | 83 | 10/14/2024 |
3.24.1012.114 | 75 | 10/12/2024 |
3.24.1009.115 | 83 | 10/9/2024 |
3.24.1008.160 | 72 | 10/8/2024 |
3.24.1008.133 | 81 | 10/8/2024 |
3.24.1007.185 | 77 | 10/7/2024 |
3.24.1003.33 | 89 | 10/2/2024 |
3.24.1002.162 | 92 | 10/2/2024 |
3.24.929.143 | 92 | 9/29/2024 |
3.24.929.141 | 85 | 9/29/2024 |
3.24.929.131 | 86 | 9/29/2024 |
3.24.929.122 | 75 | 9/29/2024 |
3.24.926.184 | 90 | 9/26/2024 |
3.24.926.182 | 88 | 9/26/2024 |
3.24.926.175 | 87 | 9/26/2024 |
3.24.924.160 | 76 | 9/24/2024 |
3.24.924.133 | 83 | 9/24/2024 |
3.24.924.124 | 77 | 9/24/2024 |
3.24.924.10 | 90 | 9/23/2024 |
3.24.924.1 | 82 | 9/23/2024 |
3.24.923.234 | 76 | 9/23/2024 |
3.24.923.232 | 80 | 9/23/2024 |
3.24.923.155 | 89 | 9/23/2024 |
3.24.919.92 | 85 | 9/19/2024 |
3.24.914.125 | 115 | 9/14/2024 |
3.24.914.115 | 101 | 9/14/2024 |
3.24.914.111 | 110 | 9/14/2024 |
3.24.911.95 | 103 | 9/11/2024 |
3.24.908.215 | 92 | 9/8/2024 |
3.24.904.200 | 98 | 9/4/2024 |
3.24.828.163 | 124 | 8/28/2024 |
3.24.820.173 | 116 | 8/20/2024 |
3.24.814.92 | 115 | 8/14/2024 |
3.24.812.115 | 117 | 8/12/2024 |
3.24.802.100 | 80 | 8/2/2024 |
3.24.801.162 | 96 | 8/1/2024 |
3.24.801.160 | 91 | 8/1/2024 |
3.24.801.155 | 90 | 8/1/2024 |
3.24.730.164 | 55 | 7/30/2024 |
3.24.730.91 | 62 | 7/30/2024 |
3.24.724.91 | 84 | 7/24/2024 |
3.24.718.105 | 112 | 7/18/2024 |
3.24.716.95 | 92 | 7/16/2024 |
3.24.712.94 | 87 | 7/12/2024 |
3.24.710.14 | 109 | 7/9/2024 |
3.24.709.105 | 94 | 7/9/2024 |
3.24.704.94 | 113 | 7/4/2024 |
3.24.701.90 | 108 | 7/1/2024 |
3.24.628.114 | 104 | 6/28/2024 |
3.24.627.145 | 92 | 6/27/2024 |
3.24.620.160 | 102 | 6/20/2024 |
3.24.613.115 | 93 | 6/13/2024 |
3.24.612.95 | 95 | 6/12/2024 |
3.24.528.90 | 101 | 5/28/2024 |
3.24.522.84 | 105 | 5/22/2024 |
3.24.512.213 | 96 | 5/12/2024 |
3.24.508.112 | 122 | 5/8/2024 |
2.2024.428.71 | 123 | 4/28/2024 |
2.2024.427.1128 | 120 | 4/27/2024 |
2.2.72 | 115 | 4/14/2024 |
2.2.71 | 105 | 4/12/2024 |
2.2.8 | 104 | 4/26/2024 |
2.2.6 | 107 | 4/10/2024 |
2.2.5 | 103 | 3/26/2024 |
2.2.4 | 110 | 3/25/2024 |
2.2.3 | 104 | 3/24/2024 |
2.2.2 | 126 | 3/21/2024 |
2.2.1 | 134 | 3/20/2024 |
2.2.0 | 131 | 3/13/2024 |
2.1.9 | 128 | 2/21/2024 |
2.1.8 | 108 | 2/18/2024 |
2.1.7 | 128 | 2/16/2024 |
2.1.6 | 109 | 2/14/2024 |
2.1.5 | 112 | 2/14/2024 |
2.1.4 | 133 | 2/9/2024 |
2.1.3 | 134 | 2/8/2024 |
2.1.2 | 116 | 2/5/2024 |
2.1.1.2 | 189 | 12/26/2023 |
2.1.1.1 | 132 | 12/26/2023 |
2.1.1 | 135 | 12/25/2023 |
2.1.0 | 149 | 12/17/2023 |
2.0.11 | 154 | 12/6/2023 |
2.0.1 | 177 | 11/15/2023 |
2.0.0 | 164 | 11/14/2023 |
1.9.1 | 152 | 11/1/2023 |
1.9.0 | 136 | 10/19/2023 |
1.9.0-preview2 | 111 | 10/12/2023 |
1.9.0-preview1 | 132 | 10/12/2023 |
1.8.9 | 151 | 10/11/2023 |
1.8.8 | 160 | 10/11/2023 |
1.8.7-rc2 | 103 | 9/21/2023 |
1.8.7-rc1 | 115 | 9/12/2023 |
1.8.6 | 165 | 8/31/2023 |
1.8.5 | 156 | 8/25/2023 |
1.8.4 | 154 | 8/24/2023 |
1.8.3 | 168 | 8/23/2023 |
1.8.2 | 160 | 8/22/2023 |
1.8.1 | 145 | 8/18/2023 |
1.8.0 | 172 | 8/15/2023 |
1.7.9 | 169 | 8/11/2023 |
1.7.8 | 171 | 8/11/2023 |
1.7.7 | 166 | 8/10/2023 |
1.7.6 | 157 | 8/9/2023 |
1.7.5 | 174 | 8/9/2023 |
1.7.4 | 175 | 8/3/2023 |
1.7.3 | 176 | 8/1/2023 |
1.7.2 | 174 | 7/31/2023 |
1.7.1 | 167 | 7/27/2023 |
1.7.0 | 186 | 7/27/2023 |