Skip to content
/ XLocker Public

参考 Redisson ,通过 dotnet 实现可等待的分布式锁实现

License

Notifications You must be signed in to change notification settings

xunki/XLocker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XLocker

English

参考 RedissonLock ,通过 dotnet 实现可等待的分布式锁实现

如何使用

// 暂不提供同步方法,可自行实现,此处暂时通过 Task.Result 来获取,演示使用
private static readonly LockManager _lockManager = LockManager.GetLockManagerAsync(StackExchange.Redis.ConnectionMultiplexer.Connect("redis server:port"), "LOCK:").ConfigureAwait(false).GetAwaiter().GetResult();

/// <summary>
/// 为执行逻辑加锁 
/// </summary>
public async Task Lock(string key, Func<Task> func)
{
    var value = Guid.NewGuid().ToString("N")[..2];
    var success = await _lockManager.LockAsync(key, value, TimeSpan.FromMinutes(3), TimeSpan.FromSeconds(30));
    if (success)
    {
        try
        {
            await func();
        }
        finally
        {
            await _lockManager.UnLockAsync(key, value);
        }
    }
}

备注

本项目目前仅做个人的学习,闲余时的尝试,所写代码仅供参考,也可以随意复制、使用

暂时只写了两个工具类 LockManager LockSemaphoreManager 并依赖 StackExchange.Redis

About

参考 Redisson ,通过 dotnet 实现可等待的分布式锁实现

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages