当前位置: 首页>>代码示例>>C#>>正文


C# RedisClient.BeginTransaction方法代码示例

本文整理汇总了C#中RedisClient.BeginTransaction方法的典型用法代码示例。如果您正苦于以下问题:C# RedisClient.BeginTransaction方法的具体用法?C# RedisClient.BeginTransaction怎么用?C# RedisClient.BeginTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RedisClient的用法示例。


在下文中一共展示了RedisClient.BeginTransaction方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ExecTests

        public void ExecTests()
        {
            var okReply = "+OK\r\n";
            using(var mock = new MockConnector("localhost", 9999, okReply, "+QUEUED\r\n", "+QUEUED\r\n", "*2\r\n" + okReply + okReply))
            using(var redis = new RedisClient(mock))
            {
                int x = 0;
                using(var tran = redis.BeginTransaction())
                {
                    Assert.Equal("*1\r\n$5\r\nMULTI\r\n", mock.GetMessage());
                    tran.On(tran.Set("key1", "value1"), r =>
                    {
                        Assert.True(r);
                        x += 1;
                    });
                    Assert.Equal("*3\r\n$3\r\nSET\r\n$4\r\nkey1\r\n$6\r\nvalue1\r\n", mock.GetMessage());
                    tran.On(tran.Set("key2", "value2"), r =>
                    {
                        Assert.True(r);
                        Assert.Equal(1, x);
                        x += 2;
                    });
                    Assert.Equal("*3\r\n$3\r\nSET\r\n$4\r\nkey2\r\n$6\r\nvalue2\r\n", mock.GetMessage());
                    tran.Commit();
                    Assert.Equal("*1\r\n$4\r\nEXEC\r\n", mock.GetMessage());
                }
                Assert.Equal(string.Empty, mock.GetMessage());
                Assert.Equal(3, x);
            }

            this.RealCall(redis =>
            {
                int x = 0;
                using(var tran = redis.BeginTransaction())
                {
                    tran.On(tran.Set("key1", "value1"), r =>
                    {
                        Assert.True(r);
                        x += 1;
                    });
                    tran.On(tran.Set("key2", "value2"), r =>
                    {
                        Assert.True(r);
                        Assert.Equal(1, x);
                        x += 2;
                    });
                    tran.Commit();
                }
                Assert.Equal(3, x);

                Assert.Equal("value1", (string)redis.Get("key1"));
                Assert.Equal("value2", (string)redis.Get("key2"));
            });
        }
开发者ID:glorylee,项目名称:Aoite,代码行数:54,代码来源:RedisTranTests.cs

示例2: MultiTest

 public void MultiTest()
 {
     var okReply = "+OK\r\n";
     using(var mock = new MockConnector("localhost", 9999, okReply, okReply))
     using(var redis = new RedisClient(mock))
     {
         using(var tran = redis.BeginTransaction())
         {
             Assert.Equal("*1\r\n$5\r\nMULTI\r\n", mock.GetMessage());
         }
     }
 }
开发者ID:glorylee,项目名称:Aoite,代码行数:12,代码来源:RedisTranTests.cs

示例3: TranOutErrorTests

 public void TranOutErrorTests()
 {
     var okReply = "+OK\r\n";
     using(var mock = new MockConnector("localhost", 9999, okReply, "+QUEUED\r\n", "+QUEUED\r\n", "*2\r\n" + okReply + "-ERR Operation against a key holding the wrong kind of value\r\n", okReply))
     using(var redis = new RedisClient(mock))
     {
         int x = 0;
         using(var tran = redis.BeginTransaction())
         {
             Assert.Equal("*1\r\n$5\r\nMULTI\r\n", mock.GetMessage());
             tran.On(tran.Set("key1", "value1"), r =>
             {
                 Assert.True(r);
                 x += 1;
             });
             Assert.Equal("*3\r\n$3\r\nSET\r\n$4\r\nkey1\r\n$6\r\nvalue1\r\n", mock.GetMessage());
             tran.On(tran.IncrBy("key1"), r =>
             {
                 x += 2;
             });
             Assert.Equal("*2\r\n$4\r\nINCR\r\n$4\r\nkey1\r\n", mock.GetMessage());
             Assert.Throws<RedisReplyException>(() => tran.Commit());
             Assert.Equal("*1\r\n$4\r\nEXEC\r\n", mock.GetMessage());
         }
         Assert.Equal("*1\r\n$7\r\nDISCARD\r\n", mock.GetMessage());
         Assert.Equal(0, x);
     }
     this.RealCall(redis =>
     {
         using(var tran = redis.BeginTransaction())
         {
             tran.On(tran.Set("key1", "value1"), r =>
             {
                 Assert.True(r);
             });
             tran.IncrBy("key1");
             Assert.Throws<RedisReplyException>(() => tran.Commit());
         }
     });
 }
开发者ID:glorylee,项目名称:Aoite,代码行数:40,代码来源:RedisTranTests.cs


注:本文中的RedisClient.BeginTransaction方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。