Beispiel #1
0
        public bool Lock(string resourceKey, TimeSpan expires, out LockInfo lockInfo, string resourceValue = null)
        {
            string value = resourceValue;

            if (string.IsNullOrWhiteSpace(value))
            {
                value = this.CreateUniqueLockId();
            }

            LockInfo innerLock = null;

            bool isSuccess = this.RetryGetLock(retryCount, retryDelay, () =>
            {
                try
                {
                    int 当前已获得的锁   = 0;
                    var startTime = DateTime.Now;

                    this.Foreach_RedisServer(redisServer =>
                    {
                        if (this.LockInstance(redisServer, resourceKey, value, expires))
                        {
                            当前已获得的锁 += 1;
                        }
                    });

                    var drift = Convert.ToInt32((expires.TotalMilliseconds * 0.01) + 2);

                    //如果validTimeSpan大于0,表示在超时之前已经获得锁,该锁是有效的
                    var validTimeSpan = expires - (DateTime.Now - startTime) - TimeSpan.FromMilliseconds(drift);

                    if (当前已获得的锁 >= 获得锁的临界值 && validTimeSpan.TotalMilliseconds > 0)
                    {
                        innerLock = new LockInfo(resourceKey, value, validTimeSpan);

                        return(true);
                    }
                    else
                    {
                        //如果没有获取到一半以上的服务器锁,那么要尽快释放之前获得的锁
                        this.Foreach_RedisServer(redisServer =>
                        {
                            this.UnlockInstance(redisServer, resourceKey, value);
                        });

                        return(false);
                    }
                }
                catch (Exception)
                {
                    return(false);
                }
            });

            lockInfo = innerLock;

            return(isSuccess);
        }
Beispiel #2
0
        public void Unlock(LockInfo lockInfo)
        {
            if (lockInfo == null ||
                string.IsNullOrWhiteSpace(lockInfo.ResourceKey) ||
                string.IsNullOrWhiteSpace(lockInfo.Value))
            {
                return;
            }

            this.Foreach_RedisServer(redisServer =>
            {
                this.UnlockInstance(redisServer, lockInfo.ResourceKey, lockInfo.Value);
            });
        }
Beispiel #3
0
        /// <summary>
        /// 释放指定资源的锁
        /// </summary>
        /// <param name="lockInfo">锁对象</param>
        public void Unlock(LockInfo lockInfo)
        {
            RedisLock redisLock = new RedisLock(RedisManager.Connection);

            redisLock.Unlock(lockInfo);
        }
Beispiel #4
0
        /// <summary>
        /// 获取指定资源的锁
        /// </summary>
        /// <param name="resourceKey">锁资源名称</param>
        /// <param name="expires">锁过期时间</param>
        /// <param name="lockInfo">获取到的锁对象</param>
        /// <param name="resourceValue">锁资源对应的值</param>
        /// <returns>是否成功获取锁</returns>
        public bool Lock(string resourceKey, TimeSpan expires, out LockInfo lockInfo, string resourceValue = null)
        {
            RedisLock redisLock = new RedisLock(RedisManager.Connection);

            return(redisLock.Lock(resourceKey, expires, out lockInfo, resourceValue));
        }