Beispiel #1
0
        private async Task <bool> IfLockDeadAsync(string key, TimeSpan expireTime)
        {
            RedisValue redisValue = await this.database.StringGetAsync(key);

            if (!redisValue.HasValue)
            {
                return(false);
            }

            LockObject lockObject = redisValue.ToString().FromJson <LockObject>();

            return(lockObject.CreateTime.Add(-expireTime) > DateTime.UtcNow);
        }
Beispiel #2
0
        private async Task <bool> SetLockAsync(string key, LockObject redisValue, TimeSpan expireTime)
        {
            bool succeeded;

            try
            {
                succeeded = await this.database.StringSetAsync(key, redisValue.ToJson(), expireTime, When.NotExists);
            }
            catch (Exception)
            {
                succeeded = false;
            }

            return(succeeded);
        }
Beispiel #3
0
        /// <summary>
        ///     锁操作
        /// </summary>
        /// <param name="redisKey">Redis键</param>
        /// <param name="expireTime">过期时间,增强对超时操作的可操作性</param>
        /// <returns>System.Threading.Tasks.Task&lt;System.Tuple&lt;System.Boolean, Jinyinmao.OperativeCenter.Lib.RedisLock.LockObject&gt;&gt;.</returns>
        public async Task <Tuple <bool, LockObject> > LockAsync(RedisKey redisKey, TimeSpan expireTime)
        {
            int i = 0;

            while (true)
            {
                LockObject lockObject = null;
                try
                {
                    lockObject = new LockObject(redisKey, DateTime.Now.ToString(CultureInfo.InvariantCulture), DateTime.UtcNow);

                    if (!await this.SetLockAsync(redisKey, lockObject, expireTime))
                    {
                        await Task.Delay(this.retryEveryMilliseconds);

                        if (await this.IfLockDeadAsync(redisKey, expireTime))
                        {
                            await this.ReleaseLockAsync(redisKey);
                        }

                        if (i > this.maxRetryCount)
                        {
                            await this.ReleaseLockAsync(redisKey);

                            return(Tuple.Create(true, lockObject));
                        }

                        i++;
                        continue;
                    }
                }
                catch (Exception)
                {
                    await this.ReleaseLockAsync(redisKey);

                    return(Tuple.Create(false, lockObject));
                }

                return(Tuple.Create(true, lockObject));
            }
        }
Beispiel #4
0
        /// <summary>
        ///     锁操作,无自定义超时操作,直到功能运行结束
        /// </summary>
        /// <param name="redisKey">The redis key.</param>
        /// <param name="action">The action.</param>
        /// <returns>Task&lt;Tuple&lt;System.Boolean, LockObject&gt;&gt;.</returns>
        public async Task <Tuple <bool, LockObject> > LockUntilCompleteAsync(RedisKey redisKey, Task <Action> action)
        {
            LockObject lockObject = null;

            try
            {
                lockObject = new LockObject(redisKey, DateTime.Now.ToString(CultureInfo.InvariantCulture), DateTime.UtcNow);

                if (await this.SetLockAsync(redisKey, lockObject, TimeSpan.FromHours(5)))
                {
                    await action;

                    await this.ReleaseLockAsync(redisKey);
                }
            }
            catch (Exception)
            {
                await this.ReleaseLockAsync(redisKey);

                return(Tuple.Create(false, lockObject));
            }

            return(Tuple.Create(true, lockObject));
        }