This package is just RedLock.net wrapper and simplifies it using within NetCore
docker run -d -p 6379:6379 redis
dotnet add package ch1seL.DistributedLock.RedisLock
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddStackExchangeRedisLock(options => options.Configuration = "localhost");
}
public class Worker : BackgroundService
{
private readonly IDistributedLock _distributedLock;
public Worker(IDistributedLock distributedLock)
{
_distributedLock = distributedLock;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using (await _distributedLock.CreateLockAsync("test-lock", cancellationToken: stoppingToken)) {
await DoSomeStuff();
}
}
}
For testing or local usage you can register in-memory implementation
dotnet add package ch1seL.DistributedLock.MemoryLock
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) => {
if (hostContext.HostingEnvironment.IsDevelopment()) {
services.AddMemoryLock();
} else {
services.AddStackExchangeRedisLock(options => options.Configuration = "localhost");
}
}
https://github.com/ch1seL/DistributedLock/tree/master/samples/WorkerService