using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using StackExchange.Redis; namespace RedisStuy { /// /// Redis 做锁 /// public class RedisLockStudy { #region 初始化 private IDatabase redisDatabase; public RedisLockStudy() { redisDatabase = RedisHelper.GetRedisDatabase(); } #endregion public void ReidsLock() { string lockKey = "MyTestRedisKey"; string lockVlaue = Guid.NewGuid().ToString(); bool getLock = redisDatabase.LockTake(lockKey, lockVlaue, TimeSpan.FromSeconds(20), CommandFlags.None); if (getLock) { try { //do other thing } finally { bool releaseLock = redisDatabase.LockRelease(lockKey, lockVlaue, CommandFlags.None); if (releaseLock == false) { } } } } } }