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 Downloads
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
230
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
216
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
194
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
192
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
164
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
141
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
135
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
107
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
86
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
80
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
78
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
75
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
69
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
64
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
63
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
62
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
37
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
29
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
21
EFCoreSecondLevelCacheInterceptor
Entity Framework Core Second Level Caching Library.
20

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 Downloads Last updated
7.1.4 336 12/11/2024
7.1.4-preview 2 01/23/2025
7.1.3 229 11/11/2024
7.0.2 282 10/14/2024
7.0.1 301 08/22/2024
7.0.0 94 07/22/2024
7.0.0-rc3 5 07/21/2024
7.0.0-rc2 4 07/21/2024
7.0.0-rc1 4 07/03/2024
7.0.0-beta 7 07/03/2024
7.0.0-alpha 7 07/03/2024
6.4.2 292 04/29/2024
6.4.1 7 04/29/2024
6.4.0 8 04/29/2024
6.3.4 302 03/25/2024
6.3.4-rc 5 04/29/2024
6.3.4-beta 7 04/29/2024
6.3.3 6 04/29/2024
6.3.2 5 04/29/2024
6.3.0 6 04/29/2024
6.2.6 6 04/29/2024
6.2.5 7 04/29/2024
6.2.4 6 04/29/2024
6.2.3 6 04/29/2024
6.2.3-beta 7 04/29/2024
6.2.2 7 04/29/2024
6.2.1 6 04/29/2024
6.2.0 6 04/29/2024
6.1.1 7 04/05/2024
6.1.1-rc 9 04/29/2024
6.1.1-beta 6 04/29/2024
6.1.0 6 04/06/2024
6.0.5 5 04/29/2024
6.0.5-alpha 8 04/29/2024
6.0.4 7 04/22/2024
6.0.4-rc6 7 04/07/2024
6.0.4-rc5 4 05/26/2024
6.0.4-rc3 6 04/04/2024
6.0.4-rc 8 04/29/2024
6.0.4-beta 6 04/29/2024
6.0.4-alpha 7 04/29/2024
6.0.3 5 04/29/2024
6.0.2 5 04/29/2024
6.0.1 6 04/29/2024
5.1.2 8 04/29/2024
5.1.1 5 04/29/2024
5.1.0 6 04/29/2024
5.0.4 8 04/29/2024
5.0.3 6 04/29/2024
5.0.3-rc 6 04/29/2024
5.0.2-rc 8 04/29/2024
5.0.1 6 04/29/2024
4.0.2 6 04/29/2024
3.2.3 5 04/29/2024
3.2.1 5 04/29/2024
3.2.0 7 04/06/2024
3.0.1 8 04/29/2024
3.0.0 6 04/29/2024
2.0.3 6 04/29/2024
2.0.2 8 04/29/2024
2.0.1 7 04/29/2024
2.0.0 7 04/29/2024
1.1.0 7 04/29/2024
1.0.1 6 04/29/2024
1.0.0 8 04/29/2024