/// <summary> /// /// </summary> /// <param name="key"></param> public void Lock(object key) { Log.Debug("acquiring cache lock: regionName='{0}', key='{1}'", RegionName, key); LockData lockData = null; try { var lockKey = CacheNamespace.GetLockKey(key); var shouldRetry = _options.AcquireLockRetryStrategy.GetShouldRetry(); var wasLockAcquired = false; var shouldTryAcquireLock = true; while (shouldTryAcquireLock) { lockData = new LockData( key: Convert.ToString(key), lockKey: lockKey, // Recalculated each attempt to ensure a unique value. lockValue: _options.LockValueFactory.GetLockValue() ); if (TryAcquireLock(lockData)) { wasLockAcquired = true; shouldTryAcquireLock = false; } else { var shouldRetryArgs = new ShouldRetryAcquireLockArgs( RegionName, lockData.Key, lockData.LockKey, lockData.LockValue, _lockTimeout, _acquireLockTimeout ); shouldTryAcquireLock = shouldRetry(shouldRetryArgs); } } if (!wasLockAcquired) { var lockFailedArgs = new LockFailedEventArgs( RegionName, key, lockKey, _lockTimeout, _acquireLockTimeout ); _options.OnLockFailed(this, lockFailedArgs); } } catch (Exception e) { Log.Error("could not acquire cache lock: regionName='{0}', key='{1}'", RegionName, key); var evtArg = new ExceptionEventArgs(RegionName, RedisCacheMethod.Lock, e); _options.OnException(this, evtArg); if (evtArg.Throw) { throw new RedisCacheException(RegionName, "Failed to lock item in cache. See inner exception.", e); } } }
public virtual void Lock(object key) { log.DebugFormat("acquiring cache lock: regionName='{0}', key='{1}'", RegionName, key); try { var lockKey = CacheNamespace.GetLockKey(key); var shouldRetry = options.AcquireLockRetryStrategy.GetShouldRetry(); var wasLockAcquired = false; var shouldTryAcquireLock = true; while (shouldTryAcquireLock) { var lockData = new LockData( key: Convert.ToString(key), lockKey: lockKey, // Recalculated each attempt to ensure a unique value. lockValue: options.LockValueFactory.GetLockValue() ); if (TryAcquireLock(lockData)) { wasLockAcquired = true; shouldTryAcquireLock = false; } else { var shouldRetryArgs = new ShouldRetryAcquireLockArgs( RegionName, lockData.Key, lockData.LockKey, lockData.LockValue, lockTimeout, acquireLockTimeout ); shouldTryAcquireLock = shouldRetry(shouldRetryArgs); } } if (!wasLockAcquired) { var lockFailedArgs = new LockFailedEventArgs( RegionName, key, lockKey, lockTimeout, acquireLockTimeout ); options.OnLockFailed(this, lockFailedArgs); } } catch (Exception e) { log.ErrorFormat("could not acquire cache lock: regionName='{0}', key='{1}'", RegionName, key); var evtArg = new ExceptionEventArgs(RegionName, RedisCacheMethod.Lock, e); options.OnException(this, evtArg); if (evtArg.Throw) { throw new RedisCacheException(RegionName, "Failed to lock item in cache. See inner exception.", e); } } }
private async Task LockCoreAsync(object key, CancellationToken cancellationToken, bool sync = false) { cancellationToken.ThrowIfNull("cancellationToken"); if (cancellationToken.IsCancellationRequested) { return; } log.Debug("acquiring cache lock: regionName='{0}', key='{1}'", RegionName, key); try { var lockKey = CacheNamespace.GetLockKey(key); var shouldRetry = options.AcquireLockRetryStrategy.GetShouldRetry(); var wasLockAcquired = false; var shouldTryAcquireLock = true; while (shouldTryAcquireLock) { var lockData = new LockData( key: Convert.ToString(key), lockKey: lockKey, // Recalculated each attempt to ensure a unique value. lockValue: options.LockValueFactory.GetLockValue() ); bool lockAquired = sync ? this.TryAcquireLockAsync(lockData, true).ConfigureAwait(false).GetAwaiter().GetResult() : await this.TryAcquireLockAsync(lockData); if (lockAquired) { wasLockAcquired = true; shouldTryAcquireLock = false; } else { var shouldRetryArgs = new ShouldRetryAcquireLockArgs( RegionName, lockData.Key, lockData.LockKey, lockData.LockValue, lockTimeout, acquireLockTimeout ); shouldTryAcquireLock = shouldRetry(shouldRetryArgs); } } if (!wasLockAcquired) { var lockFailedArgs = new LockFailedEventArgs( RegionName, key, lockKey, lockTimeout, acquireLockTimeout ); options.OnLockFailed(this, lockFailedArgs); } } catch (Exception e) { log.Error("could not acquire cache lock: regionName='{0}', key='{1}'", RegionName, key); var evtArg = new ExceptionEventArgs(RegionName, RedisCacheMethod.Lock, e); options.OnException(this, evtArg); if (evtArg.Throw) { throw new RedisCacheException(RegionName, "Failed to lock item in cache. See inner exception.", e); } } }