示例#1
0
        private void Worker()
        {
            var rdb = Redis.Connection.GetDatabase();

            while (true)
            {
                RedisValue result = RedisValue.Null;
                try
                {
                    var currentElementCounts = (int)rdb.ListLength(QueueName);
                    if (currentElementCounts == 0)
                    {
                        Thread.Sleep(DelayRetry);
                        continue;
                    }
                    result = rdb.ListRightPop(QueueName);
                    if (!result.HasValue || result.IsNullOrEmpty)
                    {
                        Thread.Sleep(DelayRetry);
                        continue;
                    }
                    T currentItem = result.ToObject <T>();
                    if (OnAction != null)
                    {
                        OnAction(currentItem).Wait();
                    }
                    Thread.Sleep(DelayOnSucceed);
                    continue;
                }
                catch (Exception e)
                {
                    if (OnException != null)
                    {
                        OnException(new BasePullEventArgs(e, result)).Wait();
                    }
                    Thread.Sleep(DelayAfterError);
                }
            }
        }
示例#2
0
        /// <summary>
        /// 获取hash中某个key对应的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        public T HashGet <T>(string key, string fields) where T : class
        {
            RedisValue rValue = db.HashGet(key, fields);

            return(rValue.ToObject <T>());
        }
示例#3
0
        /// <summary>
        /// 出队
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>

        public T Dequeue <T>(string key) where T : class
        {
            RedisValue rValue = db.ListLeftPop(key);

            return(rValue.ToObject <T>());
        }
示例#4
0
        /// <summary>
        /// 获取某个key并反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T StringGet <T>(string key) where T : class
        {
            RedisValue rValue = db.StringGet(key);

            return(rValue.ToObject <T>());
        }
示例#5
0
        public T JsonGet <T>(RedisKey key, CommandFlags flags = CommandFlags.None)
        {
            RedisValue str = Connection.GetDatabase().StringGet(key, flags);

            return(str.ToObject <T>());
        }