namespace HealthMonitor.Core.Common { /// /// 分布式同步锁 /// public class DistributedLocker : IDisposable { private const string KEY_LOCKER = nameof(DistributedLocker); private const int EXPIRE_SECONDS = 10; private CSRedis.CSRedisClientLock redis_lock; /// /// 分布式锁(基于Redis) /// /// /// /// public DistributedLocker(object caller, bool isAutoDelay = false) : this(caller.GetType().FullName, isAutoDelay) { } /// /// 分布式锁(基于Redis) /// /// /// /// public DistributedLocker(string? title = null, bool isAutoDelay = false) { if (string.IsNullOrWhiteSpace(title)) title = KEY_LOCKER; redis_lock = RedisHelper.Lock(title, EXPIRE_SECONDS, isAutoDelay); if (redis_lock == null) throw new TimeoutException("RedisLock获取超时"); } public void Dispose() { redis_lock?.Unlock(); //RedisHelper.Del(KEY_LOCKER); } } }