2015-05-07 60 views
1

我有以下方法,它將等待Redis密鑰上的獨佔鎖。這種方法的工作原理,但我想知道沒有for循環和Thread.Sleep是否有更好的方法。釋放時,即發佈 - 用於指示何時鎖威力現在可用:StackExchange.Redis - 等待鎖的最佳方式

/// <summary> 
    /// wait up to 2 seconds to achieve a lock! 
    /// The lock is good for a maximum of 3 seconds 
    /// </summary> 
    /// <param name="codeID"></param> 
    internal void WaitForSingleUseLock(CodeID codeID) 
    { 
     var key = _redemptionRepo.SingleUseCodeLockPrefix + codeID.Value; 
     var expiration = TimeSpan.FromSeconds(3); 
     for (var i = 0; i < 20; i++) 
     { 
      var lockAchieved = _cacheRepo.LockTake(key, "1", expiration); 
      if (lockAchieved) 
      { 
       break; 
      } 
      Thread.Sleep(TimeSpan.FromMilliseconds(100)); 
     } 
    } 

回答

0

在這方面,我決定這是一個最終的解決方案以@馬克的意見納入考慮,並會與我的團隊有關Task.Delay()好處超過Thread.Sleep()後:

/// <summary> 
    /// wait up to 3 seconds to achieve a lock! 
    /// The lock is good for a maximum of 3 seconds 
    /// 
    /// Returns the total amount of time until the lock was taken 
    /// </summary> 
    internal virtual async Task<TimeSpan> WaitForSingleUseLock(CodeID codeID) 
    { 
     var key = _redemptionRepo.SingleUseCodeLockPrefix + codeID.Value; 
     var totalTime = TimeSpan.Zero; 
     var maxTime = TimeSpan.FromSeconds(3); 
     var expiration = TimeSpan.FromSeconds(3); 
     var sleepTime = TimeSpan.FromMilliseconds(50); 
     var lockAchieved = false; 

     while (!lockAchieved && totalTime < maxTime) 
     { 
      lockAchieved = _cacheRepo.LockTake(key, "1", expiration); 
      if (lockAchieved) 
      { 
       continue; 
      } 
      await Task.Delay(sleepTime); 
      totalTime += sleepTime; 
     } 
     return totalTime; 
    } 
1

我可以建議唯一不同的是考慮發佈/訂閱側通道(另外,沒有更換的意思) ,並使用該子釋放定時器(通過監視器或異步等待句柄)。

除此之外:不。 Redis不具備待處理隊列的想法。也許你可以可能構建一個使用列表,但是......

相關問題