Closed MarsonShine closed 2 years ago
using (var o = RedisHelper.Lock("LockName", TimeSpan.FromSeconds(15).Seconds))
{
if(o ==null)
{
超时//
}else{
_logger.LogWarning("{guid} 占有锁...", guid);
Thread.Sleep(TimeSpan.FromSeconds(5));
}
}
_logger.LogWarning("{guid} 释放锁...", guid);
return Ok();
using (var o = RedisHelper.Lock("LockName", TimeSpan.FromSeconds(15).Seconds)) { if(o ==null) { 超时// }else{ _logger.LogWarning("{guid} 占有锁...", guid); Thread.Sleep(TimeSpan.FromSeconds(5)); } } _logger.LogWarning("{guid} 释放锁...", guid); return Ok();
就是说我demo的那段代码中,写法有误? RedisHelper.Lock是同步方法,如果锁已被占用,就会一直等待,超时不会报错而是返回null。 我去renew一下,感谢回复。
using (var o = RedisHelper.Lock("LockName", TimeSpan.FromSeconds(15).Seconds)) { if(o ==null) { 超时// }else{ _logger.LogWarning("{guid} 占有锁...", guid); Thread.Sleep(TimeSpan.FromSeconds(5)); } } _logger.LogWarning("{guid} 释放锁...", guid); return Ok();
就是说我demo的那段代码中,写法有误? RedisHelper.Lock是同步方法,如果锁已被占用,就会一直等待,超时不会报错而是返回null。 我去renew一下,感谢回复。
另外 你这里还缺少释放锁的步骤~
using (var o = RedisHelper.Lock("LockName", TimeSpan.FromSeconds(15).Seconds)) { if(o ==null) { 超时// }else{ _logger.LogWarning("{guid} 占有锁...", guid); Thread.Sleep(TimeSpan.FromSeconds(5)); } } _logger.LogWarning("{guid} 释放锁...", guid); return Ok();
就是说我demo的那段代码中,写法有误? RedisHelper.Lock是同步方法,如果锁已被占用,就会一直等待,超时不会报错而是返回null。 我去renew一下,感谢回复。
另外 你这里还缺少释放锁的步骤~
刚测试了,是写法有误。因为超时返回了null(不过反倒有些麻烦了,需要手动再次等待... 囧)。 不用手动释放啊,lockclient返回的是dispose类型,using自动释放的。
是写法问题,close
环境: windows10 CSRedisCore 3.6.8 .net5
复现代码
在5秒内多次请求,就会发现有偶发性的出现在没有释放锁时候,重复持有锁。 以下是测试的截图