Redis中事务操作的命令有哪些

53次阅读
没有评论

共计 3651 个字符,预计需要花费 10 分钟才能阅读完成。

自动写代码机器人,免费开通

丸趣 TV 小编给大家分享一下 Redis 中事务操作的命令有哪些,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

命令

multi 与 exec

命令行

127.0.0.1:6379  multi
127.0.0.1:6379  incr total
QUEUED
127.0.0.1:6379  incr len
QUEUED
127.0.0.1:6379  exec
1) (integer) 2
2) (integer) 2
127.0.0.1:6379  get total
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 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  multi
127.0.0.1:6379  set a hello
QUEUED
127.0.0.1:6379  set b world
QUEUED
127.0.0.1:6379  incr a
QUEUED
127.0.0.1:6379  set c part
QUEUED
127.0.0.1:6379  exec
1) OK
2) OK
3) (error) ERR value is not an integer or out of range
4) OK
127.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 1
127.0.0.1:6379  multi
127.0.0.1:6379  incr sum
QUEUED
127.0.0.1:6379  discard
127.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));
 }

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 中事务操作的命令有哪些”有了一定的了解,如果想了解更多相关知识,欢迎关注丸趣 TV 行业资讯频道,感谢各位的阅读!

向 AI 问一下细节

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-16发表,共计3651字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)