Redis中事务操作的命令有哪些
发表于:2025-11-12 作者:千家信息网编辑
千家信息网最后更新 2025年11月12日,小编给大家分享一下Redis中事务操作的命令有哪些,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!命令multi与exec命令行127.0.0.1:6379> multiOK127.0
千家信息网最后更新 2025年11月12日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 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中事务操作的命令有哪些"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!
命令
事务
成功
实例
指令
篇文章
还是
乐观
前提
完了
数据
数据库
更多
机制
来使
知识
类型
网络
行业
资讯
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
上海数据网络技术服务代理商
笔记本电脑壁纸软件开发
图书订购数据库er图
网络技术还是信息安全
软件开发成本结转表
俄软件开发商
互联网科技开业主持稿
软件开发有限公司如何盈利
网站 服务器 网页
db2数据库怎么增加非空字
网络安全法涉及等保
cmo 数据库
激战2连接不了服务器
使命召唤服务器
服务器安全连接建立失败
做网络安全工程师需要什么
软件开发流程架构图
互联网 科技局
汽车智能网络技术的参考文献
基础数据库知识面试
计算机网络技术吃香不
网络规划中网络安全性测试
濮阳市网络安全工作会议
服务器在现代社会的地位
windows域服务器
华为 无线软件开发
服务器文件管理APP
神之刃修改数据库
h1z1有多少个服务器
创建远程服务器连接