Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis锁的实现 #17

Open
ren8179 opened this issue Nov 26, 2020 · 0 comments
Open

Redis锁的实现 #17

ren8179 opened this issue Nov 26, 2020 · 0 comments

Comments

@ren8179
Copy link
Owner

ren8179 commented Nov 26, 2020

该实现依赖于CSRedisCore

        /// <summary>
        /// Redis锁
        /// </summary>
        /// <param name="lockKey">锁</param>
        /// <param name="delyInterval">锁失效时间 单位:秒</param>
        /// <returns></returns>
        private bool LockOrderNo(string lockKey, int delyInterval = 120)
        {
            if (RedisHelper.SetNx(lockKey, DateTime.Now.AddSeconds(delyInterval).ToFileTimeUtc()))
            {
                RedisHelper.Expire(lockKey, delyInterval);
                return true;
            }
            else
            {
                var lockValue = RedisHelper.Get<long>(lockKey);
                if (DateTime.Now.ToFileTimeUtc() > lockValue)
                {
                    RedisHelper.GetSet<long>(lockKey, DateTime.Now.AddSeconds(delyInterval).ToFileTimeUtc());
                    RedisHelper.Expire(lockKey, delyInterval);
                    return true;
                }
            }
            return false;
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant