.net core redislock 扩展
public static class DistributedLockExtend { public static IServiceCollection AddDistributedLock(this IServiceCollection services, string connectStr) { // var existingConnectionMultiplexer = ConnectionMultiplexer.Connect(connectStr); var existingConnectionMultiplexer1 = ConnectionMultiplexer.Connect("192.168.0.192:6379"); var multiplexers = new List<RedLockMultiplexer>(); multiplexers.Add(existingConnectionMultiplexer1); services.AddSingleton<IDistributedLockFactory>(sp => { ILoggerFactory logger = sp.GetService<ILoggerFactory>(); var redlockFactory = RedLockFactory.Create(multiplexers,logger); return new DistributedLockFactory(logger, redlockFactory); }); return services; } }
public class DistributedLockFactory : IDistributedLockFactory, IDisposable { private readonly ILoggerFactory _loggerFactory; private readonly RedLockFactory _redLockFactory; public DistributedLockFactory(ILoggerFactory loggerFactory, RedLockFactory redLockFactory) { _loggerFactory = loggerFactory; _redLockFactory = redLockFactory; } public IDistributedLock CreateLock(string resource, TimeSpan expiryTime) { return new DistributedLock(_redLockFactory.CreateLock(resource, expiryTime)); } public IDistributedLock CreateLock(string resource, TimeSpan expiryTime, TimeSpan waitTime, TimeSpan retryTime, CancellationToken? cancellationToken = null) { return new DistributedLock(_redLockFactory.CreateLock(resource, expiryTime, waitTime, retryTime, cancellationToken)); } public async Task<IDistributedLock> CreateLockAsync(string resource, TimeSpan expiryTime) { return new DistributedLock(await _redLockFactory.CreateLockAsync(resource, expiryTime)); } public async Task<IDistributedLock> CreateLockAsync(string resource, TimeSpan expiryTime, TimeSpan waitTime, TimeSpan retryTime, CancellationToken? cancellationToken = null) { return new DistributedLock(await _redLockFactory.CreateLockAsync(resource, expiryTime, waitTime, retryTime, cancellationToken)); } public void Dispose() { _redLockFactory.Dispose(); } }
public interface IDistributedLockFactory { /// <summary> /// Gets a RedLock using the factory's set of redis endpoints. You should check the IsAcquired property before performing actions. /// Blocks and retries up to the specified time limits. /// </summary> /// <param name="resource">The resource string to lock on. Only one RedLock should be acquired for any given resource at once.</param> /// <param name="expiryTime">How long the lock should be held for. /// RedLocks will automatically extend if the process that created the RedLock is still alive and the RedLock hasn't been disposed.</param> /// <returns>A RedLock object.</returns> IDistributedLock CreateLock(string resource, TimeSpan expiryTime); /// <summary> /// Gets a RedLock using the factory's set of redis endpoints. You should check the IsAcquired property before performing actions. /// </summary> /// <param name="resource">The resource string to lock on. Only one RedLock should be acquired for any given resource at once.</param> /// <param name="expiryTime">How long the lock should be held for. /// RedLocks will automatically extend if the process that created the RedLock is still alive and the RedLock hasn't been disposed.</param> /// <returns>A RedLock object.</returns> Task<IDistributedLock> CreateLockAsync(string resource, TimeSpan expiryTime); /// <summary> /// Gets a RedLock using the factory's set of redis endpoints. You should check the IsAcquired property before performing actions. /// Blocks and retries up to the specified time limits. /// </summary> /// <param name="resource">The resource string to lock on. Only one RedLock should be acquired for any given resource at once.</param> /// <param name="expiryTime">How long the lock should be held for. /// RedLocks will automatically extend if the process that created the RedLock is still alive and the RedLock hasn't been disposed.</param> /// <param name="waitTime">How long to block for until a lock can be acquired.</param> /// <param name="retryTime">How long to wait between retries when trying to acquire a lock.</param> /// <param name="cancellationToken">CancellationToken to abort waiting for blocking lock.</param> /// <returns>A RedLock object.</returns> IDistributedLock CreateLock(string resource, TimeSpan expiryTime, TimeSpan waitTime, TimeSpan retryTime, CancellationToken? cancellationToken = null); /// <summary> /// Gets a RedLock using the factory's set of redis endpoints. You should check the IsAcquired property before performing actions. /// Blocks and retries up to the specified time limits. /// </summary> /// <param name="resource">The resource string to lock on. Only one RedLock should be acquired for any given resource at once.</param> /// <param name="expiryTime">How long the lock should be held for. /// RedLocks will automatically extend if the process that created the RedLock is still alive and the RedLock hasn't been disposed.</param> /// <param name="waitTime">How long to block for until a lock can be acquired.</param> /// <param name="retryTime">How long to wait between retries when trying to acquire a lock.</param> /// <param name="cancellationToken">CancellationToken to abort waiting for blocking lock.</param> /// <returns>A RedLock object.</returns> Task<IDistributedLock> CreateLockAsync(string resource, TimeSpan expiryTime, TimeSpan waitTime, TimeSpan retryTime, CancellationToken? cancellationToken = null); }
//DONE:分布式锁 public interface IDistributedLock : IDisposable { bool IsAcquired { get; } }
public class DistributedLock : IDistributedLock { private readonly IRedLock _redLock; public DistributedLock(IRedLock redLock) { _redLock = redLock; } public bool IsAcquired =>_redLock.IsAcquired ; public void Dispose() { throw new NotImplementedException(); } }
//添加分布式锁 builder.Services.AddDistributedLock("");
测试