AsyncKeyedLock 3.0.0

AsyncKeyedLock AsyncKeyedLock

GitHub Workflow Status Nuget Nuget

An asynchronous .NET Standard 2.0 library that allows you to lock based on a key (keyed semaphores), only allowing a defined number of concurrent threads that share the same key.

For example, if you're processing transactions, you may want to limit to only one transaction per user so that the order is maintained, but meanwhile allowing parallel processing of multiple users.

Installation

The recommended means is to use NuGet, but you could also download the source code from here.

Usage

var asyncKeyedLocker = new AsyncKeyedLocker();
using (var lockObj = await asyncKeyedLocker.LockAsync(myObject))
{
	...
}

You can also set the maximum number of requests for the semaphore that can be granted concurrently (set to 1 by default):

var asyncKeyedLocker = new AsyncKeyedLocker(2);

If you would like to see how many concurrent requests there are for a semaphore for a given key:

int myRemainingCount = asyncKeyedLocker.GetRemainingCount(myObject);

If you would like to see the number of remaining threads that can enter the lock for a given key:

int myCurrentCount = asyncKeyedLocker.GetCurrentCount(myObject);

If you would like to check whether any request is using a specific key:

bool isInUse = asyncKeyedLocker.IsInUse(myObject);

And if for some reason you need to force release the requests in the semaphore for a key:

asyncKeyedLocker.ForceRelease(myObject);

You may also use Dependency Injection to inject an instance of AsyncKeyedLock.

Credits

This library is based on Stephen Cleary's solution.

Showing the top 20 packages that depend on AsyncKeyedLock.

Packages
EasyCaching.Core
EasyCaching is a open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier!
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.

Fixed serious issue with concurrent threads having been limited to 1 irrespective of setting in the constructor. Also added a few features and tests.

.NET Standard 2.0

  • No dependencies.

Version Last updated
7.1.6 04/16/2025
7.1.5-alpha 04/21/2025
7.1.4 03/12/2025
7.1.4-preview 03/23/2025
7.1.3 04/11/2025
7.0.2 03/27/2025
7.0.1 06/09/2025
7.0.0 04/05/2025
7.0.0-rc3 03/25/2025
7.0.0-rc2 03/13/2025
7.0.0-rc1 06/10/2025
7.0.0-beta 04/01/2025
7.0.0-alpha 03/29/2025
6.4.2 05/13/2025
6.4.1 06/08/2025
6.4.0 03/14/2025
6.3.4 04/07/2025
6.3.4-rc 05/04/2025
6.3.4-beta 06/09/2025
6.3.3 03/22/2025
6.3.2 04/02/2025
6.3.0 04/01/2025
6.2.6 03/15/2025
6.2.5 03/14/2025
6.2.4 03/17/2025
6.2.3 04/14/2025
6.2.3-beta 04/06/2025
6.2.2 03/22/2025
6.2.1 03/22/2025
6.2.0 05/02/2025
6.1.1 06/06/2025
6.1.1-rc 06/06/2025
6.1.1-beta 03/28/2025
6.1.0 03/17/2025
6.0.5 03/14/2025
6.0.5-alpha 04/20/2025
6.0.4 03/26/2025
6.0.4-rc6 05/13/2025
6.0.4-rc5 03/21/2025
6.0.4-rc3 04/14/2025
6.0.4-rc 03/17/2025
6.0.4-beta 04/22/2025
6.0.4-alpha 04/18/2025
6.0.3 04/03/2025
6.0.2 04/02/2025
6.0.1 05/09/2025
5.1.2 06/08/2025
5.1.1 03/15/2025
5.1.0 03/16/2025
5.0.4 04/04/2025
5.0.3 04/18/2025
5.0.3-rc 04/21/2025
5.0.2-rc 06/06/2025
5.0.1 03/18/2025
4.0.2 03/21/2025
3.2.3 06/09/2025
3.2.1 03/28/2025
3.2.0 06/08/2025
3.0.1 04/29/2025
3.0.0 06/09/2025
2.0.3 05/04/2025
2.0.2 06/07/2025
2.0.1 06/08/2025
2.0.0 04/28/2025
1.1.0 05/07/2025
1.0.1 04/11/2025
1.0.0 05/16/2025