Redis用LPUSH和RPOP实现消息队列

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;
namespace RedisTest3_LPUSH
{
    class Program
    {
        static void Main(string[] args)
        {
            var r = Console.ReadLine().ToString();
            if (r == "push")
            {
                push();
            }
            else
            {
                pop();
            }

            Console.ReadLine();
        }

        public static void push()
        {
            Console.WriteLine("push---");
            IRedisClient client = new RedisClient("127.0.0.1", 6379);
           while(true)
            {
                client.PushItemToList("list1", Console.ReadLine());
            }
            Console.WriteLine("ok");
          
        }

        public static void pop()
        {
            Console.WriteLine("pop---");
            IRedisClient client = new RedisClient("127.0.0.1", 6379);
            while (true)
            {
                var msg = client.PopItemFromList("list1");
                if(!string.IsNullOrEmpty(msg))
               Console.WriteLine(msg);
            }


        }
    }
}
View Code

posted @ 2017-03-23 14:44  甜菜波波  阅读(1957)  评论(0编辑  收藏  举报