6

複数のマシンで機能するロック メカニズムの推奨事項を探しています。私の場合、サービス マシンがダウンした場合に冗長性を確保する簡単な方法として、2 台のマシンでサービスを開始し、もう一方が終了するまで 1 つのブロックを保持できるようにしたいだけです。

Distributed Lock Serviceと同様ですが、人々が .NET との統合に成功したプロジェクトを特に探しています。

4

3 に答える 3

0

NCache を使用して、この特定のユース ケースに悲観的ロックを使用できます。オプティミスティック ロックは、読み取りが集中するアプリケーションを扱う場合のシナリオに役立ちます。

NCache はそれを実現するのに役立ちます。 http://blogs.alachisoft.com/ncache/distributed-locking/

// Instance of the object used to lock and unlock cache items in NCache
LockHandle lockHandle = new LockHandle();

// Specify time span of 10 sec for which the item remains locked
// NCache will auto release the lock after 10 seconds.
TimeSpan lockSpan = new TimeSpan(0, 0, 10); 

try
{
    // If item fetch is successful, lockHandle object will be populated
    // The lockHandle object will be used to unlock the cache item
    // acquireLock should be true if you want to acquire to the lock.
    // If item does not exists, account will be null
    BankAccount account = cache.Get(key, lockSpan, 
    ref lockHandle, acquireLock) as BankAccount;
    // Lock acquired otherwise it will throw LockingException exception

    if(account != null &&; account.IsActive)
    {
        // Withdraw money or Deposit
        account.Balance += withdrawAmount;
        // account.Balance -= depositAmount;

        // Insert the data in the cache and release the lock simultaneously 
        // LockHandle initially used to lock the item must be provided
        // releaseLock should be true to release the lock, otherwise false
        cache.Insert("Key", account, lockHandle, releaseLock); 
    }
    else
    {
        // Either does not exist or unable to cast
        // Explicitly release the lock in case of errors
        cache.Unlock("Key", lockHandle);
    } 
}
catch(LockingException lockException)
{
    // Lock couldn't be acquired
    // Wait and try again
}
于 2016-05-03T06:04:28.793 に答える