• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Redis的事务操作的命令与执行操作(代码)

mysql 搞代码 4年前 (2022-01-09) 40次浏览 已收录 0个评论
文章目录[隐藏]

本篇文章给大家带来的内容是关于Redis的事务操作的命令与执行操作(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

本文主要研究一下redis的事务操作

命令

multi与exec

  • 命令行

127.0.0.1:6379> multiOK127.0.0.1:6379> incr totalQUEUED127.0.0.1:6379> incr lenQUEUED127.0.0.1:6379> exec1) (integer) 22) (integer) 2127.0.0.1:6379> get total"2"127.0.0.1:6379> get len"2"
  • lettuce实例

    @Test    public vo<div style="color:transparent">本文来源gaodai^.ma#com搞#代!码网</div>id testMultiExec(){        RedisClient client = RedisClient.create("redis://192.168.99.100:6379/0");        StatefulRedisConnection<String, String> connection = client.connect();        RedisCommands<String, String> syncCommands = connection.sync();        syncCommands.set("hello","1");        syncCommands.set("world","2");        syncCommands.multi();        syncCommands.incr("hello");        syncCommands.incr("world");        //DefaultTransactionResult[wasRolledBack=false,result=[1, 2, 1, 3, 1]]        TransactionResult transactionResult = syncCommands.exec();        System.out.println(transactionResult);        System.out.println(syncCommands.get("hello"));        System.out.println(syncCommands.get("world"));    }

部分执行

  • 命令行

127.0.0.1:6379> multiOK127.0.0.1:6379> set a helloQUEUED127.0.0.1:6379> set b worldQUEUED127.0.0.1:6379> incr aQUEUED127.0.0.1:6379> set c partQUEUED127.0.0.1:6379> exec1) OK2) OK3) (error) ERR value is not an integer or out of range4) OK127.0.0.1:6379> get a"hello"127.0.0.1:6379> get b"world"127.0.0.1:6379> get c"part"
  • lettuce实例

    @Test    public void testMultiExecError(){        RedisClient client = RedisClient.create("redis://192.168.99.100:6379/0");        StatefulRedisConnection<String, String> connection = client.connect();        RedisCommands<String, String> syncCommands = connection.sync();        syncCommands.multi();        syncCommands.set("a","hello");        syncCommands.set("b","world");        syncCommands.incr("a");        syncCommands.set("c","part");        //DefaultTransactionResult[wasRolledBack=false,result=[OK, OK, io.lettuce.core.RedisCommandExecutionException: ERR value is not an integer or out of range, OK, 1]]        TransactionResult transactionResult = syncCommands.exec();        System.out.println(transactionResult);        System.out.println(syncCommands.get("a"));        System.out.println(syncCommands.get("b"));        System.out.println(syncCommands.get("c"));    }

multi与discard

  • 命令行

127.0.0.1:6379> set sum 1OK127.0.0.1:6379> multiOK127.0.0.1:6379> incr sumQUEUED127.0.0.1:6379> discardOK127.0.0.1:6379> get sum"1"
  • lettuce实例

    @Test    public void testMultiDiscard(){        RedisClient client = RedisClient.create("redis://192.168.99.100:6379/0");        StatefulRedisConnection<String, String> connection = client.connect();        RedisCommands<String, String> syncCommands = connection.sync();        syncCommands.incr("key1");        syncCommands.multi();        syncCommands.incr("key1");        //需要有multi才可以执行discard,成功返回OK        String result = syncCommands.discard();        System.out.println(result);        System.out.println(syncCommands.get("key1"));    }

check and set

    @Test    public void testWatch(){        RedisClient client = RedisClient.create("redis://192.168.99.100:6379/0");        StatefulRedisConnection<String, String> connection = client.connect();        RedisCommands<String, String> syncCommands = connection.sync();        String key = "key";        syncCommands.watch(key);        //another connection        StatefulRedisConnection<String, String> conn2 = client.connect();        RedisCommands<String, String> syncCommands2 = conn2.sync();        syncCommands2.set(key, "a");        syncCommands.multi();        syncCommands.append(key, "b");        //DefaultTransactionResult [wasRolledBack=true, responses=0]        TransactionResult transactionResult = syncCommands.exec();        System.out.println(transactionResult);        System.out.println(syncCommands.get(key));    }

小结

发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址