Skip to content

xembly/xembly-caching-distributed-lock

Repository files navigation

Xembly Caching Distributed Lock

Build codecov nuget nuget

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.

Installation

Install the Xembly.Caching.Distributed.Lock

.NET Core CLI

Install-Package Xembly.Caching.Distributed.Lock

Package Manager Console

Install-Package Xembly.Caching.Distributed.Lock

Setup

services.AddDistributedCacheLock();

or with options

services.AddDistributedCacheLock(options =>
{
    options.AbandonedTaskLogLevel = LogLevel.Warning;
    options.DefaultTimeoutAfter = TimeSpan.FromSeconds(10);
    options.KeyPrefix = "distributed:cache:entry:";
});

Usage

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
		}
	}
}

License

Licensed under the terms of the BSD-3-Clause License

Credits

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages