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

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

序

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

命令

multi与exec

命令行

码哩写作 码哩写作

最懂作者的AI辅助创作工具

码哩写作 60 查看详情 码哩写作

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 void testMultiExec(){        RedisClient client = RedisClient.create("redis://192.168.99.100:6379/0");        StatefulRedisConnection connection = client.connect();        RedisCommands 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 connection = client.connect();        RedisCommands 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 connection = client.connect();        RedisCommands 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 connection = client.connect();        RedisCommands syncCommands = connection.sync();        String key = "key";        syncCommands.watch(key);        //another connection        StatefulRedisConnection conn2 = client.connect();        RedisCommands 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));    }

小结

reids提供multi exec/discard指令,类似open commit/rollback transaction,不过exec遇到类型操作等错误时不会滚,该成功执行的命令还是成功执行,该失败的还是失败

multi exec保证的是,只要exec命令有执行成功,则事务中一系列的命令都能执行,如果exec因为网络等问题,server端没有接收到,则事务中的一系列命令都不会被执行

discard需要在有调用multi的前提下才能使用,该命令会清空事务队列等待执行的命令

redis提供watch指令,可以配合multi exec来使用,可以实现类似数据库的乐观锁的机制,一旦watch的key被其他client有更新,则整个exec操作失败

以上就是Redis的事务操作的命令与执行操作(代码)的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/381624.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Selenium 中操作 Ubuntu 系统文件上传对话框
上一篇 2025年11月6日 11:35:35
任天堂历史销量数据:硬件超8.6亿台 软件近59亿份
下一篇 2025年11月6日 11:36:00

相关推荐

发表回复

登录后才能评论
关注微信