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


C# RedisClient.SetEntryIfNotExists方法代码示例

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


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

示例1: RedisLock

        public RedisLock(RedisClient redisClient, string key, TimeSpan? timeOut)
        {
            this.redisClient = redisClient;
            this.key = key;

            ExecExtensions.RetryUntilTrue(
                () => redisClient.SetEntryIfNotExists(key, "lock " + DateTime.UtcNow.ToUnixTime()),
                timeOut
            );
        }
开发者ID:EvgeniyProtas,项目名称:servicestack,代码行数:10,代码来源:RedisLock.cs

示例2: RedisLock

		public RedisLock(RedisClient redisClient, string key, TimeSpan? timeOut)
		{
			this.redisClient = redisClient;
			this.key = key;

			ExecExtensions.RetryUntilTrue(
				() =>
				    {
                        //This pattern is taken from the redis command for SETNX http://redis.io/commands/setnx
				        
                        //Calculate a unix time for when the lock should expire
                        TimeSpan realSpan = timeOut ?? new TimeSpan(365, 0, 0, 0); //if nothing is passed in the timeout hold for a year
				        DateTime expireTime = DateTime.UtcNow.Add(realSpan);
				        string lockString = (expireTime.ToUnixTimeMs() + 1).ToString();
                        
                        //Try to set the lock, if it does not exist this will succeed and the lock is obtained
				        var nx = redisClient.SetEntryIfNotExists(key, lockString);
                        if (nx)
                            return true;

                        //If we've gotten here then a key for the lock is present. This could be because the lock is
                        //correctly acquired or it could be because a client that had acquired the lock crashed (or didn't release it properly).
                        //Therefore we need to get the value of the lock to see when it should expire
				        string lockExpireString = redisClient.Get<string>(key);
                        long lockExpireTime;
                        if (!long.TryParse(lockExpireString, out lockExpireTime))
                            return false;
                        //If the expire time is greater than the current time then we can't let the lock go yet
                        if (lockExpireTime > DateTime.UtcNow.ToUnixTimeMs())
                            return false;

                        //If the expire time is less than the current time then it wasn't released properly and we can attempt to 
                        //acquire the lock. This is done by setting the lock to our timeout string AND checking to make sure
                        //that what is returned is the old timeout string in order to account for a possible race condition.
                        return redisClient.GetAndSetEntry(key, lockString) == lockExpireString;
				    },
				timeOut
			);
		}
开发者ID:Eddie0330,项目名称:ServiceStack.Redis,代码行数:39,代码来源:RedisLock.cs


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