BB.Caching.Cache.Shared.Strings.TakeLockAsync C# (CSharp) Method

TakeLockAsync() public static method

This is a composite helper command, to help with using redis as a lock provider. This is achieved as a RedisKey key/value pair with timeout. If the lock does not exist (or has expired), then a new RedisKey key is created (with the supplied duration), and true is returned to indicate success. If the lock already exists, then no lock is taken, and false is returned. The value may be fetched separately, but the meaning is implementation-defined). No change is made if the lock was not successfully taken. In this case, the client should delay and retry.

It is expected that a well-behaved client will also release the lock in a timely fashion via ReleaseLockAsync.

It transpires that robust locking in redis is actually remarkably hard, and most implementations are broken in one way or another (most commonly: thread-race, or extending the lock duration when failing to take the lock).
public static TakeLockAsync ( RedisKey key, RedisValue value, System.TimeSpan expiry ) : Task
key RedisKey /// The key. ///
value RedisValue /// The value. ///
expiry System.TimeSpan /// An expiration lifetime. ///
return Task
                public static Task<bool> TakeLockAsync(RedisKey key, RedisValue value, TimeSpan expiry)
                {
                    Task<bool> result = SharedCache.Instance.GetWriteConnection(key)
                        .GetDatabase(SharedCache.Instance.Db)
                        .LockTakeAsync(key, value, expiry);

                    return result;
                }