當前位置: 首頁>>代碼示例>>C#>>正文


C# RedisClient.UnWatch方法代碼示例

本文整理匯總了C#中ServiceStack.Redis.RedisClient.UnWatch方法的典型用法代碼示例。如果您正苦於以下問題:C# RedisClient.UnWatch方法的具體用法?C# RedisClient.UnWatch怎麽用?C# RedisClient.UnWatch使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在ServiceStack.Redis.RedisClient的用法示例。


在下文中一共展示了RedisClient.UnWatch方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: 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.SetValueIfNotExists(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

                        redisClient.Watch(key);
                        string lockExpireString = redisClient.Get<string>(key);
                        long lockExpireTime;
                        if (!long.TryParse(lockExpireString, out lockExpireTime))
                        {
                            redisClient.UnWatch();  // since the client is scoped externally
                            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())
                        {
                            redisClient.UnWatch();  // since the client is scoped externally
                            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. The above call to Watch(_lockKey) enrolled the key in monitoring, so if it changes
                        //before we call Commit() below, the Commit will fail and return false, which means that another thread 
                        //was able to acquire the lock before we finished processing.
                        using (var trans = redisClient.CreateTransaction()) // we started the "Watch" above; this tx will succeed if the value has not moved 
                        {
                            trans.QueueCommand(r => r.Set(key, lockString));
                            return trans.Commit(); //returns false if Transaction failed
                        }
                    },
                timeOut
            );
        }
開發者ID:CatomStudio,項目名稱:ServiceStack.Redis,代碼行數:53,代碼來源:RedisLock.cs


注:本文中的ServiceStack.Redis.RedisClient.UnWatch方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。