EasilyNET.MongoDistributedLock
1.7.7
The owner has unlisted this package.
This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package EasilyNET.MongoDistributedLock --version 1.7.7
NuGet\Install-Package EasilyNET.MongoDistributedLock -Version 1.7.7
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.7" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EasilyNET.MongoDistributedLock" Version="1.7.7" />
<PackageReference Include="EasilyNET.MongoDistributedLock" />
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 1.7.7
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EasilyNET.MongoDistributedLock, 1.7.7"
#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.
#:package EasilyNET.MongoDistributedLock@1.7.7
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EasilyNET.MongoDistributedLock&version=1.7.7
#tool nuget:?package=EasilyNET.MongoDistributedLock&version=1.7.7
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");
// 获取锁
// 这里使用一个随机的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);
}
- 注意事项和工作原理
- 当您尝试获取锁时,具有指定 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. net9.0 was computed. 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.
-
net6.0
- MongoDB.Driver (>= 2.20.0)
-
net7.0
- MongoDB.Driver (>= 2.20.0)
-
net8.0
- MongoDB.Driver (>= 2.20.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|