AsyncKeyedLock 6.2.6

AsyncKeyedLock AsyncKeyedLock

GitHub Workflow Status NuGet NuGet Codacy Grade Codecov

An asynchronous .NET Standard 2.0 library that allows you to lock based on a key (keyed semaphores), limiting concurrent threads sharing the same key to a specified number, with optional pooling for reducing memory allocations.

For example, suppose you were processing financial transactions, but while working on one account you wouldn't want to concurrently process a transaction for the same account. Of course, you could just add a normal lock, but then you can only process one transaction at a time. If you're processing a transaction for account A, you may want to also be processing a separate transaction for account B. That's where AsyncKeyedLock comes in: it allows you to lock but only if the key matches.

The library uses two very different methods for locking, one using an underlying ConcurrentDictionary that's cleaned up after use whilst the other using a technique called striped locking. Both have their advantages and disadvantages, and in order to help you choose you are highly recommended to read about it in the wiki.

Installation and usage

Using this library is straightforward. Here's a simple example:

private static readonly AsyncKeyedLocker<string> _asyncKeyedLocker = new(o =>
  {
    o.PoolSize = 20; // this is NOT a concurrency limit
    o.PoolInitialFill = 1;
  });

...

using (await _asyncKeyedLocker.LockAsync("test123"))
{
  ...
}

Please read the full documentation in our wiki for help in using this libary.

Credits

Check out our list of contributors!

Showing the top 20 packages that depend on AsyncKeyedLock.

Packages Downloads
Volo.Abp.DistributedLocking.Abstractions
Package Description
9
Volo.Abp.DistributedLocking.Abstractions
Package Description
4
Volo.Abp.DistributedLocking.Abstractions
Package Description
1

Cleaning pooling and aggressively inlining locking methods.

.NET 5.0

  • No dependencies.

.NET 8.0

  • No dependencies.

.NET Standard 2.0

.NET Standard 2.1

  • No dependencies.

Version Downloads Last updated
7.0.1 1 9/12/2024
7.0.0 0 7/21/2024
7.0.0-rc3 0 7/20/2024
7.0.0-rc2 0 7/20/2024
7.0.0-rc1 1 9/12/2024
7.0.0-beta 1 9/12/2024
7.0.0-alpha 0 6/29/2024
6.4.2 0 4/21/2024
6.4.1 1 8/29/2024
6.4.0 0 4/20/2024
6.3.4 2 8/30/2024
6.3.4-rc 1 8/29/2024
6.3.4-beta 0 1/23/2024
6.3.3 0 1/14/2024
6.3.2 0 1/14/2024
6.3.0 0 1/14/2024
6.2.6 1 9/12/2024
6.2.5 1 9/12/2024
6.2.4 1 9/12/2024
6.2.3 0 12/3/2023
6.2.3-beta 0 12/2/2023
6.2.2 2 11/16/2023
6.2.1 4 5/18/2023
6.2.0 2 6/29/2023
6.1.1 1 7/13/2023
6.1.1-rc 0 1/27/2023
6.1.1-beta 0 1/27/2023
6.1.0 2 6/29/2023
6.0.5 1 9/12/2024
6.0.5-alpha 0 12/30/2022
6.0.4 1 6/29/2023
6.0.4-rc6 3 6/29/2023
6.0.4-rc5 1 6/29/2023
6.0.4-rc3 0 12/30/2022
6.0.4-rc 1 6/29/2023
6.0.4-beta 1 6/29/2023
6.0.4-alpha 1 6/29/2023
6.0.3 1 6/29/2023
6.0.2 1 6/29/2023
6.0.1 1 4/15/2024
5.1.2 1 6/29/2023
5.1.1 1 9/12/2024
5.1.0 1 6/29/2023
5.0.4 1 6/29/2023
5.0.3 1 6/29/2023
5.0.3-rc 1 6/29/2023
5.0.2-rc 1 6/29/2023
5.0.1 1 6/29/2023
4.0.2 1 6/29/2023
3.2.3 0 10/26/2022
3.2.1 0 10/23/2022
3.2.0 1 6/29/2023
3.0.1 2 6/29/2023
3.0.0 1 6/29/2023
2.0.3 1 6/29/2023
2.0.2 1 6/29/2023
2.0.1 1 6/29/2023
2.0.0 2 6/29/2023
1.1.0 0 12/11/2021
1.0.1 1 6/29/2023
1.0.0 0 11/22/2021