当前位置: 首页>>代码示例>>C#>>正文


C# RedisClient.AcquireLock方法代码示例

本文整理汇总了C#中RedisClient.AcquireLock方法的典型用法代码示例。如果您正苦于以下问题:C# RedisClient.AcquireLock方法的具体用法?C# RedisClient.AcquireLock怎么用?C# RedisClient.AcquireLock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RedisClient的用法示例。


在下文中一共展示了RedisClient.AcquireLock方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Acquiring_lock_with_timeout

		public void Acquiring_lock_with_timeout()
		{
			var redisClient = new RedisClient(TestConfig.SingleHost);

			//Initialize and set counter to '1'
			redisClient.IncrementValue("atomic-counter"); 
			
			//Acquire lock and never release it
			redisClient.AcquireLock("testlock");

			var waitFor = TimeSpan.FromSeconds(2);
			var now = DateTime.Now;

			try
			{
				using (var newClient = new RedisClient(TestConfig.SingleHost))
				{
					//Attempt to acquire a lock with a 2 second timeout
					using (newClient.AcquireLock("testlock", waitFor))
					{
						//If lock was acquired this would be incremented to '2'
						redisClient.IncrementValue("atomic-counter"); 
					}
				}
			}
			catch (TimeoutException tex)
			{
				var timeTaken = DateTime.Now - now;
				Debug.WriteLine(String.Format("After '{0}', Received TimeoutException: '{1}'", timeTaken, tex.Message));

				var counter = redisClient.Get<int>("atomic-counter");
				Debug.WriteLine(String.Format("atomic-counter remains at '{0}'", counter));
			}
		}
开发者ID:modulexcite,项目名称:ServiceStack.Redis,代码行数:34,代码来源:SimpleLocks.cs

示例2: Use_multiple_redis_clients_to_safely_execute

		public void Use_multiple_redis_clients_to_safely_execute()
		{
			//The number of concurrent clients to run
			const int noOfClients = 5;
			var asyncResults = new List<IAsyncResult>(noOfClients);
			for (var i = 1; i <= noOfClients; i++)
			{
				var clientNo = i;
				var actionFn = (Action)delegate
				{
					var redisClient = new RedisClient(TestConfig.SingleHost);
					using (redisClient.AcquireLock("testlock"))
					{
						Debug.WriteLine(String.Format("client {0} acquired lock", clientNo));
						var counter = redisClient.Get<int>("atomic-counter");

						//Add an artificial delay to demonstrate locking behaviour
						Thread.Sleep(100);

						redisClient.Set("atomic-counter", counter + 1);
						Debug.WriteLine(String.Format("client {0} released lock", clientNo));
					}
				};

				//Asynchronously invoke the above delegate in a background thread
				asyncResults.Add(actionFn.BeginInvoke(null, null));
			}

			//Wait at most 1 second for all the threads to complete
			asyncResults.WaitAll(TimeSpan.FromSeconds(1));

			//Print out the 'atomic-counter' result
			using (var redisClient = new RedisClient(TestConfig.SingleHost))
			{
				var counter = redisClient.Get<int>("atomic-counter");
				Debug.WriteLine(String.Format("atomic-counter after 1sec: {0}", counter));
			}
		}
开发者ID:modulexcite,项目名称:ServiceStack.Redis,代码行数:38,代码来源:SimpleLocks.cs

示例3: Can_AcquireLock_TimeOut

		public void Can_AcquireLock_TimeOut()
		{
            var key = PrefixedKey("AcquireLockKeyTimeOut");
		    var lockKey = PrefixedKey("Can_AcquireLock_TimeOut");
            Redis.IncrementValue(key); //1
            var acquiredLock = Redis.AcquireLock(lockKey);
			var waitFor = TimeSpan.FromMilliseconds(1000);
			var now = DateTime.Now;

			try
			{
				using (var client = new RedisClient(TestConfig.SingleHost))
				{
					using (client.AcquireLock(lockKey, waitFor))
					{
                        Redis.IncrementValue(key); //2
					}
				}
			}
			catch (TimeoutException tex)
			{
                var val = Redis.Get<int>(key);
				Assert.That(val, Is.EqualTo(1));

				var timeTaken = DateTime.Now - now;
				Assert.That(timeTaken.TotalMilliseconds > waitFor.TotalMilliseconds, Is.True);
				Assert.That(timeTaken.TotalMilliseconds < waitFor.TotalMilliseconds + 1000, Is.True);
				return;
			}
			Assert.Fail("should have Timed out");
		}
开发者ID:stevegraygh,项目名称:ServiceStack.Redis,代码行数:31,代码来源:RedisClientTests.cs

示例4: SimulateLockTimeout

        public void SimulateLockTimeout()
        {
            var redisClient = new RedisClient(TestConfig.SingleHost);
            var waitFor = TimeSpan.FromMilliseconds(20);

            var loc = redisClient.AcquireLock("testlock",waitFor);
            Thread.Sleep(100); //should have lock expire
            using(var newloc = redisClient.AcquireLock("testlock", waitFor))
            {
                
            }
        }
开发者ID:modulexcite,项目名称:ServiceStack.Redis,代码行数:12,代码来源:SimpleLocks.cs

示例5: AcquireLock_using_Tasks

        public void AcquireLock_using_Tasks()
        {
            const int noOfClients = 4;
            var tasks = new Task[noOfClients];
            for (var i = 0; i < noOfClients; i++)
            {
                Thread.Sleep(2000);
                tasks[i] = Task.Factory.StartNew((object clientNo) =>
                {
                    try
                    {
                        Console.WriteLine("About to process " + clientNo);
                        //var redisClient = new RedisClient("xxxx.redis.cache.windows.net", 6379, "xxxx");
                        var redisClient = new RedisClient(TestConfig.SingleHost, 6379);

                        using (redisClient.AcquireLock("testlock1", TimeSpan.FromMinutes(3)))
                        {
                            Console.WriteLine("client {0} acquired lock", (int)clientNo);
                            var counter = redisClient.Get<int>("atomic-counter");

                            //Add an artificial delay to demonstrate locking behaviour
                            Thread.Sleep(100);

                            redisClient.Set("atomic-counter", counter + 1);
                            Console.WriteLine("client {0} released lock", (int)clientNo);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }

                }, i + 1);
            }
        }
开发者ID:CatomStudio,项目名称:ServiceStack.Redis,代码行数:35,代码来源:SimpleLocks.cs


注:本文中的RedisClient.AcquireLock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。