Пример #1
0
        /// <summary>
        /// 获得一个非阻塞锁
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="timeout">过期时间</param>
        /// <returns></returns>
        public async Task <RedisLock> GetNonBlockingLockAsync(string key, TimeSpan timeout)
        {
            IDatabase database = GetDB(key);
            bool      isLock   = await database.LockTakeAsync(key, string.Empty, timeout);

            if (!isLock)
            {
                return(null);
            }
            var result = new RedisLock(key, database);

            return(result);
        }
Пример #2
0
        /// <summary>
        /// 获得一个阻塞锁
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="timeout">过期时间</param>
        /// <returns></returns>
        public async Task <RedisLock> GetBlockingLockAsync(string key, TimeSpan timeout)
        {
            IDatabase database = GetDB(key);

            do
            {
                bool isLock = await database.LockTakeAsync(key, string.Empty, timeout);

                if (isLock)
                {
                    var result = new RedisLock(key, database);
                    return(result);
                }
                await Task.Delay(500);
            } while (true);
        }