/redlock-cs

Distributed lock with Redis and C# (based on http://redis.io/topics/distlock)

Primary LanguageC#Apache License 2.0Apache-2.0

redlock-cs

Build status

Distributed lock with Redis and C# (based on the redlock algorithm)

Redlock-cs is available through nuget as redlock-cs package.

Usage

Check our Unit Test.

The API is based on antirez Ruby implementation and works as in the following example:

// Declare a Distributed Lock based on 3 REDIS servers

var dlm = new Redlock(
		ConnectionMultiplexer.Connect("127.0.0.1:6379"), 
		ConnectionMultiplexer.Connect("127.0.0.1:6380"), 
		ConnectionMultiplexer.Connect("127.0.0.1:6381")
	      );

// Declare lock object.
Lock lockObject;

// Try to acquire the lock (with resourceName as lock identifier and an 
// expiration time of 10 seconds).
var locked = dlm.Lock(
		resourceName,
		new TimeSpan(0, 0, 10), 
		out lockObject
	     );

// If locked is true, lockObject is populated and the lock has been acquired, 
// otherwise the lock has not been acquired.

// Tries to release the lock contained in lockObject.
dlm.Unlock(lockObject);

TODO

  • Disposable pattern.
  • Hide StackExchange.Redis library inside Redlock object.