1.//使用方法
using (var Lock = RedisHelper.Lock("锁名", "过期时间"))//返回CSRedisClientLock方法
{
if (Lock == null)
{
return new Response().Fail("获取分布式锁失败");
}
//业务代码
}//using结束默认调用CSRedisClientLock的Dispose方法,释放锁
2.//源代码分析
public CSRedisClientLock Lock(string name, int timeoutSeconds, bool autoDelay = true)
{
name = $"CSRedisClientLock:{name}";
var startTime = DateTime.Now;
while (DateTime.Now.Subtract(startTime).TotalSeconds < timeoutSeconds)//通多过期时间循环去等待加锁
{
var value = Guid.NewGuid().ToString();
if (this.Set(name, value, timeoutSeconds, RedisExistence.Nx) == true)//通过Redis Setnx设置锁也就是redis的 key value
{
double refreshSeconds = (double)timeoutSeconds / 2.0;
return new CSRedisClientLock(this, name, value, timeoutSeconds, refreshSeconds, autoDelay);//返回CSRedisClientLock类
}
Thread.CurrentThread.Join(3);//加锁失败阻塞当前线程
}
return null;
}
public bool Set(string key, object value, TimeSpan expire, RedisExistence? exists = null)//通过Redis Setnx设置锁成功返回true失败返回false
{
object redisValule = this.SerializeRedisValueInternal(value);
if (expire <= TimeSpan.Zero && exists == null) return ExecuteScalar(key, (c, k) => c.Value.Set(k, redisValule)) == "OK";
if (expire <= TimeSpan.Zero && exists != null) return ExecuteScalar(key, (c, k) => c.Value.Set(k, redisValule, null, exists)) == "OK";
if (expire > TimeSpan.Zero && exists == null) return ExecuteScalar(key, (c, k) => c.Value.Set(k, redisValule, expire, null)) == "OK";
if (expire > TimeSpan.Zero && exists != null) return ExecuteScalar(key, (c, k) => c.Value.Set(k, redisValule, expire, exists)) == "OK";
return false;
}
private static RedisStatus.Nullable Set(string key, object value, int? expirationSeconds = null, long? expirationMilliseconds = null, RedisExistence? exists = null)
{
var args = new List
CSRedis GitHub地址
3.分布式加锁流程
1.通过Redis Setnx加锁并设置过期时间。
2.如果锁不存在就加锁。
3.如果锁存在就通过join阻塞线程,循环等待加锁直至过期时间结束。
4.加锁成功后执行业务并释放锁。
5.加锁失败返回错误。



