기본적으로 분산 잠금 메커니즘이 필요합니다. 많은 분산 서버 응용 프로그램이 이러한 기능을 제공합니다. 우리가 코드에 질문을 변환 기본적으로 경우이
// BANK WITHDRAWAL APPLICATION
// Fetch BankAccount object from NCache
BankAccount account = cache.Get("Key") as BankAccount; // balance = 30,000
Money withdrawAmount = 15000;
if (account != null && account.IsActive)
{
// Withdraw money and reduce the balance
account.Balance -= withdrawAmount;
// Update cache with new balance = 15,000
cache.Insert("Key", account);
}
=========================
처럼
볼 것이다
// BANK DEPOSIT APPLICATION
// Fetch BankAccount object from NCache
BankAccount account = cache.Get("Key") as BankAccount; // balance = 30,000
Money depositAmount = 5000;
if (account != null && account.IsActive)
{
// Deposit money and increment the balance
account.Balance += depositAmount;
// Update cache with new balance = 35,000
cache.Insert("Key", account);
}
이 는 기본적으로 경쟁 조건의 예입니다
두 개 이상의 사용자가 액세스하는 동시에 동일한 공유 데이터를 변경하지만 그 일을 끝낼려고 할 때 경쟁 조건이 있습니다 잘못된 순서로
분산 잠금에서 위의 코드에 대한 대답은
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
}
이 답변은 NCache (A 분산 캐시)에 매우 특이하다. 난 당신이
Source
접근 방식이나 도구가 필요한지 확실하지 않지만 다음을 참고하십시오. http://blog.cask.co/2014/11/how-we-built-it-designing-a-globally- 일관된 트랜잭션 엔진 / – guillaume31