示例#1
0
        /// <summary>
        /// ServiceStack.Redis
        /// Redis 有事务,但是没有回滚的操作;如果在事务过程中,有地方改了监控key的值,则整个事务会提交失败;
        /// </summary>
        public static void TransationDemo()
        {
            bool flag = false;

            using (RedisClient redisClient = new RedisClient("127.0.0.1", 6379))
            {
                redisClient.Set("a", "1");
                redisClient.Set("b", "1");
                redisClient.Set("c", "1");

                redisClient.Watch("a", "b", "c");//要把这些key放入到监控中
                using (var trans = redisClient.CreateTransaction())
                {
                    trans.QueueCommand(p => p.Set("a", "3"));//在这可以加个断点,然后用工具去改a的值为2
                    trans.QueueCommand(p => p.Set("b", "3"));
                    trans.QueueCommand(p => p.Set("c", "3"));

                    flag = trans.Commit();
                }
                Console.WriteLine($"提交结果:{flag},获取值a:{redisClient.Get<string>("a")},b:{redisClient.Get<string>("b")},c:{redisClient.Get<string>("c")}");
            }
        }