Exemplo n.º 1
0
		private bool ExtendInstance(RedisConnection cache)
		{
			var redisKey = GetRedisKey(cache.RedisKeyFormat, Resource);
			var host = GetHost(cache.ConnectionMultiplexer);

			var result = false;

			try
			{
				Logger.Debug(() => $"ExtendInstance enter {host}: {redisKey}, {LockId}, {expiryTime}");
				var extendResult = (long) cache.ConnectionMultiplexer
					.GetDatabase(cache.RedisDatabase)
					.ScriptEvaluate(ExtendIfMatchingValueScript, new RedisKey[] {redisKey}, new RedisValue[] {LockId, (int) expiryTime.TotalSeconds}, CommandFlags.DemandMaster);

				result = (extendResult == 1);
			}
			catch (Exception ex)
			{
				Logger.Debug($"Error extending lock instance {host}: {ex.Message}");
			}

			Logger.Debug(() => $"ExtendInstance exit {host}: {redisKey}, {LockId}, {result}");

			return result;
		}
Exemplo n.º 2
0
		private async Task<bool> UnlockInstanceAsync(RedisConnection cache)
		{
			var redisKey = GetRedisKey(cache.RedisKeyFormat, Resource);
			var host = GetHost(cache.ConnectionMultiplexer);

			var result = false;

			try
			{
				Logger.Debug(() => $"UnlockInstanceAsync enter {host}: {redisKey}, {LockId}");
				result = (bool) await cache.ConnectionMultiplexer
					.GetDatabase(cache.RedisDatabase)
					.ScriptEvaluateAsync(UnlockScript, new RedisKey[] { redisKey }, new RedisValue[] { LockId }, CommandFlags.DemandMaster)
					.ConfigureAwait(false);
			}
			catch (Exception ex)
			{
				Logger.Debug($"Error unlocking lock instance {host}: {ex.Message}");
			}

			Logger.Debug(() => $"UnlockInstanceAsync exit {host}: {redisKey}, {LockId}, {result}");

			return result;
		}
Exemplo n.º 3
0
		private async Task<bool> LockInstanceAsync(RedisConnection cache)
		{
			var redisKey = GetRedisKey(cache.RedisKeyFormat, Resource);
			var host = GetHost(cache.ConnectionMultiplexer);

			var result = false;

			try
			{
				Logger.Debug(() => $"LockInstanceAsync enter {host}: {redisKey}, {LockId}, {expiryTime}");
				result = await cache.ConnectionMultiplexer
					.GetDatabase(cache.RedisDatabase)
					.StringSetAsync(redisKey, LockId, expiryTime, When.NotExists, CommandFlags.DemandMaster)
					.ConfigureAwait(false);
			}
			catch (Exception ex)
			{
				Logger.Debug($"Error locking lock instance {host}: {ex.Message}");
			}

			Logger.Debug(() => $"LockInstanceAsync exit {host}: {redisKey}, {LockId}, {result}");

			return result;
		}