csredis icon indicating copy to clipboard operation
csredis copied to clipboard

关于pipeline使用的问题

Open wangpengliang815 opened this issue 4 years ago • 0 comments

如果我的case里只有这个,程序不会报错,对字符串进行IncrBy操作,错误的命令不被执行,正确的会被执行,Redis中多两条数据

 [TestMethod]
        public void Transaction()
        {
            string strKey = Guid.NewGuid().ToString();
            string strval = Faker.Name.FullName();

            string intKey = Guid.NewGuid().ToString();
            int intVal = Faker.RandomNumber.Next(1, 10);

            using var rc = csRedis.Nodes.First().Value.Get();
            rc.Value.Multi();
            rc.Value.SAdd(strKey, strval);
            rc.Value.SAdd(intKey, intVal);
            rc.Value.SAdd(strKey, 5);
            rc.Value.Exec();
        }

问题1:如果是这样,程序会报错:EXEC ---> CSRedis.RedisException: WRONGTYPE Operation against a key holding the wrong kind of value,Redis中只有两条数据,说明 rc.Value.Multi() 没有执行(正确的命令也没有)

  [TestMethod]
        public void Transaction()
        {
            string strKey = Guid.NewGuid().ToString();
            string strval = Faker.Name.FullName();

            string intKey = Guid.NewGuid().ToString();
            int intVal = Faker.RandomNumber.Next(1, 10);

            // 正常情况下redis命令批处理,返回[true,true]
            object[] result = csRedis.StartPipe()
                .Set(strKey, strval)
                .Set(intKey, intVal)
                .EndPipe();
            Console.WriteLine(JsonConvert.SerializeObject(result));

            // 使用事务命令
            using var rc = csRedis.Nodes.First().Value.Get();
            rc.Value.Multi();
            rc.Value.SAdd(strKey, strval);
            rc.Value.SAdd(intKey, intVal);
            rc.Value.SAdd(strKey, 5);           
            rc.Value.Exec();
        }

问题2:此时Redis中会多两条数据,错误的命令不被执行,但返回值是:[false, false, null],这里的返回值该如何理解

[TestMethod]
        public void Transaction()
        {
            string strKey = Guid.NewGuid().ToString();
            string strval = Faker.Name.FullName();

            string intKey = Guid.NewGuid().ToString();
            int intVal = Faker.RandomNumber.Next(1, 10);

          object[] result2 = csRedis.StartPipe()
                .Set(strKey, strval)
                .Set(intKey, intVal)
                .IncrBy(strKey, 5)
                .EndPipe();
            Console.WriteLine(JsonConvert.SerializeObject(result2));
        }

以上是使用中的一些疑惑,希望能在闲暇时刻解答。非常感谢~

wangpengliang815 avatar Sep 08 '21 15:09 wangpengliang815