• Stars
    star
    136
  • Rank 267,670 (Top 6 %)
  • Language
    C#
  • License
    Apache License 2.0
  • Created about 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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.