FoundatioFx / Foundatio

Pluggable foundation blocks for building distributed apps.
Apache License 2.0
1.99k stars 244 forks source link

How can i use Foundatio.Lock? #112

Closed ccccccmd closed 6 years ago

ccccccmd commented 6 years ago

It's like this ?

somecode to get lockprovider

   var rediscco = new RedisCacheClientOptions() { ConnectionMultiplexer = muxer };

 _lockProvider = new CacheLockProvider(new RedisCacheClient(rediscco), new RedisMessageBus(new RedisMessageBusOptions() { Subscriber = rediscco.ConnectionMultiplexer.GetSubscriber() }));

some code to get lock

 public static  async Task<ILock> AcquireAsync(string name,
        TimeSpan? lockTimeout = null,
        CancellationToken cancellationToken = default(CancellationToken),
        bool continueOnCapturedContext = false)
    {
        var @lock = await _lockProvider.AcquireAsync(name, lockTimeout, cancellationToken)
            .ConfigureAwait(continueOnCapturedContext);
        return @lock;
    }

user lock

  var l = LockHelper.AcquireAsync(key, TimeSpan.FromSeconds(5)).Result ;   
  ............my code to process stock............
  l.ReleaseAsync();
niemyjski commented 6 years ago

That looks right, are you seeing some kind of issue?

ccccccmd commented 6 years ago

i find this project: https://github.com/IvanZheng/IFramework/tree/master/Src/iFramework.Plugins/IFramework.FoundatioLock

by the way, two mechines have two singleton lockproviders,will it lead to problems?

niemyjski commented 6 years ago

I suppose it could if you are locking on the same key inside of each other but I'm not sure without trying it. If you have a sample that can show off an issue that would be greatly appreciated.

IvanZheng commented 6 years ago

@ccccccmd IFramework.FoundatioLock is only used for single process, because it's in memory lock based on Foundatio. If you want to use for distributed system, you should use IFramework.FoundatioLockRedis which is based on Foundatio.Redis.

niemyjski commented 6 years ago

I'm going to close this issue, if you have any questions, please let us know.