AsyncKeyedLock 3.2.3

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.

Benchmarks

Tests show that AsyncKeyedLock is faster than similar libraries, while consuming less memory.

Installation

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

Usage

You need to start off with creating an instance of AsyncKeyedLocker or AsyncKeyedLocker<T>. The recommended way is to use the latter, which consumes less memory. The former uses object and may be slightly faster, but at the expense of higher memory usage.

Dependency injection

services.AddSingleton<IAsyncKeyedLocker, AsyncKeyedLocker>();

or:

services.AddSingleton<IAsyncKeyedLocker<string>, AsyncKeyedLocker<string>>();

Variable instantiation

var asyncKeyedLocker = new AsyncKeyedLocker();

or:

var asyncKeyedLocker = new AsyncKeyedLocker<string>();

or if you would like to set the maximum number of requests for the semaphore that can be granted concurrently (set to 1 by default):

var asyncKeyedLocker = new AsyncKeyedLocker<string>(2);

Locking

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

There are other overloaded methods for LockAsync which allow you to use CancellationToken, milliseconds timeout, System.TimeSpan or a combination of these. In the case of timeouts, you can also use TryLockAsync methods which will call a Func<Task> or Action if the timeout is not expired, whilst returning a boolean representing whether or not it waited successfully.

There are also synchronous Lock methods available, including out parameters for checking whether or not the timeout was reached.

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);

Credits

This library was inspired by 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

Fixes CancellationToken issues and adding TryLockAsync methods.

.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