Пример #1
0
        static void Main(string[] args)
        {
            XTrace.UseConsole();
            Console.WriteLine("进入Redis消息订阅者模式订单消息推送订阅者客户端!");

            EventBus eventBus = new EventBus();

            eventBus.EventRegister(typeof(OrderCreateEventNotifyHandle), typeof(OrderCreateEventData));
            eventBus.EventRegister(typeof(OrderCreateEventStockLockHandle), typeof(OrderCreateEventData));

            FullRedis fullRedis = new FullRedis("127.0.0.1:6379", "", 1);

            fullRedis.Log     = XTrace.Log;
            fullRedis.Timeout = 30000;
            OrderModel order = null;

            while (order == null)
            {
                order = fullRedis.BLPOP <OrderModel>("orders", 20);
                if (order != null)
                {
                    Console.WriteLine($"得到订单信息:{JsonConvert.SerializeObject(order)}");
                    //执行相关事件
                    eventBus.Trigger(new OrderCreateEventData()
                    {
                        Order = order,
                    });
                    //再次设置为null方便循环读取
                    order = null;
                }
            }
            Console.ReadLine();
        }
Пример #2
0
        /// <summary>
        /// 测试列表相关命令
        /// LPUSH、RPUSH、BLPOP、RLPOP
        /// </summary>
        static void TestList()
        {
            //TODO 使用模型
            FullRedis fullRedis = new FullRedis("127.0.0.1:6379", "", 1);

            fullRedis.Log = XTrace.Log;
            #region 以对象方式读写
            //LPUSH
            fullRedis.LPUSH("vm", new VmModel[] { new VmModel()
                                                  {
                                                      Id   = Guid.NewGuid(),
                                                      Name = "测试1"
                                                  }, new VmModel()
                                                  {
                                                      Id   = Guid.NewGuid(),
                                                      Name = "测试2"
                                                  }, new VmModel()
                                                  {
                                                      Id   = Guid.NewGuid(),
                                                      Name = "测试3"
                                                  } });

            //RPUSH
            fullRedis.RPUSH("vm", new VmModel[] { new VmModel()
                                                  {
                                                      Id   = Guid.NewGuid(),
                                                      Name = "测试4"
                                                  }, new VmModel()
                                                  {
                                                      Id   = Guid.NewGuid(),
                                                      Name = "测试5"
                                                  }, new VmModel()
                                                  {
                                                      Id   = Guid.NewGuid(),
                                                      Name = "测试6"
                                                  } });

            //BLPOP
            fullRedis.Timeout = 20000;  //需要注意BLPOP、RLPOP在列表无元素时候会阻塞进程,超时时间不能超过FullRedis默认的Timeout时间
            var vm = fullRedis.BLPOP <VmModel>("vm", 10);
            if (vm != null)
            {
                Console.WriteLine($"BLPOP得到的vm名称:{vm.Name}");
            }
            //BRPOP
            vm = fullRedis.BRPOP <VmModel>("vm", 10);
            if (vm != null)
            {
                Console.WriteLine($"BRPOP得到的vm名称:{vm.Name}");
            }
            //RPOPLPUSH
            vm = fullRedis.RPOPLPUSH <VmModel>("vm", "vmback");
            Console.WriteLine($"RPOPLPUSH的结果值:{vm.Name}");

            //BRPOPLPUSH
            vm = fullRedis.BRPOPLPUSH <VmModel>("vm", "vmback1", 2);
            Console.WriteLine($"BRPOPLPUSH的结果值:{vm.Name}");

            #endregion

            #region 以常规数据类型读写
            //LPUSH
            fullRedis.LPUSH("test", new int[] { 1, 2, 3 });
            //RPUSH
            fullRedis.RPUSH("test", new int[] { 4, 5, 6 });
            //BLPOP
            var testInt = fullRedis.BLPOP <int>("test", 10);
            if (testInt > 0)
            {
                Console.WriteLine($"BLPOP得到的testInt:{testInt.ToString()}");
            }
            //BRPOP
            testInt = fullRedis.BRPOP <int>("test", 10);
            if (testInt > 0)
            {
                Console.WriteLine($"BRPOP得到的testInt:{testInt.ToString()}");
            }
            //RPOPLPUSH
            var rpopTest = fullRedis.RPOPLPUSH <int>("test", "testback");
            Console.WriteLine($"RPOPLPUSH的结果值:{rpopTest.ToString()}");

            //BRPOPLPUSH
            var brpopTest = fullRedis.BRPOPLPUSH <int>("test", "testback1", 2);
            Console.WriteLine($"BRPOPLPUSH的结果值:{brpopTest.ToString()}");
            #endregion
        }