Пример #1
0
        public bool AcquireLock(string id, TimeSpan expiryTime)
        {
            var redLock = _redlockFactory.CreateLock(id, expiryTime);

            if (redLock.IsAcquired)
            {
                lock (ManagedLocks)
                {
                    ManagedLocks.Add(redLock);
                }
                return(true);
            }
            return(false);
        }
        public IDistributedLock AcquireLock(string resource)
        {
            var redLock = _redLockFactory.CreateLock(resource, _options.Expiry, _options.Wait, _options.Retry);

            return(redLock.IsAcquired
                ? new RedLockDecorator(redLock)
                : throw new RedLockAcquiringException());
        }
Пример #3
0
        public void Lock(string resource, TimeSpan expiry, Action action)
        {
            var wait  = TimeSpan.FromMilliseconds(50);
            var retry = TimeSpan.FromSeconds(2);

            using (var redLock = _redLockFactory.CreateLock(resource, expiry, wait, retry))
            {
                if (redLock.IsAcquired)
                {
                    action();
                }
            }
        }
        public bool PerformActionWithLock(string resource, TimeSpan expirationTime, Action action)
        {
            //use RedLock library
            using (var redisLock = _redisLockFactory.CreateLock(resource, expirationTime))
            {
                if (!redisLock.IsAcquired)
                {
                    return(false);
                }

                action();
                return(true);
            }
        }
Пример #5
0
        /// <summary>
        /// Perform some action with Redis distributed lock
        /// </summary>
        /// <param name="resource">The thing we are locking on</param>
        /// <param name="expirationTime">The time after which the lock will automatically be expired by Redis</param>
        /// <param name="action">Action to be performed with locking</param>
        /// <returns>True if lock was acquired and action was performed; otherwise false</returns>
        public bool PerformActionWithLock(string resource, TimeSpan expirationTime, Action action)
        {
            //use RedLock library
            using (var redisLock = _redisLockFactory.CreateLock(resource, expirationTime))
            {
                //ensure that lock is acquired
                if (!redisLock.IsAcquired)
                    return false;

                //perform action
                action();

                return true;
            }
        }
Пример #6
0
        /// <summary>
        /// Perform some action with Redis distributed lock
        /// </summary>
        /// <param name="key">The thing we are locking on</param>
        /// <param name="expirationTime">The time after which the lock will automatically be expired by Redis</param>
        /// <param name="action">Action to be performed with locking</param>
        /// <returns>True if lock was acquired and action was performed; otherwise false</returns>
        public async Task <bool> PerformActionWithLock(string key, TimeSpan expirationTime, Func <Task> action)
        {
            //use RedLock library
            using var redisLock = _redisLockFactory.CreateLock(key, expirationTime);
            //ensure that lock is acquired
            if (!redisLock.IsAcquired)
            {
                return(false);
            }

            //perform action
            await action();

            return(true);
        }
Пример #7
0
        /// <summary>
        /// Perform an exclusive action
        /// </summary>
        /// <param name="resource">Name of the resource to lock</param>
        /// <param name="expiryTime">Lock expiration time</param>
        /// <param name="action">Action to perform</param>
        /// <returns></returns>
        public bool DoExclusive(string resource, TimeSpan expiryTime, TimeSpan waitTime, TimeSpan retryTime, Action action)
        {
            using (var redLock = _redLockFactory.CreateLock(resource, expiryTime, waitTime, retryTime))
            {
                // make sure we got the lock
                if (redLock.IsAcquired)
                {
                    action.Invoke();
                }
                return(redLock.IsAcquired);
            }

            /*
             * using (var redLock = _redLockFactory.CreateLock(resource, expiryTime))
             * {
             *  // make sure we got the lock
             *  if (redLock.IsAcquired)
             *  {
             *      action.Invoke();
             *  }
             *  return redLock.IsAcquired;
             * }
             */
        }