AsyncKeyedLock 6.3.3
AsyncKeyedLock
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.
A simple non-keyed lock is also available through AsyncNonKeyedLocker
.
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.
Benchmarks
This library has been extensively benchmarked against several other options and our benchmarks run publicly and transparently on Github Actions.
When striped locking is an option, the StripedAsyncKeyedLocker
consistently beats any other option in terms of both speed as well as memory allocations.
When the traditional dictionary-based method is used, AsyncKeyedLocker
with pooling provides a great balance in terms of speed and memory allocations, which is an excellent, safe go-to solution.
Credits
Check out our list of contributors!
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 |
.NET 5.0
- No dependencies.
.NET 8.0
- No dependencies.
.NET Standard 2.0
- System.Threading.Tasks.Extensions (>= 4.5.4)
.NET Standard 2.1
- 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 |