Xembly Caching Distributed Lock is an addon for Microsoft's Distributed Cache library which creates a distributed lock for your chosen type of distributed cache system that implements IDistributedCache.
Install the Xembly.Caching.Distributed.Lock
Install-Package Xembly.Caching.Distributed.Lock
Install-Package Xembly.Caching.Distributed.Lock
services.AddDistributedCacheLock();
or with options
services.AddDistributedCacheLock(options =>
{
options.AbandonedTaskLogLevel = LogLevel.Warning;
options.DefaultTimeoutAfter = TimeSpan.FromSeconds(10);
options.KeyPrefix = "distributed:cache:entry:";
});
public class MyService
{
private readonly IDistributedCacheLock _lock;
public MyService(IDistributedCacheLock dcl)
{
_lock = dcl;
}
public async Task Run(CancellationToken ct)
{
// Create the lock with default timeout
using (await _lock.Acquire("nameOfLock", ct))
{
// do something while there is a lock
}
// Create the lock with custom timeout
using (await _lock.Acquire("nameOfLock", TimeSpan.FromSeconds(30), ct))
{
// do something while there is a lock
}
}
}
Licensed under the terms of the BSD-3-Clause License
This application uses Open Source components. You can find the source code of their open source projects along with license information below. We acknowledge and are grateful to these developers for their contributions to open source.
Project: Microsoft.Extensions.Caching.Abstractions
Copyright © Microsoft Corporation. All rights reserved.
License Apache-2.0
Project: Microsoft.Extensions.Logging.Abstractions
Copyright © Microsoft Corporation. All rights reserved.
License Apache-2.0
Project: Microsoft.Extensions.Options
Copyright © Microsoft Corporation. All rights reserved.
License Apache-2.0
Project: Polly
Copyright © 2020, App vNext
License BSD-3-Clause